I have the following structure:
trait DoesIt: Sized {
fn do_it(self) -> usize {
// Only adding default impl for this example,
// normally these are different for the One and Two below;
3
}
}
struct One {}
struct Two {}
impl DoesIt for One {}
impl DoesIt for Two {}
enum Container {
Ones(Vec<One>),
Twos(Vec<Two>),
}
trait HasContainer {
fn get(self) -> Container;
}
struct Outer {
container: Container,
}
impl HasContainer for Outer {
fn get(self) -> Container {
self.container
}
}
I would like to have a trait Works, which, if implemented, would allow iterating over things that implement DoesIt - in this case Ones or Twoes (whichever the Container enum holds).
Attempts
I've tried implementing
IntoIterfor theOuter, but due to rustc --explain E0207 this does not work, since I cannot bind theIntoIter<Item = T>to some trait.Creating a wrapping generic struct which also holds a
PhantomDatato capture the type, and them implementIntoIterfor that. This also does not work since it does not return theItemtype (which is generic), but rather the particularOneorTwo. As of the following does not work, but I'd like it to:struct PhantomContainer<T: DoesIt, S> { a_type: PhantomData<S>, pub it: T, } impl<T, I> for PhancomContainer<T, I> where I: DoesIt, T: HasContainer, { type Item = I; type IntoIterator = std::vec::IntoIter<Self::Item>; fn into_iter(self) -> Self::IntoIter { match self.it.get() { Container::Ones(ones) => (ones as Vec<I>).into_iter(), Container::Twos(twoes) => (twoes as Vec<I>).into_iter(), } } }