Pre-RFC: #[derive] support for arithmetic traits (Add, Sub, Mul, Div) on structs

I have thought a bit about potential syntax for quickly implementing traits, and this could reduce the help reduce the boiler plate.

struct Foo(u32);

use std::fmt::*;

impl Foo {
    fn Debug::fmt(&self, f: &mut Formatter) -> Result { .. }

    // with this might be too much, but possible in theory
    fn std::ops::Add::<Output = Self>::add(self, other: Self) -> Self::Output { .. }
}

(specifically the Self::Output as a part of return is a bit shakey, since you'd often have to write Add::Output, or even <Self as Add>::Output to disambiguate. (trait::Output could be used instead?))

For things like Add, the piece I'd like is to have (more than a derive) is inferred associated types.

For example, there's really no need in

impl Iterator for MyType {
    fn next(&mut self) -> Option<i32> { ... }
}

for me to have to write the

    type Item = i32;

because it's the only possible thing given that next.

And that would immediately make it that little bit simpler to implement Add, Sub, IntoIterator, even things like Deref.

8 Likes