Re:Virtual Structs Part 1: Where Rust’s Enum Shines
by dcb18 on Tuesday May 05, 2015 @01:16PM
(#9493648)
A trait is a type, but the type does not have a size, which is the reason it is not possible to store a trait instance directly in a Vec (or any other place needing a sized type). It is possible to store a pointer to a trait object in a Vec, which would look like Vec<Box<CanineTrait>>.
Re:Virtual Structs Part 1: Where Rust’s Enum Shines
by jarrettc on Tuesday May 05, 2015 @02:19PM
(#9494014)
Ah, my mistake. So would I be correct in saying that a trait is a type, but it does not necessarily implement the Sized trait, and if it does not, then it cannot be allocated directly on the stack?
Also, I've been wondering something about dereferencing a Box where the inner type is just a trait. If I dereference a Box<CanineTrait> and call "bark," how is the correct implementation found at runtime? Is there a vtable or something analogous?
Re:Virtual Structs Part 1: Where Rust’s Enum Shines
by Rusky on Tuesday May 05, 2015 @02:22PM
(#9494039)
Trait object pointers are "fat"- they consist of both a pointer to the object and a vtable pointer (much like Go interfaces if you've used those). Other DSTs are similar- pointers to slices consist of both a pointer to the elements and a count.
Re:Virtual Structs Part 1: Where Rust’s Enum Shines
by losvedir on Tuesday May 05, 2015 @02:33PM
(#9494099)
> If I dereference a Box<CanineTrait> and call "bark," how is the correct implementation found at runtime? Is there a vtable or something analogous?
I think a vtable. It's my understanding that "trait objects" (boxed traits) are how you implement dynamic dispatch in rust. If instead you were talking about a `fn foo<T: CanineTrait>(x: T) { ... }` trait bound (the more common case), there will be monomorphization and static dispatch.
Source: http://doc.rust-lang.org/1.0.0-beta/book/static-and-dynamic-...
Re:Virtual Structs Part 1: Where Rust’s Enum Shines
by steveklabnik on Tuesday May 05, 2015 @02:42PM
(#9494164)
After the beta landed, I re-did the TOC for the book, and so this information will be at http://doc.rust-lang.org/nightly/book/trait-objects.html in the future. (where nightly = 1.0.0, of course)
So it's a lot of boilerplate. I think it can be slimmed down with a macro, but still.
You might think traits rather than enums are the way to go here. Sometimes that may be true. But often, an enum is far preferable because in Rust, a trait is not a type, but an enum is. That means you cannot, for example, have a "Vec<CanineTrait>," but you can have a "Vec<CanineEnum>."