by alilleybrinker · 2y ago · 0 comments
They're right that trait objects are dynamically sized types, which means they can't be passed by value to functions, but wrong that they need to be boxed; they can instead be put behind a reference. Both of the following are valid types.
type DynFutureBox = Pin<Box<dyn Future<Output = ()>>>;
type DynFutureRef<'f> = Pin<&'f dyn Future<Output = ()>>;
You can see this in the Rust Playground here: https://play.rust-lang.org/?version=stable&mode=debug&editio...To me traits are like a definition of capabilities. A way to duck type things.
See https://doc.rust-lang.org/reference/types/trait-object.html (`dyn Trait`, runtime dynamic dispatch) vs https://doc.rust-lang.org/reference/types/impl-trait.html (`impl Trait`, compile-time monomorphization)
Traits conceptually are kind of like definitions of capabilities. So you're not really wrong about that, that understanding probably may even help you.