#How to write less where clauses + lifetimes ?

8 messages · Page 1 of 1 (latest)

split merlin
#

How to write less where clauses + lifetimes ?

urban willow
#

however, it will be simpler if you do the + only in the impls of the trait, not in a default method

#

in that case, every fn etc that is just calling ImportantTrait doesn't need the additional bounds

#

?play

use std::fmt::{Debug, Display};

pub trait ImportantTrait<S>: Sized + Clone + Copy + Debug + Default + Display {
    fn example(&self, other: &S) -> Self;
}

impl ImportantTrait<i32> for i32 {
    fn example(&self, other: &i32) -> Self {
        self + other
    }
}

pub trait SomeTrait<S, T: ImportantTrait<S>> {}

fn a<S, T: ImportantTrait<S>>(s: &S, t: &T) {
    dbg!(t.example(s));
}

pub fn main() {}
silver gobletBOT
urban willow
#

in general, if you find that you're writing bodies on all the methods of a trait declaration, you should be suspicious that this isn't a good use of a trait

#

most methods of most traits should only have code in the impl, not the trait