Skip to content

Commit 9db593c

Browse files
committed
rollup merge of #21907: alexcrichton/iter-by-ref
This removes the `ByRef` iterator adaptor to stay in line with the changes to `std::io`. The `by_ref` method instead just returns `&mut Self`. This also removes the implementation of `Iterator for &mut Iterator` and instead generalizes it to `Iterator for &mut I` where `I: Iterator + ?Sized`. The `Box<I>` implementations were also updated.
2 parents 74f7e06 + d30f225 commit 9db593c

File tree

2 files changed

+34
-59
lines changed

2 files changed

+34
-59
lines changed

src/liballoc/boxed.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,18 @@
4545
4646
#![stable(feature = "rust1", since = "1.0.0")]
4747

48+
use core::prelude::*;
49+
4850
use core::any::Any;
49-
use core::clone::Clone;
50-
use core::cmp::{PartialEq, PartialOrd, Eq, Ord, Ordering};
51+
use core::cmp::Ordering;
5152
use core::default::Default;
5253
use core::error::{Error, FromError};
5354
use core::fmt;
5455
use core::hash::{self, Hash};
55-
use core::iter::Iterator;
56-
use core::marker::Sized;
5756
use core::mem;
5857
use core::ops::{Deref, DerefMut};
59-
use core::option::Option;
6058
use core::ptr::Unique;
6159
use core::raw::TraitObject;
62-
use core::result::Result::{Ok, Err};
63-
use core::result::Result;
6460

6561
/// A value that represents the heap. This is the default place that the `box` keyword allocates
6662
/// into when no place is supplied.
@@ -296,18 +292,20 @@ impl<T: ?Sized> DerefMut for Box<T> {
296292
fn deref_mut(&mut self) -> &mut T { &mut **self }
297293
}
298294

299-
impl<'a, T> Iterator for Box<Iterator<Item=T> + 'a> {
300-
type Item = T;
301-
302-
fn next(&mut self) -> Option<T> {
303-
(**self).next()
304-
}
305-
306-
fn size_hint(&self) -> (usize, Option<usize>) {
307-
(**self).size_hint()
308-
}
295+
#[stable(feature = "rust1", since = "1.0.0")]
296+
impl<I: Iterator + ?Sized> Iterator for Box<I> {
297+
type Item = I::Item;
298+
fn next(&mut self) -> Option<I::Item> { (**self).next() }
299+
fn size_hint(&self) -> (usize, Option<usize>) { (**self).size_hint() }
300+
}
301+
#[stable(feature = "rust1", since = "1.0.0")]
302+
impl<I: DoubleEndedIterator + ?Sized> DoubleEndedIterator for Box<I> {
303+
fn next_back(&mut self) -> Option<I::Item> { (**self).next_back() }
309304
}
305+
#[stable(feature = "rust1", since = "1.0.0")]
306+
impl<I: ExactSizeIterator + ?Sized> ExactSizeIterator for Box<I> {}
310307

308+
#[stable(feature = "rust1", since = "1.0.0")]
311309
impl<'a, E: Error + 'a> FromError<E> for Box<Error + 'a> {
312310
fn from_error(err: E) -> Box<Error + 'a> {
313311
Box::new(err)

src/libcore/iter.rs

Lines changed: 19 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,11 @@ pub trait Iterator {
101101
fn size_hint(&self) -> (usize, Option<usize>) { (0, None) }
102102
}
103103

104-
impl<'a, T> Iterator for &'a mut (Iterator<Item=T> + 'a) {
105-
type Item = T;
106-
107-
fn next(&mut self) -> Option<T> {
108-
(**self).next()
109-
}
110-
111-
fn size_hint(&self) -> (usize, Option<usize>) {
112-
(**self).size_hint()
113-
}
104+
#[stable(feature = "rust1", since = "1.0.0")]
105+
impl<'a, I: Iterator + ?Sized> Iterator for &'a mut I {
106+
type Item = I::Item;
107+
fn next(&mut self) -> Option<I::Item> { (**self).next() }
108+
fn size_hint(&self) -> (usize, Option<usize>) { (**self).size_hint() }
114109
}
115110

116111
/// Conversion from an `Iterator`
@@ -549,9 +544,7 @@ pub trait IteratorExt: Iterator + Sized {
549544
/// assert!(it.next() == Some(5));
550545
/// ```
551546
#[stable(feature = "rust1", since = "1.0.0")]
552-
fn by_ref<'r>(&'r mut self) -> ByRef<'r, Self> {
553-
ByRef{iter: self}
554-
}
547+
fn by_ref(&mut self) -> &mut Self { self }
555548

556549
/// Loops through the entire iterator, collecting all of the elements into
557550
/// a container implementing `FromIterator`.
@@ -1019,15 +1012,22 @@ impl<I> IteratorExt for I where I: Iterator {}
10191012

10201013
/// A range iterator able to yield elements from both ends
10211014
///
1022-
/// A `DoubleEndedIterator` can be thought of as a deque in that `next()` and `next_back()` exhaust
1023-
/// elements from the *same* range, and do not work independently of each other.
1015+
/// A `DoubleEndedIterator` can be thought of as a deque in that `next()` and
1016+
/// `next_back()` exhaust elements from the *same* range, and do not work
1017+
/// independently of each other.
10241018
#[stable(feature = "rust1", since = "1.0.0")]
10251019
pub trait DoubleEndedIterator: Iterator {
1026-
/// Yield an element from the end of the range, returning `None` if the range is empty.
1020+
/// Yield an element from the end of the range, returning `None` if the
1021+
/// range is empty.
10271022
#[stable(feature = "rust1", since = "1.0.0")]
10281023
fn next_back(&mut self) -> Option<Self::Item>;
10291024
}
10301025

1026+
#[stable(feature = "rust1", since = "1.0.0")]
1027+
impl<'a, I: DoubleEndedIterator + ?Sized> DoubleEndedIterator for &'a mut I {
1028+
fn next_back(&mut self) -> Option<I::Item> { (**self).next_back() }
1029+
}
1030+
10311031
/// An object implementing random access indexing by `usize`
10321032
///
10331033
/// A `RandomAccessIterator` should be either infinite or a `DoubleEndedIterator`.
@@ -1067,6 +1067,9 @@ pub trait ExactSizeIterator: Iterator {
10671067
}
10681068
}
10691069

1070+
#[stable(feature = "rust1", since = "1.0.0")]
1071+
impl<'a, I: ExactSizeIterator + ?Sized> ExactSizeIterator for &'a mut I {}
1072+
10701073
// All adaptors that preserve the size of the wrapped iterator are fine
10711074
// Adaptors that may overflow in `size_hint` are not, i.e. `Chain`.
10721075
#[stable(feature = "rust1", since = "1.0.0")]
@@ -1119,32 +1122,6 @@ impl<I> RandomAccessIterator for Rev<I> where I: DoubleEndedIterator + RandomAcc
11191122
}
11201123
}
11211124

1122-
/// A mutable reference to an iterator
1123-
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
1124-
#[stable(feature = "rust1", since = "1.0.0")]
1125-
pub struct ByRef<'a, I:'a> {
1126-
iter: &'a mut I,
1127-
}
1128-
1129-
#[stable(feature = "rust1", since = "1.0.0")]
1130-
impl<'a, I> Iterator for ByRef<'a, I> where I: 'a + Iterator {
1131-
type Item = <I as Iterator>::Item;
1132-
1133-
#[inline]
1134-
fn next(&mut self) -> Option<<I as Iterator>::Item> { self.iter.next() }
1135-
#[inline]
1136-
fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() }
1137-
}
1138-
1139-
#[stable(feature = "rust1", since = "1.0.0")]
1140-
impl<'a, I> DoubleEndedIterator for ByRef<'a, I> where I: 'a + DoubleEndedIterator {
1141-
#[inline]
1142-
fn next_back(&mut self) -> Option<<I as Iterator>::Item> { self.iter.next_back() }
1143-
}
1144-
1145-
#[stable(feature = "rust1", since = "1.0.0")]
1146-
impl<'a, I> ExactSizeIterator for ByRef<'a, I> where I: 'a + ExactSizeIterator {}
1147-
11481125
/// A trait for iterators over elements which can be added together
11491126
#[unstable(feature = "core",
11501127
reason = "needs to be re-evaluated as part of numerics reform")]

0 commit comments

Comments
 (0)