Module blog::daily_rfcs::trait_sys::_0255_object_safety
source · [−]Expand description
Restrict which traits can be used to make trait objects
Note
Object-safe method
- require
Self : Sized
; OR - meet all of the following conditions:
- must not have any type parameters; AND
- must have a receiver that has type
Self
or which dereferences to theSelf
type;- for now, this means
self
,&self
,&mut self
, orself: Box<Self>
, but eventually this should be extended to custom types like self: Rcand so forth.
- for now, this means
- must not use
Self
(in the future, where we allow arbitrary types for the receiver, Self may only be used for the type of the receiver and only where we allow Sized? types).
Object-safe trait
- all of its methos are object safe.
- the trait does not require that
Self : Sized
How to bypass object-safety rules
- split trait into object-safe and non-object-safe parts.
- add
where Self: Sized
to non-object-safe methods.(derive from Object-safe method rule 1)