I have a collection of struct objects. I'd like to iterate over the collection with an iterator of trait objects, but I can't create an appropriate iterator for that. My reduced test code is:
struct MyStruct {}
struct MyStorage(Vec<MyStruct>);
trait MyTrait {} // Dummy trait to demonstrate the problem
impl MyTrait for MyStruct {}
trait MyContainer {
fn items<'a>(&'a self) -> Box<Iterator<Item = &'a MyTrait> + 'a>;
}
impl MyContainer for MyStorage {
fn items<'a>(&'a self) -> Box<Iterator<Item = &'a MyTrait> + 'a> {
Box::new(self.0.iter())
}
}
This results the following compiler error:
error[E0271]: type mismatch resolving `<std::slice::Iter<'_, MyStruct> as std::iter::Iterator>::Item == &MyTrait`
--> src/main.rs:12:9
|
12 | Box::new(self.0.iter())
| ^^^^^^^^^^^^^^^^^^^^^^^ expected struct `MyStruct`, found trait MyTrait
|
= note: expected type `&MyStruct`
found type `&MyTrait`
= note: required for the cast to the object type `std::iter::Iterator<Item=&MyTrait>`
My understanding is that though &MyStruct
is normally convertible to &MyTrait
, the Iterator
implementation of the standard library doesn't allow it in this case.
Note that the same construct works with a Vec<Box<MyStruct>>
and Iterator<Item=&Box<MyTrait>>
, but boxing doesn't feel necessary here.
Is there any way to make this work with references?
You need to cast the single elements explicitly, like this:
Box::new(self.0.iter().map(|e| e as &MyTrait))
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