Does a trait exist for generic code for references and values without having to write it all out twice?

Viewed 41

I have a trait implemented for iterators over &u64, but sometimes I want to use it with an iterator that produces u64. I'm wondering if there's a way to implement my trait for both T and &T without having to write it all out twice. I think it is equivalent to this problem:

pub fn foo<T: RefOrValue<i32>>(t: T) {
    let x: i32 = t.to_value();
    // ...
}

pub fn main() {
    foo(1i32);
    foo(&2i32);
}

Does a trait like RefOrValue exist?

0 Answers
Related