From d30f225b492163b14005d5069b7924f3fecf868c Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Tue, 3 Feb 2015 12:32:56 -0800 Subject: [PATCH] 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` 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] --- src/liballoc/boxed.rs | 32 +++++++++++------------ src/libcore/iter.rs | 61 ++++++++++++++----------------------------- 2 files changed, 34 insertions(+), 59 deletions(-) diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index 504b58d8ad1..340a8d59612 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -45,22 +45,18 @@ #![stable(feature = "rust1", since = "1.0.0")] +use core::prelude::*; + use core::any::Any; -use core::clone::Clone; -use core::cmp::{PartialEq, PartialOrd, Eq, Ord, Ordering}; +use core::cmp::Ordering; use core::default::Default; use core::error::{Error, FromError}; use core::fmt; use core::hash::{self, Hash}; -use core::iter::Iterator; -use core::marker::Sized; use core::mem; use core::ops::{Deref, DerefMut}; -use core::option::Option; use core::ptr::Unique; use core::raw::TraitObject; -use core::result::Result::{Ok, Err}; -use core::result::Result; /// A value that represents the heap. This is the default place that the `box` keyword allocates /// into when no place is supplied. @@ -296,18 +292,20 @@ impl DerefMut for Box { fn deref_mut(&mut self) -> &mut T { &mut **self } } -impl<'a, T> Iterator for Box + 'a> { - type Item = T; - - fn next(&mut self) -> Option { - (**self).next() - } - - fn size_hint(&self) -> (usize, Option) { - (**self).size_hint() - } +#[stable(feature = "rust1", since = "1.0.0")] +impl Iterator for Box { + type Item = I::Item; + fn next(&mut self) -> Option { (**self).next() } + fn size_hint(&self) -> (usize, Option) { (**self).size_hint() } } +#[stable(feature = "rust1", since = "1.0.0")] +impl DoubleEndedIterator for Box { + fn next_back(&mut self) -> Option { (**self).next_back() } +} +#[stable(feature = "rust1", since = "1.0.0")] +impl ExactSizeIterator for Box {} +#[stable(feature = "rust1", since = "1.0.0")] impl<'a, E: Error + 'a> FromError for Box { fn from_error(err: E) -> Box { Box::new(err) diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs index d0734f9c039..f676bb891b6 100644 --- a/src/libcore/iter.rs +++ b/src/libcore/iter.rs @@ -101,16 +101,11 @@ pub trait Iterator { fn size_hint(&self) -> (usize, Option) { (0, None) } } -impl<'a, T> Iterator for &'a mut (Iterator + 'a) { - type Item = T; - - fn next(&mut self) -> Option { - (**self).next() - } - - fn size_hint(&self) -> (usize, Option) { - (**self).size_hint() - } +#[stable(feature = "rust1", since = "1.0.0")] +impl<'a, I: Iterator + ?Sized> Iterator for &'a mut I { + type Item = I::Item; + fn next(&mut self) -> Option { (**self).next() } + fn size_hint(&self) -> (usize, Option) { (**self).size_hint() } } /// Conversion from an `Iterator` @@ -548,9 +543,7 @@ pub trait IteratorExt: Iterator + Sized { /// assert!(it.next() == Some(5)); /// ``` #[stable(feature = "rust1", since = "1.0.0")] - fn by_ref<'r>(&'r mut self) -> ByRef<'r, Self> { - ByRef{iter: self} - } + fn by_ref(&mut self) -> &mut Self { self } /// Loops through the entire iterator, collecting all of the elements into /// a container implementing `FromIterator`. @@ -1017,15 +1010,22 @@ impl IteratorExt for I where I: Iterator {} /// A range iterator able to yield elements from both ends /// -/// A `DoubleEndedIterator` can be thought of as a deque in that `next()` and `next_back()` exhaust -/// elements from the *same* range, and do not work independently of each other. +/// A `DoubleEndedIterator` can be thought of as a deque in that `next()` and +/// `next_back()` exhaust elements from the *same* range, and do not work +/// independently of each other. #[stable(feature = "rust1", since = "1.0.0")] pub trait DoubleEndedIterator: Iterator { - /// Yield an element from the end of the range, returning `None` if the range is empty. + /// Yield an element from the end of the range, returning `None` if the + /// range is empty. #[stable(feature = "rust1", since = "1.0.0")] fn next_back(&mut self) -> Option; } +#[stable(feature = "rust1", since = "1.0.0")] +impl<'a, I: DoubleEndedIterator + ?Sized> DoubleEndedIterator for &'a mut I { + fn next_back(&mut self) -> Option { (**self).next_back() } +} + /// An object implementing random access indexing by `usize` /// /// A `RandomAccessIterator` should be either infinite or a `DoubleEndedIterator`. @@ -1065,6 +1065,9 @@ pub trait ExactSizeIterator: Iterator { } } +#[stable(feature = "rust1", since = "1.0.0")] +impl<'a, I: ExactSizeIterator + ?Sized> ExactSizeIterator for &'a mut I {} + // All adaptors that preserve the size of the wrapped iterator are fine // Adaptors that may overflow in `size_hint` are not, i.e. `Chain`. #[stable(feature = "rust1", since = "1.0.0")] @@ -1117,32 +1120,6 @@ impl RandomAccessIterator for Rev where I: DoubleEndedIterator + RandomAcc } } -/// A mutable reference to an iterator -#[must_use = "iterator adaptors are lazy and do nothing unless consumed"] -#[stable(feature = "rust1", since = "1.0.0")] -pub struct ByRef<'a, I:'a> { - iter: &'a mut I, -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl<'a, I> Iterator for ByRef<'a, I> where I: 'a + Iterator { - type Item = ::Item; - - #[inline] - fn next(&mut self) -> Option<::Item> { self.iter.next() } - #[inline] - fn size_hint(&self) -> (usize, Option) { self.iter.size_hint() } -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl<'a, I> DoubleEndedIterator for ByRef<'a, I> where I: 'a + DoubleEndedIterator { - #[inline] - fn next_back(&mut self) -> Option<::Item> { self.iter.next_back() } -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl<'a, I> ExactSizeIterator for ByRef<'a, I> where I: 'a + ExactSizeIterator {} - /// A trait for iterators over elements which can be added together #[unstable(feature = "core", reason = "needs to be re-evaluated as part of numerics reform")]