[rust-dev] Implementation inheritance or mixins

Lucian Branescu lucian.branescu at gmail.com
Tue Dec 25 11:43:58 PST 2012


Traits are basically what you want here. You can have a trait with the emit
method and implement it on both structs.
On 25 Dec 2012 19:25, "Michael Neumann" <mneumann at ntecs.de> wrote:

> Hi,
>
> 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!
>
> Michael
> ______________________________**_________________
> Rust-dev mailing list
> Rust-dev at mozilla.org
> https://mail.mozilla.org/**listinfo/rust-dev<https://mail.mozilla.org/listinfo/rust-dev>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20121225/788cb325/attachment.html>


More information about the Rust-dev mailing list