Live data from Hacker News

Abstraction without overhead: traits in Rust

blog.rust-lang.org

21–30 of 150 posts

Re: Abstraction without overhead: traits in Rust

#21
post #5

Great article. The main bit of new information for me was that while Rust supports dynamic dispatch, its implementation has a noticeable difference from C++. In C++, the vtable pointer is in the object itself. In Rust, it's stored inside what is essentially a "fat pointer." Pointers to traits ("trait objects") are actually two pointers: the pointer to the vtable and the pointer to the actual object. This seems to hav…

    >- allows traits to be implemented for existing types, as opposed
    >  to C++ where the type's declaration has to list all base classes.

    >- allows a type to be used through dynamic dispatch while allowing users
    >  who don't need this to avoid the vtable overhead.
In C++ you can have this as well. For example, std::function is able to wrap any callable type without them having to have any base classes. Sean Parent gives a great talk about this: http://channel9.msdn.com/Events/GoingNative/2013/Inheritance...

Re: Abstraction without overhead: traits in Rust

#23

> Traits are interfaces So why not use "interface" keyword?

Because trait is a much better word for it: https://en.wikipedia.org/wiki/Trait_(computer_programming)

So in Rust traits can include full methods bodies, not only signatures?

Re: Abstraction without overhead: traits in Rust

#24

Earlier quoted context omitted.

Because trait is a much better word for it: https://en.wikipedia.org/wiki/Trait_(computer_programming)

So in Rust traits can include full methods bodies, not only signatures?

Yes: http://rustbyexample.com/trait.html

Re: Abstraction without overhead: traits in Rust

#25

> Traits are interfaces So why not use "interface" keyword?

> Traits are somewhat between an interface and a mixin: an interface is made only of method signatures, while a trait includes also the full method definitions, on the other side mixins include method definitions, but they can also carry state through attributes while traits usually don't.

Rust's traits may specify just a method signature and force the implementor of the trait to implement the method, but they may specify full definitions of methods too.

Re: Abstraction without overhead: traits in Rust

#26

Earlier quoted context omitted.

Because trait is a much better word for it: https://en.wikipedia.org/wiki/Trait_(computer_programming)

So in Rust traits can include full methods bodies, not only signatures?

Yes. The feature has been proposed for Java too: "defender methods".

Originally I resisted them on the grounds of not being necessary, but they're used all over now. Being able to supply a default implementation is extremely useful.

Re: Abstraction without overhead: traits in Rust

#27
post #21
post #5

Great article. The main bit of new information for me was that while Rust supports dynamic dispatch, its implementation has a noticeable difference from C++. In C++, the vtable pointer is in the object itself. In Rust, it's stored inside what is essentially a "fat pointer." Pointers to traits ("trait objects") are actually two pointers: the pointer to the vtable and the pointer to the actual object. This seems to hav…

>- allows traits to be implemented for existing types, as opposed > to C++ where the type's declaration has to list all base classes. >- allows a type to be used through dynamic dispatch while allowing users > who don't need this to avoid the vtable overhead. In C++ you can have this as well. For example, std::function is able to wrap any callable type without them having to have any base classes. Sean Parent gives a…

C++ is Turing-complete, so you can do whatever you want. What Rust offers is an hierarchy free (i.e., no inheritance) polymorphism mechanism as a core part of the language.

Re: Abstraction without overhead: traits in Rust

#29
post #21

Earlier quoted context omitted.

>- allows traits to be implemented for existing types, as opposed > to C++ where the type's declaration has to list all base classes. >- allows a type to be used through dynamic dispatch while allowing users > who don't need this to avoid the vtable overhead. In C++ you can have this as well. For example, std::function is able to wrap any callable type without them having to have any base classes. Sean Parent gives a…

C++ is Turing-complete, so you can do whatever you want. What Rust offers is an hierarchy free (i.e., no inheritance) polymorphism mechanism as a core part of the language.

The turing-complete argument makes no sense here.

Re: Abstraction without overhead: traits in Rust

#30
post #2

There is always overhead when adding abstractions--the only question is whether you pay at runtime or at compile time. C++ (and presumably Rust) choose the latter, Python and Go choose the former.

Yes, in these discussions, the overhead being referred to is runtime overhead. There's also overhead in the sense of complexity for the programmer, which isn't really either of those two.

> There's also overhead in the sense of complexity for the programmer

Well, from the programmers' perspectives there are both read-time and write-time overheads. In C++-land, the discussion about the new (to C++) 'auto' keyword is about the trade-offs between the two.

Post reply on HN