4

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?

Timmmm
  • 88,195
  • 71
  • 364
  • 509
  • 1
    The specific example doesn't really call for a generic type -- I gave justifications for that in my answer to [Opposite of Borrow trait for a Copy type in Rust?](/q/63465672/3650362) But in the general case where the type is non-`Copy` and the function actually only needs a reference, `Borrow` is the way to go. – trent Sep 30 '20 at 14:11

0 Answers0