E4038#
Trait object for the type is not allowed.
Erroneous Example#
pub fn eq_object() -> &Eq {
3 as &Eq
}
The example above tries to create an object of the Eq trait.
This is not allowed, as the trait is not object safe.
The Eq trait is defined as follows:
pub(open) trait Eq {
op_equal(Self, Self) -> Bool
}
... which is why the following error is given on line 2:
Trait object for Eq is not allowed: `Self` occur multiple times in the type of method op_equal
Suggestion#
Use an object-safe trait to create a trait object.
For a trait to be object-safe, its dispatchable functions must use and only use the Self
type as the first parameter. For example, Show is such a trait:
pub fn show_object() -> &Show {
3 as &Show
}