[rust-dev] Implementation inheritance or mixins

Michael Neumann mneumann at ntecs.de
Tue Dec 25 11:25:13 PST 2012


I am trying to switch over my msgpack implementation [1] to use the
Encoder/Decoder trait from serialize.rs. I want to implement a basic
encoder and a more optimized one (in terms of generated storage).
Most of the emit_() functions will be shared between the two, just
integers would be emitted based on their actual range and not depending
on their type. Now my question is wheather I can reuse some code by
using something like a Mixin?

Is there something like this:

   struct Encoder { ... }

   pub impl Encoder : serialize::Encoder {
     fn emit_nil ...

   struct OptEncoder { ... }

   pub impl OptEncoder : serialize::Encoder {
     include Encoder

     fn emit_uint(...) { ... } // overwrite emit_uint

Or how would you implement that?

Actually what I want is some way to literally "include" some functions into
the scope of an implementation (like a mixin). Similar in the way Ruby or
Sather handles this.

Merry Christmas!


More information about the Rust-dev mailing list