Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 688cbda

Browse files
author
Clar Charr
committedApr 24, 2017
Move RangeArgument and Bound to libcore.
1 parent 968ae7b commit 688cbda

File tree

3 files changed

+217
-196
lines changed

3 files changed

+217
-196
lines changed
 

‎src/libcollections/lib.rs

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -136,60 +136,11 @@ mod std {
136136
pub use core::ops; // RangeFull
137137
}
138138

139-
/// An endpoint of a range of keys.
140-
///
141-
/// # Examples
142-
///
143-
/// `Bound`s are range endpoints:
144-
///
145-
/// ```
146-
/// #![feature(collections_range)]
147-
///
148-
/// use std::collections::range::RangeArgument;
149-
/// use std::collections::Bound::*;
150-
///
151-
/// assert_eq!((..100).start(), Unbounded);
152-
/// assert_eq!((1..12).start(), Included(&1));
153-
/// assert_eq!((1..12).end(), Excluded(&12));
154-
/// ```
155-
///
156-
/// Using a tuple of `Bound`s as an argument to [`BTreeMap::range`].
157-
/// Note that in most cases, it's better to use range syntax (`1..5`) instead.
158-
///
159-
/// ```
160-
/// use std::collections::BTreeMap;
161-
/// use std::collections::Bound::{Excluded, Included, Unbounded};
162-
///
163-
/// let mut map = BTreeMap::new();
164-
/// map.insert(3, "a");
165-
/// map.insert(5, "b");
166-
/// map.insert(8, "c");
167-
///
168-
/// for (key, value) in map.range((Excluded(3), Included(8))) {
169-
/// println!("{}: {}", key, value);
170-
/// }
171-
///
172-
/// assert_eq!(Some((&3, &"a")), map.range((Unbounded, Included(5))).next());
173-
/// ```
174-
///
175-
/// [`BTreeMap::range`]: btree_map/struct.BTreeMap.html#method.range
176-
#[stable(feature = "collections_bound", since = "1.17.0")]
177-
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
178-
pub enum Bound<T> {
179-
/// An inclusive bound.
180-
#[stable(feature = "collections_bound", since = "1.17.0")]
181-
Included(T),
182-
/// An exclusive bound.
183-
#[stable(feature = "collections_bound", since = "1.17.0")]
184-
Excluded(T),
185-
/// An infinite endpoint. Indicates that there is no bound in this direction.
186-
#[stable(feature = "collections_bound", since = "1.17.0")]
187-
Unbounded,
188-
}
189-
190139
/// An intermediate trait for specialization of `Extend`.
191140
#[doc(hidden)]
192141
trait SpecExtend<I: IntoIterator> {
193142
/// Extends `self` with the contents of the given iterator.
194143
fn spec_extend(&mut self, iter: I);
195144
}
145+
146+
pub use core::ops::Bound;

‎src/libcollections/range.rs

Lines changed: 1 addition & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -8,151 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![unstable(feature = "collections_range",
12-
reason = "waiting for dust to settle on inclusive ranges",
13-
issue = "30877")]
1411

1512
//! Range syntax.
1613
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(collections)]
31-
/// #![feature(collections_range)]
32-
///
33-
/// extern crate collections;
34-
///
35-
/// # fn main() {
36-
/// use collections::range::RangeArgument;
37-
/// use collections::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(collections)]
53-
/// #![feature(collections_range)]
54-
///
55-
/// extern crate collections;
56-
///
57-
/// # fn main() {
58-
/// use collections::range::RangeArgument;
59-
/// use collections::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-
match *self {
110-
RangeInclusive::Empty{ ref at } => Included(at),
111-
RangeInclusive::NonEmpty { ref start, .. } => Included(start),
112-
}
113-
}
114-
fn end(&self) -> Bound<&T> {
115-
match *self {
116-
RangeInclusive::Empty{ ref at } => Excluded(at),
117-
RangeInclusive::NonEmpty { ref end, .. } => Included(end),
118-
}
119-
}
120-
}
121-
122-
#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
123-
impl<T> RangeArgument<T> for RangeToInclusive<T> {
124-
fn start(&self) -> Bound<&T> {
125-
Unbounded
126-
}
127-
fn end(&self) -> Bound<&T> {
128-
Included(&self.end)
129-
}
130-
}
131-
132-
impl<T> RangeArgument<T> for (Bound<T>, Bound<T>) {
133-
fn start(&self) -> Bound<&T> {
134-
match *self {
135-
(Included(ref start), _) => Included(start),
136-
(Excluded(ref start), _) => Excluded(start),
137-
(Unbounded, _) => Unbounded,
138-
}
139-
}
140-
141-
fn end(&self) -> Bound<&T> {
142-
match *self {
143-
(_, Included(ref end)) => Included(end),
144-
(_, Excluded(ref end)) => Excluded(end),
145-
(_, Unbounded) => Unbounded,
146-
}
147-
}
148-
}
149-
150-
impl<'a, T: ?Sized + 'a> RangeArgument<T> for (Bound<&'a T>, Bound<&'a T>) {
151-
fn start(&self) -> Bound<&T> {
152-
self.0
153-
}
154-
155-
fn end(&self) -> Bound<&T> {
156-
self.1
157-
}
158-
}
14+
pub use core::ops::RangeArgument;

‎src/libcore/ops.rs

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2381,6 +2381,220 @@ impl<Idx: PartialOrd<Idx>> RangeToInclusive<Idx> {
23812381
// RangeToInclusive<Idx> cannot impl From<RangeTo<Idx>>
23822382
// because underflow would be possible with (..0).into()
23832383

2384+
2385+
/// An endpoint of a range of keys.
2386+
///
2387+
/// # Examples
2388+
///
2389+
/// `Bound`s are range endpoints:
2390+
///
2391+
/// ```
2392+
/// #![feature(collections_range)]
2393+
///
2394+
/// use std::collections::range::RangeArgument;
2395+
/// use std::collections::Bound::*;
2396+
///
2397+
/// assert_eq!((..100).start(), Unbounded);
2398+
/// assert_eq!((1..12).start(), Included(&1));
2399+
/// assert_eq!((1..12).end(), Excluded(&12));
2400+
/// ```
2401+
///
2402+
/// Using a tuple of `Bound`s as an argument to [`BTreeMap::range`].
2403+
/// Note that in most cases, it's better to use range syntax (`1..5`) instead.
2404+
///
2405+
/// ```
2406+
/// use std::collections::BTreeMap;
2407+
/// use std::collections::Bound::{Excluded, Included, Unbounded};
2408+
///
2409+
/// let mut map = BTreeMap::new();
2410+
/// map.insert(3, "a");
2411+
/// map.insert(5, "b");
2412+
/// map.insert(8, "c");
2413+
///
2414+
/// for (key, value) in map.range((Excluded(3), Included(8))) {
2415+
/// println!("{}: {}", key, value);
2416+
/// }
2417+
///
2418+
/// assert_eq!(Some((&3, &"a")), map.range((Unbounded, Included(5))).next());
2419+
/// ```
2420+
///
2421+
/// [`BTreeMap::range`]: btree_map/struct.BTreeMap.html#method.range
2422+
#[stable(feature = "collections_bound", since = "1.17.0")]
2423+
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
2424+
pub enum Bound<T> {
2425+
/// An inclusive bound.
2426+
#[stable(feature = "collections_bound", since = "1.17.0")]
2427+
Included(T),
2428+
/// An exclusive bound.
2429+
#[stable(feature = "collections_bound", since = "1.17.0")]
2430+
Excluded(T),
2431+
/// An infinite endpoint. Indicates that there is no bound in this direction.
2432+
#[stable(feature = "collections_bound", since = "1.17.0")]
2433+
Unbounded,
2434+
}
2435+
2436+
/// `RangeArgument` is implemented by Rust's built-in range types, produced
2437+
/// by range syntax like `..`, `a..`, `..b` or `c..d`.
2438+
#[unstable(feature = "collections_range",
2439+
reason = "waiting for dust to settle on inclusive ranges",
2440+
issue = "30877")]
2441+
pub trait RangeArgument<T: ?Sized> {
2442+
/// Start index bound.
2443+
///
2444+
/// Returns the start value as a `Bound`.
2445+
///
2446+
/// # Examples
2447+
///
2448+
/// ```
2449+
/// #![feature(collections)]
2450+
/// #![feature(collections_range)]
2451+
///
2452+
/// extern crate collections;
2453+
///
2454+
/// # fn main() {
2455+
/// use collections::range::RangeArgument;
2456+
/// use collections::Bound::*;
2457+
///
2458+
/// assert_eq!((..10).start(), Unbounded);
2459+
/// assert_eq!((3..10).start(), Included(&3));
2460+
/// # }
2461+
/// ```
2462+
fn start(&self) -> Bound<&T>;
2463+
2464+
/// End index bound.
2465+
///
2466+
/// Returns the end value as a `Bound`.
2467+
///
2468+
/// # Examples
2469+
///
2470+
/// ```
2471+
/// #![feature(collections)]
2472+
/// #![feature(collections_range)]
2473+
///
2474+
/// extern crate collections;
2475+
///
2476+
/// # fn main() {
2477+
/// use collections::range::RangeArgument;
2478+
/// use collections::Bound::*;
2479+
///
2480+
/// assert_eq!((3..).end(), Unbounded);
2481+
/// assert_eq!((3..10).end(), Excluded(&10));
2482+
/// # }
2483+
/// ```
2484+
fn end(&self) -> Bound<&T>;
2485+
}
2486+
2487+
// FIXME add inclusive ranges to RangeArgument
2488+
2489+
#[unstable(feature = "collections_range",
2490+
reason = "waiting for dust to settle on inclusive ranges",
2491+
issue = "30877")]
2492+
impl<T: ?Sized> RangeArgument<T> for RangeFull {
2493+
fn start(&self) -> Bound<&T> {
2494+
Bound::Unbounded
2495+
}
2496+
fn end(&self) -> Bound<&T> {
2497+
Bound::Unbounded
2498+
}
2499+
}
2500+
2501+
#[unstable(feature = "collections_range",
2502+
reason = "waiting for dust to settle on inclusive ranges",
2503+
issue = "30877")]
2504+
impl<T> RangeArgument<T> for RangeFrom<T> {
2505+
fn start(&self) -> Bound<&T> {
2506+
Bound::Included(&self.start)
2507+
}
2508+
fn end(&self) -> Bound<&T> {
2509+
Bound::Unbounded
2510+
}
2511+
}
2512+
2513+
#[unstable(feature = "collections_range",
2514+
reason = "waiting for dust to settle on inclusive ranges",
2515+
issue = "30877")]
2516+
impl<T> RangeArgument<T> for RangeTo<T> {
2517+
fn start(&self) -> Bound<&T> {
2518+
Bound::Unbounded
2519+
}
2520+
fn end(&self) -> Bound<&T> {
2521+
Bound::Excluded(&self.end)
2522+
}
2523+
}
2524+
2525+
#[unstable(feature = "collections_range",
2526+
reason = "waiting for dust to settle on inclusive ranges",
2527+
issue = "30877")]
2528+
impl<T> RangeArgument<T> for Range<T> {
2529+
fn start(&self) -> Bound<&T> {
2530+
Bound::Included(&self.start)
2531+
}
2532+
fn end(&self) -> Bound<&T> {
2533+
Bound::Excluded(&self.end)
2534+
}
2535+
}
2536+
2537+
#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
2538+
impl<T> RangeArgument<T> for RangeInclusive<T> {
2539+
fn start(&self) -> Bound<&T> {
2540+
match *self {
2541+
RangeInclusive::Empty{ ref at } => Bound::Included(at),
2542+
RangeInclusive::NonEmpty { ref start, .. } => Bound::Included(start),
2543+
}
2544+
}
2545+
fn end(&self) -> Bound<&T> {
2546+
match *self {
2547+
RangeInclusive::Empty{ ref at } => Bound::Excluded(at),
2548+
RangeInclusive::NonEmpty { ref end, .. } => Bound::Included(end),
2549+
}
2550+
}
2551+
}
2552+
2553+
#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
2554+
impl<T> RangeArgument<T> for RangeToInclusive<T> {
2555+
fn start(&self) -> Bound<&T> {
2556+
Bound::Unbounded
2557+
}
2558+
fn end(&self) -> Bound<&T> {
2559+
Bound::Included(&self.end)
2560+
}
2561+
}
2562+
2563+
#[unstable(feature = "collections_range",
2564+
reason = "waiting for dust to settle on inclusive ranges",
2565+
issue = "30877")]
2566+
impl<T> RangeArgument<T> for (Bound<T>, Bound<T>) {
2567+
fn start(&self) -> Bound<&T> {
2568+
match *self {
2569+
(Bound::Included(ref start), _) => Bound::Included(start),
2570+
(Bound::Excluded(ref start), _) => Bound::Excluded(start),
2571+
(Bound::Unbounded, _) => Bound::Unbounded,
2572+
}
2573+
}
2574+
2575+
fn end(&self) -> Bound<&T> {
2576+
match *self {
2577+
(_, Bound::Included(ref end)) => Bound::Included(end),
2578+
(_, Bound::Excluded(ref end)) => Bound::Excluded(end),
2579+
(_, Bound::Unbounded) => Bound::Unbounded,
2580+
}
2581+
}
2582+
}
2583+
2584+
#[unstable(feature = "collections_range",
2585+
reason = "waiting for dust to settle on inclusive ranges",
2586+
issue = "30877")]
2587+
impl<'a, T: ?Sized + 'a> RangeArgument<T> for (Bound<&'a T>, Bound<&'a T>) {
2588+
fn start(&self) -> Bound<&T> {
2589+
self.0
2590+
}
2591+
2592+
fn end(&self) -> Bound<&T> {
2593+
self.1
2594+
}
2595+
}
2596+
2597+
23842598
/// The `Deref` trait is used to specify the functionality of dereferencing
23852599
/// operations, like `*v`.
23862600
///

0 commit comments

Comments
 (0)
Please sign in to comment.