nikomatsakis / fields-in-traits-rfc

An (experimental) RFC repo devoted to the "fields in traits" RFC.
Apache License 2.0
65 stars 1 forks source link

example use cases #13

Open nikomatsakis opened 7 years ago

nikomatsakis commented 7 years ago

It'd be great to accumulate more use-cases.

Open thread for suggestions. =)

nielsle commented 6 years ago

Delegation traits (Code stolen from EddyB)

// Potentially somewhere central, or generated per-trait.
trait Delegate/*Health*/ {
    type Delegatee;
    delegatee: Self::Delegatee;
}
impl<T: Delegate/*Health*/> Health for T
    where T::Delegatee: Health
{
    fn health(&self) -> u8 {
        self.delegatee.health()
    }
}
impl Delegate/*Health*/ for Player {
    delegatee => self.health;
}

https://github.com/rust-lang/rfcs/pull/2393#issuecomment-391978163