reem / rust-modifier

Convenient chaining APIs for free
29 stars 9 forks source link

Move generic argument on Set methods to trait level #5

Closed bvssvni closed 9 years ago

bvssvni commented 10 years ago

This will make it possible to inherit generic constraints for modifiers, such as:

pub trait Window: Set<Title> + Set<Size> {};

New trait design for Set:

pub trait Set<M: Modifier<Self>> {
    fn set(mut self, modifier: M) -> Self {
        modifier.modify(&mut self);
        self
    }

    fn set_mut(&mut self, modifier: M) -> &mut Self {
        modifier.modify(self);
        self
    }
}
reem commented 10 years ago

This is a good idea. I will make this change when I have a minute to update Iron at the same time.

reem commented 9 years ago

I made this change, but it breaks type inference at several usage sites in Iron, so I reverted it.

bvssvni commented 9 years ago

Notice that Set<Title> + Set<Size> has to be replaced with SetTitle and SetSize with an impl for every Modifier.

bvssvni commented 9 years ago

By moving the default trait methods to the blanket impl, I got rid of the Modifier constraint. This allows default trait impls for SetTitle and SetSize, such that no extra impl is required beside Modifier.

reem commented 9 years ago

Closing due to inactivity, but @bvssvni I'd be curious to hear your thoughts about this since November, so feel free to make a new issue or comment here and have me re-open.

bvssvni commented 9 years ago

@reem That's OK.

piston-quack uses a design where Modifier/SetAt is implemented for a tuple (Data, Object) with a Pair trait for associated types. It ended up a bit complex to solve all the edge cases with generics and uses a macro to generate the impls.