[rust-dev] Implementation of traits in Rust: could it be dynamic?

Patrick Walton pcwalton at mozilla.com
Tue Jul 22 11:47:18 PDT 2014

On 7/22/14 10:16 AM, Lionel Parreaux wrote:
> I'm not sure whether this is a big problem in practice, but I was
> wondering if it would be possible to switch to some runtime mechanism in
> cases like this. Maybe we could make a special version of every generic
> functions, that takes a dictionary at runtime and that would be able to
> handle types unknown at compile-time. We would switch to this version
> when monomorphization does not work. It could also allow dynamic linking
> of libraries with generic functions, or it could be a way to compile
> some programs (or some parts of programs) much faster.

The hard part about doing that is not the dictionary passing. The hard 
part is that generic types may have unknown size or alignment. In 
Haskell this is not a problem because the language is garbage-collected 
and lazy so values have a uniform representation. But in Rust this is 
not true.

Old Rust used to try to use runtime dictionary passing, where the 
dictionary contained size and alignment information, and all 
size/alignment info was computed at runtime for generics. I cannot 
overstate how *fiendishly* complex this was. We never got all the bugs 
out. In many cases, the amount of runtime code generated to compute size 
and alignment outweighed the cost of just monomorphizing.

I strongly feel that the current system, where you can use generic type 
parameters to get monomorphization or trait objects to get dictionary 
passing, is the sweet spot.


More information about the Rust-dev mailing list