Reading The Reference here: https://doc.rust-lang.org/reference/items/traits.html
#![allow(unused)]
fn main() {
use std::rc::Rc;
// Examples of non-object safe traits.
trait NotObjectSafe {
const CONST: i32 = 1; // ERROR: cannot have associated const
fn foo() {} // ERROR: associated function without Sized
fn returns(&self) -> Self; // ERROR: Self in return type
fn typed<T>(&self, x: T) {} // ERROR: has generic type parameters
fn nested(self: Rc<Box<Self>>) {} // ERROR: nested receiver not yet supported
}
struct S;
impl NotObjectSafe for S {
fn returns(&self) -> Self { S }
}
let obj: Box<dyn NotObjectSafe> = Box::new(S); // ERROR
}
I want to ask about this:
fn foo() {} // ERROR: associated function without Sized
I am making a proc-macro for get info about an enum.
pub trait EnumReflexion {
/// Returns the identifier of the enum type as an &str
fn enum_name<'a>() -> &'a str;
/// Returns an [`my_proj::my_crate::EnumInfo`] entity that contains
/// runtime reflexive info about `Self`.
fn enum_info<'a>() -> EnumInfo<'a>;
}
Brief piece of the impl on the macro:
let quote = quote! {
impl my_proj::my_crate::EnumReflexion for #ty {
fn enum_name<'a>() -> &'a str {
#ty_str
}
fn enum_info<'a>() -> EnumInfo<'a> {
#enum_info
}
}
};
I can compile my code perfectly fine, and it passes tests like this:
#[test]
fn get_enum_name() {
let variant = EnumMock::A;
assert_eq!("EnumMockd", EnumMock::enum_name());
}
#[derive(EnumInfo)]
enum EnumMock {
A,
B
}
Did that restriction of Sized in associated functions on traits get dropped? Or am I missing something?