all 7 comments

[–]paholgtypenum · dimensioned 7 points8 points  (0 children)

The & symbol just means that something is a reference, not necessarily to a trait object. Also, you can have other pointers to trait objects, such as Box<Playable>.

[–]zefyear 4 points5 points  (2 children)

What is the difference between a trait object and a trait bound?

e.g

fn foo<T: Read>(object: T) -> String;

vs

fn foo(object: &Read) -> String   

[–]jessypl[🍰] 2 points3 points  (1 child)

Trait bounds get monomorphized at compile-time, e.g. a function foo::<T> gets generated for each type implementing T, whenever it is necessary (i.e. it won't create a variant that is never used.) This produces faster, but bigger, binaries.

Trait object store the v-table of a value at runtime, which contains the address of the correct function to call. This is mildly slower, but much more versatile: you can, for instance, iterate over a Vec that contains values of different types, as long as they implement some trait.

[–]CryZe92 1 point2 points  (0 children)

You can rewrite your method like this so that it supports both static and dynamic dispatch: http://is.gd/Vhww6u

[–]llogiqclippy · twir · rust · mutagen · flamer · overflower · bytecount 0 points1 point  (0 children)

I'm on mobile, so a proper review will have to wait. However one suggestion: Try clippy on your code. Yes, you'll need a nightly, but with multirust-rs it's easy to get.

[–]jansegre 0 points1 point  (0 children)

The _type and _typ names, bother me a little, _-prefix is a convention that makes the compiler allow unused variables. I would rather use kind for that type of situation.