Skip to content

Commit 3fef221

Browse files
committed
Auto merge of #38134 - bluss:iter-nth, r=aturon
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.
2 parents 5f128ed + bc3618e commit 3fef221

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

src/liballoc/boxed.rs

+3
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,9 @@ impl<I: Iterator + ?Sized> Iterator for Box<I> {
524524
fn size_hint(&self) -> (usize, Option<usize>) {
525525
(**self).size_hint()
526526
}
527+
fn nth(&mut self, n: usize) -> Option<I::Item> {
528+
(**self).nth(n)
529+
}
527530
}
528531
#[stable(feature = "rust1", since = "1.0.0")]
529532
impl<I: DoubleEndedIterator + ?Sized> DoubleEndedIterator for Box<I> {

src/libcore/iter/iterator.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ pub trait Iterator {
247247
/// ```
248248
#[inline]
249249
#[stable(feature = "rust1", since = "1.0.0")]
250-
fn nth(&mut self, mut n: usize) -> Option<Self::Item> where Self: Sized {
250+
fn nth(&mut self, mut n: usize) -> Option<Self::Item> {
251251
for x in self {
252252
if n == 0 { return Some(x) }
253253
n -= 1;
@@ -2179,4 +2179,7 @@ impl<'a, I: Iterator + ?Sized> Iterator for &'a mut I {
21792179
type Item = I::Item;
21802180
fn next(&mut self) -> Option<I::Item> { (**self).next() }
21812181
fn size_hint(&self) -> (usize, Option<usize>) { (**self).size_hint() }
2182+
fn nth(&mut self, n: usize) -> Option<Self::Item> {
2183+
(**self).nth(n)
2184+
}
21822185
}

0 commit comments

Comments
 (0)