I'm trying to implement a trait on a structure containing a reference to a trait object. By bounding the trait object like below. Is there some way to achieve this?
trait Foo<T> {
fn foo(&self, t: T) -> u32;
}
trait Bar {
fn bar(self, n: u32) -> Self;
}
struct DoesFoo {
value: u32
}
struct Wrapper<'a, T> {
wrapped: &'a dyn Foo<T>,
}
impl Foo<u32> for DoesFoo {
fn foo(&self, t: u32) -> u32 {
self.value + t
}
}
impl<'a, T> Bar for Wrapper<'a, T>
where T: Bar,
{
fn bar(self, n: u32) -> Self {
self.wrapped.bar(n)
}
}
fn main() {
}
Errors:
Compiling playground v0.0.1 (/playground)
error[E0599]: no method named `bar` found for reference `&'a (dyn Foo<T> + 'a)` in the current scope
--> src/main.rs:27:22
|
27 | self.wrapped.bar(n)
| ^^^ method not found in `&'a (dyn Foo<T> + 'a)`
|
= help: items from traits can only be used if the trait is implemented and in scope
note: `Bar` defines an item `bar`, perhaps you need to implement it
--> src/main.rs:5:1
|
5 | trait Bar {
| ^^^^^^^^^
error: aborting due to previous error
For more information about this error, try `rustc --explain E0599`.
error: could not compile `playground`
To learn more, run the command again with --verbose.