[Pre-RFC] Forward impls

We ‘support’ named impls today through this very wild hack. If you would like your trait to support named impls, you can do this:

trait ToString<Name: NamedToString<ToString = Self>> {
    fn to_string(&self) -> String;
}

trait NamedToString {
    type Receiver: ?Sized;
    fn to_string(this: &Self::Receiver) -> String;
}

impl<Name> ToString<Name> for Name::Receiver where Name: NamedToString {
    fn to_string(&self) -> String {
        Name::to_string(self)
    }
}

Your clients can provide named impls for foreign traits:

struct ToStringForIntSlice;

impl NamedToString for ToStringForIntSlice {
    type Receiver = [i32];
    fn to_string(this: &[i32]) -> String {
        panic!()
    }
}

To be clear: I don’t think anyone should do this.

2 Likes