rusttraitsgeneric-programming

Implementing a generic incrementable trait in Rust


I'm trying to understand how to implement a generic trait in Rust.

While I've seen a number of examples, the examples are too tied to a specific use (e.g. genomic mutators) for me to be able to understand at this point in my Rust development.

Instead, here's a simple example based on something fairly universal--incrementing:

trait Incrementable {
    fn post_inc(&mut self) -> Self;
    fn post_inc_by(&mut self, n: usize) -> Self;
}

impl Incrementable for usize {
    fn post_inc(&mut self) -> Self {
        let tmp = *self;
        *self += 1;
        tmp
    }

    //"Overload" for full generalizability
    fn post_inc_by(&mut self, n: usize) -> Self {
        let tmp = *self;
        *self += n;
        tmp
    }
}

fn main() {
    let mut result = 0;
    assert!(result.post_inc() == 0);
    assert!(result == 1);

    assert!(result.post_inc_by(3) == 1);
    assert!(result == 4);
}

The above code works, but is lacking because it isn't generalizable to all numeric types without writing a lot of boilerplate code.

In my attempts to generalize the above code, I've gotten into fights with the type system, borrow checker or been forced down a path of implementing FromPrimitive for every type I want to support in my generic version (effectively putting me back to square one).

Can you help me understand how to implement Incrementable generically, such that post_inc() and post_inc_by() work for at least all integer and float types, ideally without having to write an implementation for each type?

I am hoping the answer will help me see how traits, implementations, types and associated types can work together in a more straightforward use case than I've been able to come across.

I'm on Rust 1.16.0.


Solution

  • I know this is quite old, but I think there is a cleaner solution (maybe it was not working at the time of the original post).

    trait Incrementable {
        fn post_inc(&mut self) -> Self;
        fn post_inc_by(&mut self, n: Self) -> Self;
    }
    
    impl<T> Incrementable for T where 
        T: Copy + std::ops::AddAssign<Self> + From<bool> // <-
    {
        fn post_inc(&mut self) -> Self {
            self.post_inc_by(Self::from(true)) // <- 
        }
    
        fn post_inc_by(&mut self, n: Self) -> Self {
            let tmp = *self;
            *self += n;
            tmp
        }
    }
    

    The trick here is that all numeric types seem to be From<bool>, and that true is actually 1.

    Other solutions rely on macros to generate all the one()s, leverage third-party crates, or fail to work with i8 if using some kind of From<u8> when incrementing (or the inverse). With the proposed approach the only problem I see is that from(true)=1 is documented but not very explicit.

    Some tests:

    fn test_inc() {
        let mut x = 42i8; x.post_inc(); println!("{x}"); // 43
        let mut x = 42u32; x.post_inc(); println!("{x}"); // 43
        let mut x = 42usize; x.post_inc(); println!("{x}"); // 43
        let mut x = 4.2f64; x.post_inc(); println!("{x}"); // 5.2
        //...
    }