1
Fork 0

Stabilize Index traits and most range notation

This commit marks as `#[stable]`:

* The `Index` and `IndexMut` traits. These are stabilized as taking the
  index itself *by reference*; after extensive discussion it was
  determined that this is a better match with our choices
  elsewhere (e.g. making comparison operators auto-reference), and that
  the use cases for by-value indices are better handled through
  `IndexSet`.

* The `Range`, `RangeFrom` and `RangeTo` structs, introduced for range
  notation.

* Various impls of `Index` and `IndexMut`.

The `FullRange` struct is left unstable as we may wish to rename it to
`RangeFull` in the future.

This commit also *removes* the `Step` trait in favor of direct
implementation of iterator traits on ranges for integers. The `Step`
trait was not a terribly useful factoring internally, and it is likely
that external integer types are best off implementing range iterators
directly. It was removed to simplify the API surface. We can always
reintroduce `Step` later if it turns out to be useful.

Due to this removal, this is a:

[breaking-change]
This commit is contained in:
Aaron Turon 2015-01-15 20:00:09 -08:00
parent 6869645e86
commit fba0bf63a9
3 changed files with 88 additions and 101 deletions

View file

@ -1229,7 +1229,7 @@ impl<S: hash::Writer + hash::Hasher, T: Hash<S>> Hash<S> for Vec<T> {
} }
} }
#[unstable = "waiting on Index stability"] #[stable]
impl<T> Index<uint> for Vec<T> { impl<T> Index<uint> for Vec<T> {
type Output = T; type Output = T;
@ -1239,6 +1239,7 @@ impl<T> Index<uint> for Vec<T> {
} }
} }
#[stable]
impl<T> IndexMut<uint> for Vec<T> { impl<T> IndexMut<uint> for Vec<T> {
type Output = T; type Output = T;
@ -1249,6 +1250,7 @@ impl<T> IndexMut<uint> for Vec<T> {
} }
#[stable]
impl<T> ops::Index<ops::Range<uint>> for Vec<T> { impl<T> ops::Index<ops::Range<uint>> for Vec<T> {
type Output = [T]; type Output = [T];
#[inline] #[inline]
@ -1256,6 +1258,7 @@ impl<T> ops::Index<ops::Range<uint>> for Vec<T> {
self.as_slice().index(index) self.as_slice().index(index)
} }
} }
#[stable]
impl<T> ops::Index<ops::RangeTo<uint>> for Vec<T> { impl<T> ops::Index<ops::RangeTo<uint>> for Vec<T> {
type Output = [T]; type Output = [T];
#[inline] #[inline]
@ -1263,6 +1266,7 @@ impl<T> ops::Index<ops::RangeTo<uint>> for Vec<T> {
self.as_slice().index(index) self.as_slice().index(index)
} }
} }
#[stable]
impl<T> ops::Index<ops::RangeFrom<uint>> for Vec<T> { impl<T> ops::Index<ops::RangeFrom<uint>> for Vec<T> {
type Output = [T]; type Output = [T];
#[inline] #[inline]
@ -1270,6 +1274,7 @@ impl<T> ops::Index<ops::RangeFrom<uint>> for Vec<T> {
self.as_slice().index(index) self.as_slice().index(index)
} }
} }
#[stable]
impl<T> ops::Index<ops::FullRange> for Vec<T> { impl<T> ops::Index<ops::FullRange> for Vec<T> {
type Output = [T]; type Output = [T];
#[inline] #[inline]
@ -1278,6 +1283,7 @@ impl<T> ops::Index<ops::FullRange> for Vec<T> {
} }
} }
#[stable]
impl<T> ops::IndexMut<ops::Range<uint>> for Vec<T> { impl<T> ops::IndexMut<ops::Range<uint>> for Vec<T> {
type Output = [T]; type Output = [T];
#[inline] #[inline]
@ -1285,6 +1291,7 @@ impl<T> ops::IndexMut<ops::Range<uint>> for Vec<T> {
self.as_mut_slice().index_mut(index) self.as_mut_slice().index_mut(index)
} }
} }
#[stable]
impl<T> ops::IndexMut<ops::RangeTo<uint>> for Vec<T> { impl<T> ops::IndexMut<ops::RangeTo<uint>> for Vec<T> {
type Output = [T]; type Output = [T];
#[inline] #[inline]
@ -1292,6 +1299,7 @@ impl<T> ops::IndexMut<ops::RangeTo<uint>> for Vec<T> {
self.as_mut_slice().index_mut(index) self.as_mut_slice().index_mut(index)
} }
} }
#[stable]
impl<T> ops::IndexMut<ops::RangeFrom<uint>> for Vec<T> { impl<T> ops::IndexMut<ops::RangeFrom<uint>> for Vec<T> {
type Output = [T]; type Output = [T];
#[inline] #[inline]
@ -1299,6 +1307,7 @@ impl<T> ops::IndexMut<ops::RangeFrom<uint>> for Vec<T> {
self.as_mut_slice().index_mut(index) self.as_mut_slice().index_mut(index)
} }
} }
#[stable]
impl<T> ops::IndexMut<ops::FullRange> for Vec<T> { impl<T> ops::IndexMut<ops::FullRange> for Vec<T> {
type Output = [T]; type Output = [T];
#[inline] #[inline]
@ -1307,7 +1316,6 @@ impl<T> ops::IndexMut<ops::FullRange> for Vec<T> {
} }
} }
#[stable] #[stable]
impl<T> ops::Deref for Vec<T> { impl<T> ops::Deref for Vec<T> {
type Target = [T]; type Target = [T];

View file

@ -2701,63 +2701,93 @@ impl<A: Int> Iterator for RangeStepInclusive<A> {
} }
} }
macro_rules! range_impl {
($($t:ty)*) => ($(
#[stable]
impl Iterator for ::ops::Range<$t> {
type Item = $t;
/// The `Step` trait identifies objects which can be stepped over in both #[inline]
/// directions. The `steps_between` function provides a way to fn next(&mut self) -> Option<$t> {
/// compare two Step objects (it could be provided using `step()` and `Ord`, if self.start < self.end {
/// but the implementation would be so inefficient as to be useless). let result = self.start;
#[unstable = "design of range notation/iteration is in flux"] self.start += 1;
pub trait Step: Ord { return Some(result);
/// Change self to the next object.
fn step(&mut self);
/// Change self to the previous object.
fn step_back(&mut self);
/// The steps_between two step objects.
/// start should always be less than end, so the result should never be negative.
/// Return None if it is not possible to calculate steps_between without
/// overflow.
fn steps_between(start: &Self, end: &Self) -> Option<uint>;
} }
macro_rules! step_impl { return None;
}
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
debug_assert!(self.end >= self.start);
let hint = (self.end - self.start) as uint;
(hint, Some(hint))
}
}
#[stable]
impl ExactSizeIterator for ::ops::Range<$t> {}
)*)
}
macro_rules! range_impl_no_hint {
($($t:ty)*) => ($( ($($t:ty)*) => ($(
#[unstable = "Trait is unstable."] #[stable]
impl Step for $t { impl Iterator for ::ops::Range<$t> {
type Item = $t;
#[inline] #[inline]
fn step(&mut self) { *self += 1; } fn next(&mut self) -> Option<$t> {
#[inline] if self.start < self.end {
fn step_back(&mut self) { *self -= 1; } let result = self.start;
#[inline] self.start += 1;
fn steps_between(start: &$t, end: &$t) -> Option<uint> { return Some(result);
debug_assert!(end >= start); }
Some((*end - *start) as uint)
return None;
} }
} }
)*) )*)
} }
macro_rules! step_impl_no_between { macro_rules! range_other_impls {
($($t:ty)*) => ($( ($($t:ty)*) => ($(
#[unstable = "Trait is unstable."] #[stable]
impl Step for $t { impl DoubleEndedIterator for ::ops::Range<$t> {
#[inline] #[inline]
fn step(&mut self) { *self += 1; } fn next_back(&mut self) -> Option<$t> {
if self.start < self.end {
self.end -= 1;
return Some(self.end);
}
return None;
}
}
#[stable]
impl Iterator for ::ops::RangeFrom<$t> {
type Item = $t;
#[inline] #[inline]
fn step_back(&mut self) { *self -= 1; } fn next(&mut self) -> Option<$t> {
#[inline] let result = self.start;
fn steps_between(_start: &$t, _end: &$t) -> Option<uint> { self.start += 1;
None debug_assert!(result < self.start);
return Some(result);
} }
} }
)*) )*)
} }
step_impl!(uint u8 u16 u32 int i8 i16 i32); range_impl!(uint u8 u16 u32 int i8 i16 i32);
#[cfg(target_pointer_width = "64")] #[cfg(target_pointer_width = "64")]
step_impl!(u64 i64); range_impl!(u64 i64);
#[cfg(target_pointer_width = "32")] #[cfg(target_pointer_width = "32")]
step_impl_no_between!(u64 i64); range_impl_no_hint!(u64 i64);
range_other_impls!(uint u8 u16 u32 u64 int i8 i16 i32 i64);
/// An iterator that repeats an element endlessly /// An iterator that repeats an element endlessly
#[derive(Clone)] #[derive(Clone)]

View file

@ -69,10 +69,7 @@
#![stable] #![stable]
use clone::Clone;
use iter::{Step, Iterator,DoubleEndedIterator,ExactSizeIterator};
use marker::Sized; use marker::Sized;
use option::Option::{self, Some, None};
use fmt; use fmt;
/// The `Drop` trait is used to run some code when a value goes out of scope. This /// The `Drop` trait is used to run some code when a value goes out of scope. This
@ -924,10 +921,12 @@ shr_impl_all! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize }
/// } /// }
/// ``` /// ```
#[lang="index"] #[lang="index"]
#[stable]
pub trait Index<Index: ?Sized> { pub trait Index<Index: ?Sized> {
type Output: ?Sized; type Output: ?Sized;
/// The method for the indexing (`Foo[Bar]`) operation /// The method for the indexing (`Foo[Bar]`) operation
#[stable]
fn index<'a>(&'a self, index: &Index) -> &'a Self::Output; fn index<'a>(&'a self, index: &Index) -> &'a Self::Output;
} }
@ -960,20 +959,22 @@ pub trait Index<Index: ?Sized> {
/// } /// }
/// ``` /// ```
#[lang="index_mut"] #[lang="index_mut"]
#[stable]
pub trait IndexMut<Index: ?Sized> { pub trait IndexMut<Index: ?Sized> {
type Output: ?Sized; type Output: ?Sized;
/// The method for the indexing (`Foo[Bar]`) operation /// The method for the indexing (`Foo[Bar]`) operation
#[stable]
fn index_mut<'a>(&'a mut self, index: &Index) -> &'a mut Self::Output; fn index_mut<'a>(&'a mut self, index: &Index) -> &'a mut Self::Output;
} }
/// An unbounded range. /// An unbounded range.
#[derive(Copy, Clone, PartialEq, Eq)] #[derive(Copy, Clone, PartialEq, Eq)]
#[lang="full_range"] #[lang="full_range"]
#[unstable = "API still in development"] #[unstable = "may be renamed to RangeFull"]
pub struct FullRange; pub struct FullRange;
#[unstable = "API still in development"] #[stable]
impl fmt::Show for FullRange { impl fmt::Show for FullRange {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt::Show::fmt("..", fmt) fmt::Show::fmt("..", fmt)
@ -983,7 +984,7 @@ impl fmt::Show for FullRange {
/// A (half-open) range which is bounded at both ends. /// A (half-open) range which is bounded at both ends.
#[derive(Copy, Clone, PartialEq, Eq)] #[derive(Copy, Clone, PartialEq, Eq)]
#[lang="range"] #[lang="range"]
#[unstable = "API still in development"] #[stable]
pub struct Range<Idx> { pub struct Range<Idx> {
/// The lower bound of the range (inclusive). /// The lower bound of the range (inclusive).
pub start: Idx, pub start: Idx,
@ -991,48 +992,7 @@ pub struct Range<Idx> {
pub end: Idx, pub end: Idx,
} }
#[unstable = "API still in development"] #[stable]
impl<Idx: Clone + Step> Iterator for Range<Idx> {
type Item = Idx;
#[inline]
fn next(&mut self) -> Option<Idx> {
if self.start < self.end {
let result = self.start.clone();
self.start.step();
return Some(result);
}
return None;
}
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
if let Some(hint) = Step::steps_between(&self.start, &self.end) {
(hint, Some(hint))
} else {
(0, None)
}
}
}
#[unstable = "API still in development"]
impl<Idx: Clone + Step> DoubleEndedIterator for Range<Idx> {
#[inline]
fn next_back(&mut self) -> Option<Idx> {
if self.start < self.end {
self.end.step_back();
return Some(self.end.clone());
}
return None;
}
}
#[unstable = "API still in development"]
impl<Idx: Clone + Step> ExactSizeIterator for Range<Idx> {}
#[unstable = "API still in development"]
impl<Idx: fmt::Show> fmt::Show for Range<Idx> { impl<Idx: fmt::Show> fmt::Show for Range<Idx> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "{:?}..{:?}", self.start, self.end) write!(fmt, "{:?}..{:?}", self.start, self.end)
@ -1042,26 +1002,15 @@ impl<Idx: fmt::Show> fmt::Show for Range<Idx> {
/// A range which is only bounded below. /// A range which is only bounded below.
#[derive(Copy, Clone, PartialEq, Eq)] #[derive(Copy, Clone, PartialEq, Eq)]
#[lang="range_from"] #[lang="range_from"]
#[unstable = "API still in development"] #[stable]
pub struct RangeFrom<Idx> { pub struct RangeFrom<Idx> {
/// The lower bound of the range (inclusive). /// The lower bound of the range (inclusive).
pub start: Idx, pub start: Idx,
} }
#[unstable = "API still in development"]
impl<Idx: Clone + Step> Iterator for RangeFrom<Idx> {
type Item = Idx;
#[inline]
fn next(&mut self) -> Option<Idx> {
// Deliberately overflow so we loop forever.
let result = self.start.clone();
self.start.step();
return Some(result);
}
}
#[unstable = "API still in development"] #[stable]
impl<Idx: fmt::Show> fmt::Show for RangeFrom<Idx> { impl<Idx: fmt::Show> fmt::Show for RangeFrom<Idx> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "{:?}..", self.start) write!(fmt, "{:?}..", self.start)
@ -1071,13 +1020,13 @@ impl<Idx: fmt::Show> fmt::Show for RangeFrom<Idx> {
/// A range which is only bounded above. /// A range which is only bounded above.
#[derive(Copy, Clone, PartialEq, Eq)] #[derive(Copy, Clone, PartialEq, Eq)]
#[lang="range_to"] #[lang="range_to"]
#[unstable = "API still in development"] #[stable]
pub struct RangeTo<Idx> { pub struct RangeTo<Idx> {
/// The upper bound of the range (exclusive). /// The upper bound of the range (exclusive).
pub end: Idx, pub end: Idx,
} }
#[unstable = "API still in development"] #[stable]
impl<Idx: fmt::Show> fmt::Show for RangeTo<Idx> { impl<Idx: fmt::Show> fmt::Show for RangeTo<Idx> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "..{:?}", self.end) write!(fmt, "..{:?}", self.end)