Closed
Description
Note that this is the current definition of IntoIterator:
pub trait IntoIterator {
type Item;
type IntoIter: Iterator<Item=Self::Item>;
fn into_iter(self) -> Self::IntoIter;
}
So we try to make a type that can be instantiated with any IntoIter, storing its Iterator:
pub struct Internal<I> {
iter: I
}
impl<I> Internal<I> {
fn new<It: IntoIterator<IntoIter=I>>(iterable: It) -> Self {
let iter = iterable.into_iter();
Internal { iter: iter }
}
}
<anon>:7:29: 7:40 error: the trait `core::iter::Iterator` is not implemented for the type `I` [E0277]
<anon>:7 let iter = iterable.into_iter();
^~~~~~~~~~~
<anon>:7:29: 7:40 note: `I` is not an iterator; maybe try calling `.iter()` or a similar method
<anon>:7 let iter = iterable.into_iter();
^~~~~~~~~~~
Completely nonsensical. Nothing is asking I to be an Iterator, except for IntoIterator's own definiton, which proves that it is in fact an Iterator. But ok, let's try to assert that it is:
pub struct Internal<I> {
iter: I
}
impl<I: Iterator> Internal<I> {
fn new<It: IntoIterator<IntoIter=I>>(iterable: It) -> Self {
let iter = iterable.into_iter();
Internal { iter: iter }
}
}
<anon>:7:29: 7:40 error: type mismatch resolving `<I as core::iter::Iterator>::Item == <It as core::iter::IntoIterator>::Item`:
expected trait `core::iter::Iterator`,
found trait `core::iter::IntoIterator` [E0271]
<anon>:7 let iter = iterable.into_iter();
^~~~~~~~~~~
Again nothing is demanding this by IntoIterator, which is already proving it. This works, though:
pub struct Internal<I> {
iter: I
}
impl<I: Iterator> Internal<I> {
fn new<It: IntoIterator<IntoIter=I, Item=I::Item>>(iterable: It) -> Self {
let iter = iterable.into_iter();
Internal { iter: iter }
}
}
Which shouldn't be necessary.