Skip to content

Commit d30f225

Browse files
committed
std: Remove iter::ByRef and generalize impls
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. This is a breaking change due to the removal of the `std::iter::ByRef` type. All mentions of `ByRef<'a, T>` should be replaced with `&mut T` to migrate forward. [breaking-change]
1 parent 3b2ed14 commit d30f225

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`
@@ -548,9 +543,7 @@ pub trait IteratorExt: Iterator + Sized {
548543
/// assert!(it.next() == Some(5));
549544
/// ```
550545
#[stable(feature = "rust1", since = "1.0.0")]
551-
fn by_ref<'r>(&'r mut self) -> ByRef<'r, Self> {
552-
ByRef{iter: self}
553-
}
546+
fn by_ref(&mut self) -> &mut Self { self }
554547

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

10181011
/// A range iterator able to yield elements from both ends
10191012
///
1020-
/// A `DoubleEndedIterator` can be thought of as a deque in that `next()` and `next_back()` exhaust
1021-
/// elements from the *same* range, and do not work independently of each other.
1013+
/// A `DoubleEndedIterator` can be thought of as a deque in that `next()` and
1014+
/// `next_back()` exhaust elements from the *same* range, and do not work
1015+
/// independently of each other.
10221016
#[stable(feature = "rust1", since = "1.0.0")]
10231017
pub trait DoubleEndedIterator: Iterator {
1024-
/// Yield an element from the end of the range, returning `None` if the range is empty.
1018+
/// Yield an element from the end of the range, returning `None` if the
1019+
/// range is empty.
10251020
#[stable(feature = "rust1", since = "1.0.0")]
10261021
fn next_back(&mut self) -> Option<Self::Item>;
10271022
}
10281023

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

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

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

0 commit comments

Comments
 (0)