I want to make a struct that wraps another type but can take both owned and borrowed version of values that implement a given trait.
For example, let's say I have the trait Foobar:
trait Foobar {
fn foobar(&self);
}
I now want to make a struct that wraps values or borrowed references of this trait:
struct FoobarWrapper<T: Foobar> {
wrapped: T,
extra_stuff: Stuff
}
Here, I want FoobarWrapper to work with both Baz and &Baz given that impl Foobar for Baz.
I have come up with one solution that might work but I don't know if it's idiomatic and that's to simply do:
impl<'a, T: Foobar> Foobar for &'a T {
fn foobar(&self) {
(*self).foobar()
}
}
If I'm not mistaken, this makes any reference to a value that implements Foobar also an implementor of Foobar. But is this the way you're supposed to do it?
Yes, your solution is probably fine if you can support it. Iterator does the same thing with
impl<'a, I> Iterator for &'a mut I where I: Iterator + ?Sized
You should probably also add the ?Sized bound too, for flexibility.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With