Skip to content

Commit a5d34e1

Browse files
committed
Auto merge of #42991 - sfackler:unstable-rangeargument, r=alexcrichton
Revert "Stabilize RangeArgument" This reverts commit 143206d. From the discussion in #30877 it seems like this is premature.
2 parents 7a2c09b + 0a9c136 commit a5d34e1

File tree

15 files changed

+217
-187
lines changed

15 files changed

+217
-187
lines changed

src/liballoc/btree/map.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ use core::fmt::Debug;
1313
use core::hash::{Hash, Hasher};
1414
use core::iter::{FromIterator, Peekable, FusedIterator};
1515
use core::marker::PhantomData;
16-
use core::ops::{Index, RangeArgument};
17-
use core::ops::Bound::{Excluded, Included, Unbounded};
16+
use core::ops::Index;
1817
use core::{fmt, intrinsics, mem, ptr};
1918

2019
use borrow::Borrow;
20+
use Bound::{Excluded, Included, Unbounded};
21+
use range::RangeArgument;
2122

2223
use super::node::{self, Handle, NodeRef, marker};
2324
use super::search;

src/liballoc/btree/set.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ use core::cmp::{min, max};
1616
use core::fmt::Debug;
1717
use core::fmt;
1818
use core::iter::{Peekable, FromIterator, FusedIterator};
19-
use core::ops::{BitOr, BitAnd, BitXor, Sub, RangeArgument};
19+
use core::ops::{BitOr, BitAnd, BitXor, Sub};
2020

2121
use borrow::Borrow;
2222
use btree_map::{BTreeMap, Keys};
2323
use super::Recover;
24+
use range::RangeArgument;
2425

2526
// FIXME(conventions): implement bounded iterators
2627

src/liballoc/lib.rs

+50-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,56 @@ mod std {
203203
pub use core::ops; // RangeFull
204204
}
205205

206-
pub use core::ops::Bound;
206+
/// An endpoint of a range of keys.
207+
///
208+
/// # Examples
209+
///
210+
/// `Bound`s are range endpoints:
211+
///
212+
/// ```
213+
/// #![feature(collections_range)]
214+
///
215+
/// use std::collections::range::RangeArgument;
216+
/// use std::collections::Bound::*;
217+
///
218+
/// assert_eq!((..100).start(), Unbounded);
219+
/// assert_eq!((1..12).start(), Included(&1));
220+
/// assert_eq!((1..12).end(), Excluded(&12));
221+
/// ```
222+
///
223+
/// Using a tuple of `Bound`s as an argument to [`BTreeMap::range`].
224+
/// Note that in most cases, it's better to use range syntax (`1..5`) instead.
225+
///
226+
/// ```
227+
/// use std::collections::BTreeMap;
228+
/// use std::collections::Bound::{Excluded, Included, Unbounded};
229+
///
230+
/// let mut map = BTreeMap::new();
231+
/// map.insert(3, "a");
232+
/// map.insert(5, "b");
233+
/// map.insert(8, "c");
234+
///
235+
/// for (key, value) in map.range((Excluded(3), Included(8))) {
236+
/// println!("{}: {}", key, value);
237+
/// }
238+
///
239+
/// assert_eq!(Some((&3, &"a")), map.range((Unbounded, Included(5))).next());
240+
/// ```
241+
///
242+
/// [`BTreeMap::range`]: btree_map/struct.BTreeMap.html#method.range
243+
#[stable(feature = "collections_bound", since = "1.17.0")]
244+
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
245+
pub enum Bound<T> {
246+
/// An inclusive bound.
247+
#[stable(feature = "collections_bound", since = "1.17.0")]
248+
Included(T),
249+
/// An exclusive bound.
250+
#[stable(feature = "collections_bound", since = "1.17.0")]
251+
Excluded(T),
252+
/// An infinite endpoint. Indicates that there is no bound in this direction.
253+
#[stable(feature = "collections_bound", since = "1.17.0")]
254+
Unbounded,
255+
}
207256

208257
/// An intermediate trait for specialization of `Extend`.
209258
#[doc(hidden)]

src/liballoc/range.rs

+136-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,142 @@
1111
#![unstable(feature = "collections_range",
1212
reason = "waiting for dust to settle on inclusive ranges",
1313
issue = "30877")]
14-
#![rustc_deprecated(reason = "moved to core::ops", since = "1.19.0")]
1514

1615
//! Range syntax.
1716
18-
pub use core::ops::RangeArgument;
17+
use core::ops::{RangeFull, Range, RangeTo, RangeFrom, RangeInclusive, RangeToInclusive};
18+
use Bound::{self, Excluded, Included, Unbounded};
19+
20+
/// `RangeArgument` is implemented by Rust's built-in range types, produced
21+
/// by range syntax like `..`, `a..`, `..b` or `c..d`.
22+
pub trait RangeArgument<T: ?Sized> {
23+
/// Start index bound.
24+
///
25+
/// Returns the start value as a `Bound`.
26+
///
27+
/// # Examples
28+
///
29+
/// ```
30+
/// #![feature(alloc)]
31+
/// #![feature(collections_range)]
32+
///
33+
/// extern crate alloc;
34+
///
35+
/// # fn main() {
36+
/// use alloc::range::RangeArgument;
37+
/// use alloc::Bound::*;
38+
///
39+
/// assert_eq!((..10).start(), Unbounded);
40+
/// assert_eq!((3..10).start(), Included(&3));
41+
/// # }
42+
/// ```
43+
fn start(&self) -> Bound<&T>;
44+
45+
/// End index bound.
46+
///
47+
/// Returns the end value as a `Bound`.
48+
///
49+
/// # Examples
50+
///
51+
/// ```
52+
/// #![feature(alloc)]
53+
/// #![feature(collections_range)]
54+
///
55+
/// extern crate alloc;
56+
///
57+
/// # fn main() {
58+
/// use alloc::range::RangeArgument;
59+
/// use alloc::Bound::*;
60+
///
61+
/// assert_eq!((3..).end(), Unbounded);
62+
/// assert_eq!((3..10).end(), Excluded(&10));
63+
/// # }
64+
/// ```
65+
fn end(&self) -> Bound<&T>;
66+
}
67+
68+
// FIXME add inclusive ranges to RangeArgument
69+
70+
impl<T: ?Sized> RangeArgument<T> for RangeFull {
71+
fn start(&self) -> Bound<&T> {
72+
Unbounded
73+
}
74+
fn end(&self) -> Bound<&T> {
75+
Unbounded
76+
}
77+
}
78+
79+
impl<T> RangeArgument<T> for RangeFrom<T> {
80+
fn start(&self) -> Bound<&T> {
81+
Included(&self.start)
82+
}
83+
fn end(&self) -> Bound<&T> {
84+
Unbounded
85+
}
86+
}
87+
88+
impl<T> RangeArgument<T> for RangeTo<T> {
89+
fn start(&self) -> Bound<&T> {
90+
Unbounded
91+
}
92+
fn end(&self) -> Bound<&T> {
93+
Excluded(&self.end)
94+
}
95+
}
96+
97+
impl<T> RangeArgument<T> for Range<T> {
98+
fn start(&self) -> Bound<&T> {
99+
Included(&self.start)
100+
}
101+
fn end(&self) -> Bound<&T> {
102+
Excluded(&self.end)
103+
}
104+
}
105+
106+
#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
107+
impl<T> RangeArgument<T> for RangeInclusive<T> {
108+
fn start(&self) -> Bound<&T> {
109+
Included(&self.start)
110+
}
111+
fn end(&self) -> Bound<&T> {
112+
Included(&self.end)
113+
}
114+
}
115+
116+
#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
117+
impl<T> RangeArgument<T> for RangeToInclusive<T> {
118+
fn start(&self) -> Bound<&T> {
119+
Unbounded
120+
}
121+
fn end(&self) -> Bound<&T> {
122+
Included(&self.end)
123+
}
124+
}
125+
126+
impl<T> RangeArgument<T> for (Bound<T>, Bound<T>) {
127+
fn start(&self) -> Bound<&T> {
128+
match *self {
129+
(Included(ref start), _) => Included(start),
130+
(Excluded(ref start), _) => Excluded(start),
131+
(Unbounded, _) => Unbounded,
132+
}
133+
}
134+
135+
fn end(&self) -> Bound<&T> {
136+
match *self {
137+
(_, Included(ref end)) => Included(end),
138+
(_, Excluded(ref end)) => Excluded(end),
139+
(_, Unbounded) => Unbounded,
140+
}
141+
}
142+
}
143+
144+
impl<'a, T: ?Sized + 'a> RangeArgument<T> for (Bound<&'a T>, Bound<&'a T>) {
145+
fn start(&self) -> Bound<&T> {
146+
self.0
147+
}
148+
149+
fn end(&self) -> Bound<&T> {
150+
self.1
151+
}
152+
}

src/liballoc/string.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,15 @@
5959
use core::fmt;
6060
use core::hash;
6161
use core::iter::{FromIterator, FusedIterator};
62-
use core::ops::{self, Add, AddAssign, Index, IndexMut, RangeArgument};
63-
use core::ops::Bound::{Excluded, Included, Unbounded};
62+
use core::ops::{self, Add, AddAssign, Index, IndexMut};
6463
use core::ptr;
6564
use core::str::pattern::Pattern;
6665
use std_unicode::lossy;
6766
use std_unicode::char::{decode_utf16, REPLACEMENT_CHARACTER};
6867

6968
use borrow::{Cow, ToOwned};
69+
use range::RangeArgument;
70+
use Bound::{Excluded, Included, Unbounded};
7071
use str::{self, from_boxed_utf8_unchecked, FromStr, Utf8Error, Chars};
7172
use vec::Vec;
7273
use boxed::Box;

src/liballoc/vec.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ use core::iter::{FromIterator, FusedIterator, TrustedLen};
7474
use core::mem;
7575
#[cfg(not(test))]
7676
use core::num::Float;
77-
use core::ops::{InPlace, Index, IndexMut, Place, Placer, RangeArgument};
78-
use core::ops::Bound::{Excluded, Included, Unbounded};
77+
use core::ops::{InPlace, Index, IndexMut, Place, Placer};
7978
use core::ops;
8079
use core::ptr;
8180
use core::ptr::Shared;
@@ -85,6 +84,8 @@ use borrow::ToOwned;
8584
use borrow::Cow;
8685
use boxed::Box;
8786
use raw_vec::RawVec;
87+
use super::range::RangeArgument;
88+
use Bound::{Excluded, Included, Unbounded};
8889

8990
/// A contiguous growable array type, written `Vec<T>` but pronounced 'vector'.
9091
///

src/liballoc/vec_deque.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ use core::cmp::Ordering;
2121
use core::fmt;
2222
use core::iter::{repeat, FromIterator, FusedIterator};
2323
use core::mem;
24-
use core::ops::{Index, IndexMut, Place, Placer, InPlace, RangeArgument};
25-
use core::ops::Bound::{Excluded, Included, Unbounded};
24+
use core::ops::{Index, IndexMut, Place, Placer, InPlace};
2625
use core::ptr;
2726
use core::ptr::Shared;
2827
use core::slice;
@@ -32,6 +31,8 @@ use core::cmp;
3231

3332
use raw_vec::RawVec;
3433

34+
use super::range::RangeArgument;
35+
use Bound::{Excluded, Included, Unbounded};
3536
use super::vec::Vec;
3637

3738
const INITIAL_CAPACITY: usize = 7; // 2^3 - 1

src/libcollections/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ pub use alloc::binary_heap;
4646
pub use alloc::borrow;
4747
pub use alloc::fmt;
4848
pub use alloc::linked_list;
49-
#[allow(deprecated)]
5049
pub use alloc::range;
5150
pub use alloc::slice;
5251
pub use alloc::str;

src/libcore/ops/mod.rs

-3
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,6 @@ pub use self::index::{Index, IndexMut};
183183
#[stable(feature = "rust1", since = "1.0.0")]
184184
pub use self::range::{Range, RangeFrom, RangeFull, RangeTo};
185185

186-
#[stable(feature = "range_argument", since = "1.19.0")]
187-
pub use self::range::{RangeArgument, Bound};
188-
189186
#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
190187
pub use self::range::{RangeInclusive, RangeToInclusive};
191188

0 commit comments

Comments
 (0)