Skip to content

Commit 4278b5f

Browse files
committed
Auto merge of #27545 - apasel422:btree-range, r=Gankro
This permits collections with `String` keys to be ranged over with `&str` bounds. The `K` defaults for `Min` and `Max` permit the default type parameter fallback to work with things like ```rust use std::collections::{BTreeSet, Bound}; let set = BTreeSet::<String>::new(); set.range(Bound::Included("a"), Bound::Unbounded); ``` Without the defaults, the type of the maximum bound would be unconstrained. r? @gankro
2 parents 4f33e43 + 4fdeb35 commit 4278b5f

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

src/libcollections/btree/map.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,7 +1522,11 @@ impl<K: Ord, V> BTreeMap<K, V> {
15221522
/// ```
15231523
#[unstable(feature = "btree_range",
15241524
reason = "matches collection reform specification, waiting for dust to settle")]
1525-
pub fn range<'a>(&'a self, min: Bound<&K>, max: Bound<&K>) -> Range<'a, K, V> {
1525+
pub fn range<Min: ?Sized + Ord = K, Max: ?Sized + Ord = K>(&self, min: Bound<&Min>,
1526+
max: Bound<&Max>)
1527+
-> Range<K, V> where
1528+
K: Borrow<Min> + Borrow<Max>,
1529+
{
15261530
range_impl!(&self.root, min, max, as_slices_internal, iter, Range, edges, [])
15271531
}
15281532

@@ -1542,7 +1546,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
15421546
/// let mut map: BTreeMap<&str, i32> = ["Alice", "Bob", "Carol", "Cheryl"].iter()
15431547
/// .map(|&s| (s, 0))
15441548
/// .collect();
1545-
/// for (_, balance) in map.range_mut(Included(&"B"), Excluded(&"Cheryl")) {
1549+
/// for (_, balance) in map.range_mut(Included("B"), Excluded("Cheryl")) {
15461550
/// *balance += 100;
15471551
/// }
15481552
/// for (name, balance) in &map {
@@ -1551,7 +1555,11 @@ impl<K: Ord, V> BTreeMap<K, V> {
15511555
/// ```
15521556
#[unstable(feature = "btree_range",
15531557
reason = "matches collection reform specification, waiting for dust to settle")]
1554-
pub fn range_mut<'a>(&'a mut self, min: Bound<&K>, max: Bound<&K>) -> RangeMut<'a, K, V> {
1558+
pub fn range_mut<Min: ?Sized + Ord = K, Max: ?Sized + Ord = K>(&mut self, min: Bound<&Min>,
1559+
max: Bound<&Max>)
1560+
-> RangeMut<K, V> where
1561+
K: Borrow<Min> + Borrow<Max>,
1562+
{
15551563
range_impl!(&mut self.root, min, max, as_slices_internal_mut, iter_mut, RangeMut,
15561564
edges_mut, [mut])
15571565
}

src/libcollections/btree/node.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,7 +1528,9 @@ macro_rules! node_slice_impl {
15281528
}
15291529

15301530
/// Returns a sub-slice with elements starting with `min_key`.
1531-
pub fn slice_from(self, min_key: &K) -> $NodeSlice<'a, K, V> {
1531+
pub fn slice_from<Q: ?Sized + Ord>(self, min_key: &Q) -> $NodeSlice<'a, K, V> where
1532+
K: Borrow<Q>,
1533+
{
15321534
// _______________
15331535
// |_1_|_3_|_5_|_7_|
15341536
// | | | | |
@@ -1556,7 +1558,9 @@ macro_rules! node_slice_impl {
15561558
}
15571559

15581560
/// Returns a sub-slice with elements up to and including `max_key`.
1559-
pub fn slice_to(self, max_key: &K) -> $NodeSlice<'a, K, V> {
1561+
pub fn slice_to<Q: ?Sized + Ord>(self, max_key: &Q) -> $NodeSlice<'a, K, V> where
1562+
K: Borrow<Q>,
1563+
{
15601564
// _______________
15611565
// |_1_|_3_|_5_|_7_|
15621566
// | | | | |

src/libcollections/btree/set.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,11 @@ impl<T: Ord> BTreeSet<T> {
158158
/// ```
159159
#[unstable(feature = "btree_range",
160160
reason = "matches collection reform specification, waiting for dust to settle")]
161-
pub fn range<'a>(&'a self, min: Bound<&T>, max: Bound<&T>) -> Range<'a, T> {
161+
pub fn range<'a, Min: ?Sized + Ord = T, Max: ?Sized + Ord = T>(&'a self, min: Bound<&Min>,
162+
max: Bound<&Max>)
163+
-> Range<'a, T> where
164+
T: Borrow<Min> + Borrow<Max>,
165+
{
162166
fn first<A, B>((a, _): (A, B)) -> A { a }
163167
let first: fn((&'a T, &'a ())) -> &'a T = first; // coerce to fn pointer
164168

0 commit comments

Comments
 (0)