Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate over a collection of structs as an iterator of trait object references?

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?

like image 330
Zólyomi István Avatar asked Nov 07 '17 10:11

Zólyomi István


1 Answers

You need to cast the single elements explicitly, like this:

Box::new(self.0.iter().map(|e| e as &MyTrait))
like image 102
Stefan Avatar answered Oct 17 '22 22:10

Stefan