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 One
s or Two
es (whichever the Container
enum holds).
Attempts
I've tried implementing
IntoIter
for 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
PhantomData
to capture the type, and them implementIntoIter
for that. This also does not work since it does not return theItem
type (which is generic), but rather the particularOne
orTwo
. 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(), } } }