diff options
| author | bors <bors@rust-lang.org> | 2017-07-01 11:21:53 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2017-07-01 11:21:53 +0000 |
| commit | a5d34e1d035e5441fb5b3d56d5205d038538f053 (patch) | |
| tree | 7332309577b58d4917a07e9b2ea8d8d5c4b43cc0 /src/liballoc | |
| parent | 7a2c09b6f5282335fc0000f5504fbed2f70e89c8 (diff) | |
| parent | 0a9c13624d2fede5c6ce8e5aa7f486403098bde6 (diff) | |
| download | rust-a5d34e1d035e5441fb5b3d56d5205d038538f053.tar.gz rust-a5d34e1d035e5441fb5b3d56d5205d038538f053.zip | |
Auto merge of #42991 - sfackler:unstable-rangeargument, r=alexcrichton
Revert "Stabilize RangeArgument" This reverts commit 143206d54d7558c2326212df99efc98110904fdb. From the discussion in #30877 it seems like this is premature.
Diffstat (limited to 'src/liballoc')
| -rw-r--r-- | src/liballoc/btree/map.rs | 5 | ||||
| -rw-r--r-- | src/liballoc/btree/set.rs | 3 | ||||
| -rw-r--r-- | src/liballoc/lib.rs | 51 | ||||
| -rw-r--r-- | src/liballoc/range.rs | 138 | ||||
| -rw-r--r-- | src/liballoc/string.rs | 5 | ||||
| -rw-r--r-- | src/liballoc/vec.rs | 5 | ||||
| -rw-r--r-- | src/liballoc/vec_deque.rs | 5 |
7 files changed, 200 insertions, 12 deletions
diff --git a/src/liballoc/btree/map.rs b/src/liballoc/btree/map.rs index 5243fb6ae0e..a51c70159db 100644 --- a/src/liballoc/btree/map.rs +++ b/src/liballoc/btree/map.rs @@ -13,11 +13,12 @@ use core::fmt::Debug; use core::hash::{Hash, Hasher}; use core::iter::{FromIterator, Peekable, FusedIterator}; use core::marker::PhantomData; -use core::ops::{Index, RangeArgument}; -use core::ops::Bound::{Excluded, Included, Unbounded}; +use core::ops::Index; use core::{fmt, intrinsics, mem, ptr}; use borrow::Borrow; +use Bound::{Excluded, Included, Unbounded}; +use range::RangeArgument; use super::node::{self, Handle, NodeRef, marker}; use super::search; diff --git a/src/liballoc/btree/set.rs b/src/liballoc/btree/set.rs index c755d2d8b85..d32460da939 100644 --- a/src/liballoc/btree/set.rs +++ b/src/liballoc/btree/set.rs @@ -16,11 +16,12 @@ use core::cmp::{min, max}; use core::fmt::Debug; use core::fmt; use core::iter::{Peekable, FromIterator, FusedIterator}; -use core::ops::{BitOr, BitAnd, BitXor, Sub, RangeArgument}; +use core::ops::{BitOr, BitAnd, BitXor, Sub}; use borrow::Borrow; use btree_map::{BTreeMap, Keys}; use super::Recover; +use range::RangeArgument; // FIXME(conventions): implement bounded iterators diff --git a/src/liballoc/lib.rs b/src/liballoc/lib.rs index 6f4e300fd3c..ca52943ea97 100644 --- a/src/liballoc/lib.rs +++ b/src/liballoc/lib.rs @@ -203,7 +203,56 @@ mod std { pub use core::ops; // RangeFull } -pub use core::ops::Bound; +/// An endpoint of a range of keys. +/// +/// # Examples +/// +/// `Bound`s are range endpoints: +/// +/// ``` +/// #![feature(collections_range)] +/// +/// use std::collections::range::RangeArgument; +/// use std::collections::Bound::*; +/// +/// assert_eq!((..100).start(), Unbounded); +/// assert_eq!((1..12).start(), Included(&1)); +/// assert_eq!((1..12).end(), Excluded(&12)); +/// ``` +/// +/// Using a tuple of `Bound`s as an argument to [`BTreeMap::range`]. +/// Note that in most cases, it's better to use range syntax (`1..5`) instead. +/// +/// ``` +/// use std::collections::BTreeMap; +/// use std::collections::Bound::{Excluded, Included, Unbounded}; +/// +/// let mut map = BTreeMap::new(); +/// map.insert(3, "a"); +/// map.insert(5, "b"); +/// map.insert(8, "c"); +/// +/// for (key, value) in map.range((Excluded(3), Included(8))) { +/// println!("{}: {}", key, value); +/// } +/// +/// assert_eq!(Some((&3, &"a")), map.range((Unbounded, Included(5))).next()); +/// ``` +/// +/// [`BTreeMap::range`]: btree_map/struct.BTreeMap.html#method.range +#[stable(feature = "collections_bound", since = "1.17.0")] +#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)] +pub enum Bound<T> { + /// An inclusive bound. + #[stable(feature = "collections_bound", since = "1.17.0")] + Included(T), + /// An exclusive bound. + #[stable(feature = "collections_bound", since = "1.17.0")] + Excluded(T), + /// An infinite endpoint. Indicates that there is no bound in this direction. + #[stable(feature = "collections_bound", since = "1.17.0")] + Unbounded, +} /// An intermediate trait for specialization of `Extend`. #[doc(hidden)] diff --git a/src/liballoc/range.rs b/src/liballoc/range.rs index 0a058c47a50..f862da0d61e 100644 --- a/src/liballoc/range.rs +++ b/src/liballoc/range.rs @@ -11,8 +11,142 @@ #![unstable(feature = "collections_range", reason = "waiting for dust to settle on inclusive ranges", issue = "30877")] -#![rustc_deprecated(reason = "moved to core::ops", since = "1.19.0")] //! Range syntax. -pub use core::ops::RangeArgument; +use core::ops::{RangeFull, Range, RangeTo, RangeFrom, RangeInclusive, RangeToInclusive}; +use Bound::{self, Excluded, Included, Unbounded}; + +/// `RangeArgument` is implemented by Rust's built-in range types, produced +/// by range syntax like `..`, `a..`, `..b` or `c..d`. +pub trait RangeArgument<T: ?Sized> { + /// Start index bound. + /// + /// Returns the start value as a `Bound`. + /// + /// # Examples + /// + /// ``` + /// #![feature(alloc)] + /// #![feature(collections_range)] + /// + /// extern crate alloc; + /// + /// # fn main() { + /// use alloc::range::RangeArgument; + /// use alloc::Bound::*; + /// + /// assert_eq!((..10).start(), Unbounded); + /// assert_eq!((3..10).start(), Included(&3)); + /// # } + /// ``` + fn start(&self) -> Bound<&T>; + + /// End index bound. + /// + /// Returns the end value as a `Bound`. + /// + /// # Examples + /// + /// ``` + /// #![feature(alloc)] + /// #![feature(collections_range)] + /// + /// extern crate alloc; + /// + /// # fn main() { + /// use alloc::range::RangeArgument; + /// use alloc::Bound::*; + /// + /// assert_eq!((3..).end(), Unbounded); + /// assert_eq!((3..10).end(), Excluded(&10)); + /// # } + /// ``` + fn end(&self) -> Bound<&T>; +} + +// FIXME add inclusive ranges to RangeArgument + +impl<T: ?Sized> RangeArgument<T> for RangeFull { + fn start(&self) -> Bound<&T> { + Unbounded + } + fn end(&self) -> Bound<&T> { + Unbounded + } +} + +impl<T> RangeArgument<T> for RangeFrom<T> { + fn start(&self) -> Bound<&T> { + Included(&self.start) + } + fn end(&self) -> Bound<&T> { + Unbounded + } +} + +impl<T> RangeArgument<T> for RangeTo<T> { + fn start(&self) -> Bound<&T> { + Unbounded + } + fn end(&self) -> Bound<&T> { + Excluded(&self.end) + } +} + +impl<T> RangeArgument<T> for Range<T> { + fn start(&self) -> Bound<&T> { + Included(&self.start) + } + fn end(&self) -> Bound<&T> { + Excluded(&self.end) + } +} + +#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")] +impl<T> RangeArgument<T> for RangeInclusive<T> { + fn start(&self) -> Bound<&T> { + Included(&self.start) + } + fn end(&self) -> Bound<&T> { + Included(&self.end) + } +} + +#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")] +impl<T> RangeArgument<T> for RangeToInclusive<T> { + fn start(&self) -> Bound<&T> { + Unbounded + } + fn end(&self) -> Bound<&T> { + Included(&self.end) + } +} + +impl<T> RangeArgument<T> for (Bound<T>, Bound<T>) { + fn start(&self) -> Bound<&T> { + match *self { + (Included(ref start), _) => Included(start), + (Excluded(ref start), _) => Excluded(start), + (Unbounded, _) => Unbounded, + } + } + + fn end(&self) -> Bound<&T> { + match *self { + (_, Included(ref end)) => Included(end), + (_, Excluded(ref end)) => Excluded(end), + (_, Unbounded) => Unbounded, + } + } +} + +impl<'a, T: ?Sized + 'a> RangeArgument<T> for (Bound<&'a T>, Bound<&'a T>) { + fn start(&self) -> Bound<&T> { + self.0 + } + + fn end(&self) -> Bound<&T> { + self.1 + } +} diff --git a/src/liballoc/string.rs b/src/liballoc/string.rs index 1f0894d39d4..79d1ccf637d 100644 --- a/src/liballoc/string.rs +++ b/src/liballoc/string.rs @@ -59,14 +59,15 @@ use core::fmt; use core::hash; use core::iter::{FromIterator, FusedIterator}; -use core::ops::{self, Add, AddAssign, Index, IndexMut, RangeArgument}; -use core::ops::Bound::{Excluded, Included, Unbounded}; +use core::ops::{self, Add, AddAssign, Index, IndexMut}; use core::ptr; use core::str::pattern::Pattern; use std_unicode::lossy; use std_unicode::char::{decode_utf16, REPLACEMENT_CHARACTER}; use borrow::{Cow, ToOwned}; +use range::RangeArgument; +use Bound::{Excluded, Included, Unbounded}; use str::{self, from_boxed_utf8_unchecked, FromStr, Utf8Error, Chars}; use vec::Vec; use boxed::Box; diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs index e118d406ecf..5d1999a4262 100644 --- a/src/liballoc/vec.rs +++ b/src/liballoc/vec.rs @@ -74,8 +74,7 @@ use core::iter::{FromIterator, FusedIterator, TrustedLen}; use core::mem; #[cfg(not(test))] use core::num::Float; -use core::ops::{InPlace, Index, IndexMut, Place, Placer, RangeArgument}; -use core::ops::Bound::{Excluded, Included, Unbounded}; +use core::ops::{InPlace, Index, IndexMut, Place, Placer}; use core::ops; use core::ptr; use core::ptr::Shared; @@ -85,6 +84,8 @@ use borrow::ToOwned; use borrow::Cow; use boxed::Box; use raw_vec::RawVec; +use super::range::RangeArgument; +use Bound::{Excluded, Included, Unbounded}; /// A contiguous growable array type, written `Vec<T>` but pronounced 'vector'. /// diff --git a/src/liballoc/vec_deque.rs b/src/liballoc/vec_deque.rs index d1d334267bc..18175a5d01b 100644 --- a/src/liballoc/vec_deque.rs +++ b/src/liballoc/vec_deque.rs @@ -21,8 +21,7 @@ use core::cmp::Ordering; use core::fmt; use core::iter::{repeat, FromIterator, FusedIterator}; use core::mem; -use core::ops::{Index, IndexMut, Place, Placer, InPlace, RangeArgument}; -use core::ops::Bound::{Excluded, Included, Unbounded}; +use core::ops::{Index, IndexMut, Place, Placer, InPlace}; use core::ptr; use core::ptr::Shared; use core::slice; @@ -32,6 +31,8 @@ use core::cmp; use raw_vec::RawVec; +use super::range::RangeArgument; +use Bound::{Excluded, Included, Unbounded}; use super::vec::Vec; const INITIAL_CAPACITY: usize = 7; // 2^3 - 1 |
