Skip to content

Commit

Permalink
Auto merge of #38134 - bluss:iter-nth, r=aturon
Browse files Browse the repository at this point in the history
Remove Self: Sized from Iterator::nth

It is an unnecessary restriction; nth neither needs self to be sized
nor needs to be exempted from the trait object.

It increases the utility of the nth method, because type specific
implementations are available through `&mut I` or through an iterator
trait object.

It is a backwards compatible change due to the special cases of the
`where Self: Sized` bound; it was already optional to include this bound
in `Iterator` implementations.
  • Loading branch information
bors committed Dec 7, 2016
2 parents 5f128ed + bc3618e commit 3fef221
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/liballoc/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,9 @@ impl<I: Iterator + ?Sized> Iterator for Box<I> {
fn size_hint(&self) -> (usize, Option<usize>) {
(**self).size_hint()
}
fn nth(&mut self, n: usize) -> Option<I::Item> {
(**self).nth(n)
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<I: DoubleEndedIterator + ?Sized> DoubleEndedIterator for Box<I> {
Expand Down
5 changes: 4 additions & 1 deletion src/libcore/iter/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ pub trait Iterator {
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
fn nth(&mut self, mut n: usize) -> Option<Self::Item> where Self: Sized {
fn nth(&mut self, mut n: usize) -> Option<Self::Item> {
for x in self {
if n == 0 { return Some(x) }
n -= 1;
Expand Down Expand Up @@ -2179,4 +2179,7 @@ impl<'a, I: Iterator + ?Sized> Iterator for &'a mut I {
type Item = I::Item;
fn next(&mut self) -> Option<I::Item> { (**self).next() }
fn size_hint(&self) -> (usize, Option<usize>) { (**self).size_hint() }
fn nth(&mut self, n: usize) -> Option<Self::Item> {
(**self).nth(n)
}
}

0 comments on commit 3fef221

Please sign in to comment.