Do I have to implement a trait twice when implementing it for both reference and non-reference types?
This is a good example for the Borrow
trait.
use std::borrow::Borrow;
struct Bar;
trait Foo {
fn hi(&self);
}
impl<B: Borrow<Bar>> Foo for B {
fn hi(&self) {
print!("hi")
}
}
fn main() {
let bar = Bar;
(&bar).hi();
&bar.hi();
}
No, you do not have to duplicate code. Instead, you can delegate:
impl Foo for &'_ Bar {
fn hi(&self) {
(**self).hi()
}
}
I would go one step further and implement the trait for all references to types that implement the trait:
impl<T: Foo> Foo for &'_ T {
fn hi(&self) {
(**self).hi()
}
}
See also:
- When should I not implement a trait for references to implementors of that trait?
- Implementing a trait for reference and non reference types causes conflicting implementations
&bar.hi();
This code is equivalent to &(bar.hi())
and probably not what you intended.
See also:
- Why is it legal to borrow a temporary?