Skip to content

Commit fbd095d

Browse files
author
Clar Charr
committed
Change trait RangeArgument to struct RangeBounds.
1 parent 4c225c4 commit fbd095d

File tree

14 files changed

+201
-257
lines changed

14 files changed

+201
-257
lines changed

src/liballoc/btree/map.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@ 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;
16+
use core::ops::{Index, RangeBounds};
17+
use core::ops::Bound::{Excluded, Included, Unbounded};
1718
use core::{fmt, intrinsics, mem, ptr};
1819

1920
use borrow::Borrow;
20-
use Bound::{Excluded, Included, Unbounded};
21-
use range::RangeArgument;
2221

2322
use super::node::{self, Handle, NodeRef, marker};
2423
use super::search;
@@ -790,11 +789,11 @@ impl<K: Ord, V> BTreeMap<K, V> {
790789
/// ```
791790
#[stable(feature = "btree_range", since = "1.17.0")]
792791
pub fn range<T: ?Sized, R>(&self, range: R) -> Range<K, V>
793-
where T: Ord, K: Borrow<T>, R: RangeArgument<T>
792+
where T: Ord, K: Borrow<T>, R: Into<RangeBounds<T>>
794793
{
795794
let root1 = self.root.as_ref();
796795
let root2 = self.root.as_ref();
797-
let (f, b) = range_search(root1, root2, range);
796+
let (f, b) = range_search(root1, root2, range.into());
798797

799798
Range { front: f, back: b}
800799
}
@@ -830,11 +829,11 @@ impl<K: Ord, V> BTreeMap<K, V> {
830829
/// ```
831830
#[stable(feature = "btree_range", since = "1.17.0")]
832831
pub fn range_mut<T: ?Sized, R>(&mut self, range: R) -> RangeMut<K, V>
833-
where T: Ord, K: Borrow<T>, R: RangeArgument<T>
832+
where T: Ord, K: Borrow<T>, R: Into<RangeBounds<T>>
834833
{
835834
let root1 = self.root.as_mut();
836835
let root2 = unsafe { ptr::read(&root1) };
837-
let (f, b) = range_search(root1, root2, range);
836+
let (f, b) = range_search(root1, root2, range.into());
838837

839838
RangeMut {
840839
front: f,
@@ -1780,15 +1779,15 @@ fn last_leaf_edge<BorrowType, K, V>
17801779
}
17811780
}
17821781

1783-
fn range_search<BorrowType, K, V, Q: ?Sized, R: RangeArgument<Q>>(
1782+
fn range_search<BorrowType, K, V, Q: ?Sized>(
17841783
root1: NodeRef<BorrowType, K, V, marker::LeafOrInternal>,
17851784
root2: NodeRef<BorrowType, K, V, marker::LeafOrInternal>,
1786-
range: R
1785+
range: RangeBounds<&Q>
17871786
)-> (Handle<NodeRef<BorrowType, K, V, marker::Leaf>, marker::Edge>,
17881787
Handle<NodeRef<BorrowType, K, V, marker::Leaf>, marker::Edge>)
17891788
where Q: Ord, K: Borrow<Q>
17901789
{
1791-
match (range.start(), range.end()) {
1790+
match (range.start, range.end) {
17921791
(Excluded(s), Excluded(e)) if s==e =>
17931792
panic!("range start and end are equal and excluded in BTreeMap"),
17941793
(Included(s), Included(e)) |

src/liballoc/btree/set.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@ 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};
19+
use core::ops::{BitOr, BitAnd, BitXor, Sub, RangeBounds};
2020

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

2625
// FIXME(conventions): implement bounded iterators
2726

@@ -289,9 +288,9 @@ impl<T: Ord> BTreeSet<T> {
289288
/// ```
290289
#[stable(feature = "btree_range", since = "1.17.0")]
291290
pub fn range<K: ?Sized, R>(&self, range: R) -> Range<T>
292-
where K: Ord, T: Borrow<K>, R: RangeArgument<K>
291+
where K: Ord, T: Borrow<K>, R: Into<RangeBounds<K>>
293292
{
294-
Range { iter: self.map.range(range) }
293+
Range { iter: self.map.range(range.into()) }
295294
}
296295
}
297296

src/liballoc/lib.rs

+4-51
Original file line numberDiff line numberDiff line change
@@ -202,64 +202,17 @@ mod std {
202202
pub use core::ops; // RangeFull
203203
}
204204

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

212+
#[rustc_deprecated(reason = "moved to core::ops", since = "1.19.0")]
213+
#[allow(deprecated)]
214+
pub use core::ops::Bound;
215+
263216
pub use oom::oom;
264217

265218
#[doc(no_inline)]

src/liballoc/range.rs

+2-137
Original file line numberDiff line numberDiff line change
@@ -11,142 +11,7 @@
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")]
1415

1516
//! Range syntax.
16-
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-
}
17+
use core::ops::RangeBounds;

src/liballoc/string.rs

+16-17
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,14 @@
5959
use core::fmt;
6060
use core::hash;
6161
use core::iter::{FromIterator, FusedIterator};
62-
use core::ops::{self, Add, AddAssign, Index, IndexMut};
62+
use core::ops::{self, Add, AddAssign, Index, IndexMut, RangeBounds};
63+
use core::ops::Bound::{Excluded, Included, Unbounded};
6364
use core::ptr;
6465
use core::str::pattern::Pattern;
6566
use std_unicode::lossy;
6667
use std_unicode::char::{decode_utf16, REPLACEMENT_CHARACTER};
6768

6869
use borrow::{Cow, ToOwned};
69-
use range::RangeArgument;
70-
use Bound::{Excluded, Included, Unbounded};
7170
use str::{self, from_boxed_utf8_unchecked, FromStr, Utf8Error, Chars};
7271
use vec::Vec;
7372
use boxed::Box;
@@ -1270,7 +1269,7 @@ impl String {
12701269
/// ```
12711270
#[stable(feature = "drain", since = "1.6.0")]
12721271
pub fn drain<R>(&mut self, range: R) -> Drain
1273-
where R: RangeArgument<usize>
1272+
where R: Into<RangeBounds<usize>>
12741273
{
12751274
// Memory safety
12761275
//
@@ -1279,14 +1278,14 @@ impl String {
12791278
// Because the range removal happens in Drop, if the Drain iterator is leaked,
12801279
// the removal will not happen.
12811280
let len = self.len();
1282-
let start = match range.start() {
1283-
Included(&n) => n,
1284-
Excluded(&n) => n + 1,
1281+
let start = match range.start {
1282+
Included(n) => n,
1283+
Excluded(n) => n + 1,
12851284
Unbounded => 0,
12861285
};
1287-
let end = match range.end() {
1288-
Included(&n) => n + 1,
1289-
Excluded(&n) => n,
1286+
let end = match range.end {
1287+
Included(n) => n + 1,
1288+
Excluded(n) => n,
12901289
Unbounded => len,
12911290
};
12921291

@@ -1334,7 +1333,7 @@ impl String {
13341333
/// ```
13351334
#[unstable(feature = "splice", reason = "recently added", issue = "32310")]
13361335
pub fn splice<'a, 'b, R>(&'a mut self, range: R, replace_with: &'b str) -> Splice<'a, 'b>
1337-
where R: RangeArgument<usize>
1336+
where R: Into<RangeBounds<usize>>
13381337
{
13391338
// Memory safety
13401339
//
@@ -1343,14 +1342,14 @@ impl String {
13431342
// Because the range removal happens in Drop, if the Splice iterator is leaked,
13441343
// the removal will not happen.
13451344
let len = self.len();
1346-
let start = match range.start() {
1347-
Included(&n) => n,
1348-
Excluded(&n) => n + 1,
1345+
let start = match range.start {
1346+
Included(n) => n,
1347+
Excluded(n) => n + 1,
13491348
Unbounded => 0,
13501349
};
1351-
let end = match range.end() {
1352-
Included(&n) => n + 1,
1353-
Excluded(&n) => n,
1350+
let end = match range.end {
1351+
Included(n) => n + 1,
1352+
Excluded(n) => n,
13541353
Unbounded => len,
13551354
};
13561355

0 commit comments

Comments
 (0)