Skip to content

Commit 578b1b4

Browse files
committed
expose comparable in btreemap/btreeset bounds
1 parent 7dd8a84 commit 578b1b4

File tree

4 files changed

+35
-61
lines changed

4 files changed

+35
-61
lines changed

library/alloc/src/collections/btree/map.rs

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use core::borrow::Borrow;
2-
use core::cmp::Ordering;
1+
use core::cmp::{Comparable, Ordering};
32
use core::error::Error;
43
use core::fmt::{self, Debug};
54
use core::hash::{Hash, Hasher};
@@ -332,8 +331,7 @@ impl<K, A: Allocator + Clone> BTreeMap<K, SetValZST, A> {
332331

333332
pub(super) fn get_or_insert_with<Q: ?Sized, F>(&mut self, q: &Q, f: F) -> &K
334333
where
335-
K: Borrow<Q> + Ord,
336-
Q: Ord,
334+
K: Comparable<Q> + Ord,
337335
F: FnOnce(&Q) -> K,
338336
{
339337
let (map, dormant_map) = DormantMutRef::new(self);
@@ -343,7 +341,7 @@ impl<K, A: Allocator + Clone> BTreeMap<K, SetValZST, A> {
343341
Found(handle) => handle.into_kv_mut().0,
344342
GoDown(handle) => {
345343
let key = f(q);
346-
assert!(*key.borrow() == *q, "new value is not equal");
344+
assert!(key.equivalent(q), "new value is not equal");
347345
VacantEntry {
348346
key,
349347
handle: Some(handle),
@@ -710,8 +708,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
710708
#[stable(feature = "rust1", since = "1.0.0")]
711709
pub fn get<Q: ?Sized>(&self, key: &Q) -> Option<&V>
712710
where
713-
K: Borrow<Q> + Ord,
714-
Q: Ord,
711+
K: Comparable<Q> + Ord,
715712
{
716713
let root_node = self.root.as_ref()?.reborrow();
717714
match root_node.search_tree(key) {
@@ -776,8 +773,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
776773
#[stable(feature = "map_get_key_value", since = "1.40.0")]
777774
pub fn get_key_value<Q: ?Sized>(&self, k: &Q) -> Option<(&K, &V)>
778775
where
779-
K: Borrow<Q> + Ord,
780-
Q: Ord,
776+
K: Comparable<Q> + Ord,
781777
{
782778
let root_node = self.root.as_ref()?.reborrow();
783779
match root_node.search_tree(k) {
@@ -972,8 +968,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
972968
#[cfg_attr(not(test), rustc_diagnostic_item = "btreemap_contains_key")]
973969
pub fn contains_key<Q: ?Sized>(&self, key: &Q) -> bool
974970
where
975-
K: Borrow<Q> + Ord,
976-
Q: Ord,
971+
K: Comparable<Q> + Ord,
977972
{
978973
self.get(key).is_some()
979974
}
@@ -999,8 +994,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
999994
#[stable(feature = "rust1", since = "1.0.0")]
1000995
pub fn get_mut<Q: ?Sized>(&mut self, key: &Q) -> Option<&mut V>
1001996
where
1002-
K: Borrow<Q> + Ord,
1003-
Q: Ord,
997+
K: Comparable<Q> + Ord,
1004998
{
1005999
let root_node = self.root.as_mut()?.borrow_mut();
10061000
match root_node.search_tree(key) {
@@ -1101,8 +1095,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
11011095
#[rustc_confusables("delete", "take")]
11021096
pub fn remove<Q: ?Sized>(&mut self, key: &Q) -> Option<V>
11031097
where
1104-
K: Borrow<Q> + Ord,
1105-
Q: Ord,
1098+
K: Comparable<Q> + Ord,
11061099
{
11071100
self.remove_entry(key).map(|(_, v)| v)
11081101
}
@@ -1126,8 +1119,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
11261119
#[stable(feature = "btreemap_remove_entry", since = "1.45.0")]
11271120
pub fn remove_entry<Q: ?Sized>(&mut self, key: &Q) -> Option<(K, V)>
11281121
where
1129-
K: Borrow<Q> + Ord,
1130-
Q: Ord,
1122+
K: Comparable<Q> + Ord,
11311123
{
11321124
let (map, dormant_map) = DormantMutRef::new(self);
11331125
let root_node = map.root.as_mut()?.borrow_mut();
@@ -1260,7 +1252,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
12601252
pub fn range<T: ?Sized, R>(&self, range: R) -> Range<'_, K, V>
12611253
where
12621254
T: Ord,
1263-
K: Borrow<T> + Ord,
1255+
K: Comparable<T>,
12641256
R: RangeBounds<T>,
12651257
{
12661258
if let Some(root) = &self.root {
@@ -1300,7 +1292,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
13001292
pub fn range_mut<T: ?Sized, R>(&mut self, range: R) -> RangeMut<'_, K, V>
13011293
where
13021294
T: Ord,
1303-
K: Borrow<T> + Ord,
1295+
K: Comparable<T>,
13041296
R: RangeBounds<T>,
13051297
{
13061298
if let Some(root) = &mut self.root {
@@ -1388,9 +1380,9 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
13881380
/// assert_eq!(b[&41], "e");
13891381
/// ```
13901382
#[stable(feature = "btree_split_off", since = "1.11.0")]
1391-
pub fn split_off<Q: ?Sized + Ord>(&mut self, key: &Q) -> Self
1383+
pub fn split_off<Q: ?Sized>(&mut self, key: &Q) -> Self
13921384
where
1393-
K: Borrow<Q> + Ord,
1385+
K: Comparable<Q> + Ord,
13941386
A: Clone,
13951387
{
13961388
if self.is_empty() {
@@ -2440,8 +2432,7 @@ impl<K: Debug, V: Debug, A: Allocator + Clone> Debug for BTreeMap<K, V, A> {
24402432
#[stable(feature = "rust1", since = "1.0.0")]
24412433
impl<K, Q: ?Sized, V, A: Allocator + Clone> Index<&Q> for BTreeMap<K, V, A>
24422434
where
2443-
K: Borrow<Q> + Ord,
2444-
Q: Ord,
2435+
K: Comparable<Q> + Ord,
24452436
{
24462437
type Output = V;
24472438

@@ -2694,8 +2685,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
26942685
#[unstable(feature = "btree_cursors", issue = "107540")]
26952686
pub fn lower_bound<Q: ?Sized>(&self, bound: Bound<&Q>) -> Cursor<'_, K, V>
26962687
where
2697-
K: Borrow<Q> + Ord,
2698-
Q: Ord,
2688+
K: Comparable<Q> + Ord,
26992689
{
27002690
let root_node = match self.root.as_ref() {
27012691
None => return Cursor { current: None, root: None },
@@ -2747,8 +2737,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
27472737
#[unstable(feature = "btree_cursors", issue = "107540")]
27482738
pub fn lower_bound_mut<Q: ?Sized>(&mut self, bound: Bound<&Q>) -> CursorMut<'_, K, V, A>
27492739
where
2750-
K: Borrow<Q> + Ord,
2751-
Q: Ord,
2740+
K: Comparable<Q> + Ord,
27522741
{
27532742
let (root, dormant_root) = DormantMutRef::new(&mut self.root);
27542743
let root_node = match root.as_mut() {
@@ -2817,8 +2806,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
28172806
#[unstable(feature = "btree_cursors", issue = "107540")]
28182807
pub fn upper_bound<Q: ?Sized>(&self, bound: Bound<&Q>) -> Cursor<'_, K, V>
28192808
where
2820-
K: Borrow<Q> + Ord,
2821-
Q: Ord,
2809+
K: Comparable<Q> + Ord,
28222810
{
28232811
let root_node = match self.root.as_ref() {
28242812
None => return Cursor { current: None, root: None },
@@ -2870,8 +2858,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
28702858
#[unstable(feature = "btree_cursors", issue = "107540")]
28712859
pub fn upper_bound_mut<Q: ?Sized>(&mut self, bound: Bound<&Q>) -> CursorMut<'_, K, V, A>
28722860
where
2873-
K: Borrow<Q> + Ord,
2874-
Q: Ord,
2861+
K: Comparable<Q> + Ord,
28752862
{
28762863
let (root, dormant_root) = DormantMutRef::new(&mut self.root);
28772864
let root_node = match root.as_mut() {

library/alloc/src/collections/btree/map/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use core::assert_matches::assert_matches;
2+
use std::borrow::Borrow;
23
use std::iter;
34
use std::ops::Bound::{Excluded, Included, Unbounded};
45
use std::panic::{AssertUnwindSafe, catch_unwind};

library/alloc/src/collections/btree/set.rs

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use core::borrow::Borrow;
21
use core::cmp::Ordering::{self, Equal, Greater, Less};
3-
use core::cmp::{max, min};
2+
use core::cmp::{Comparable, max, min};
43
use core::fmt::{self, Debug};
54
use core::hash::{Hash, Hasher};
65
use core::iter::{FusedIterator, Peekable};
@@ -396,7 +395,7 @@ impl<T, A: Allocator + Clone> BTreeSet<T, A> {
396395
pub fn range<K: ?Sized, R>(&self, range: R) -> Range<'_, T>
397396
where
398397
K: Ord,
399-
T: Borrow<K> + Ord,
398+
T: Comparable<K> + Ord,
400399
R: RangeBounds<K>,
401400
{
402401
Range { iter: self.map.range(range) }
@@ -611,8 +610,7 @@ impl<T, A: Allocator + Clone> BTreeSet<T, A> {
611610
#[stable(feature = "rust1", since = "1.0.0")]
612611
pub fn contains<Q: ?Sized>(&self, value: &Q) -> bool
613612
where
614-
T: Borrow<Q> + Ord,
615-
Q: Ord,
613+
T: Comparable<Q> + Ord,
616614
{
617615
self.map.contains_key(value)
618616
}
@@ -636,8 +634,7 @@ impl<T, A: Allocator + Clone> BTreeSet<T, A> {
636634
#[stable(feature = "set_recovery", since = "1.9.0")]
637635
pub fn get<Q: ?Sized>(&self, value: &Q) -> Option<&T>
638636
where
639-
T: Borrow<Q> + Ord,
640-
Q: Ord,
637+
T: Comparable<Q> + Ord,
641638
{
642639
self.map.get_key_value(value).map(|(k, _)| k)
643640
}
@@ -982,8 +979,7 @@ impl<T, A: Allocator + Clone> BTreeSet<T, A> {
982979
#[unstable(feature = "btree_set_entry", issue = "133549")]
983980
pub fn get_or_insert_with<Q: ?Sized, F>(&mut self, value: &Q, f: F) -> &T
984981
where
985-
T: Borrow<Q> + Ord,
986-
Q: Ord,
982+
T: Comparable<Q> + Ord,
987983
F: FnOnce(&Q) -> T,
988984
{
989985
self.map.get_or_insert_with(value, f)
@@ -1057,8 +1053,7 @@ impl<T, A: Allocator + Clone> BTreeSet<T, A> {
10571053
#[stable(feature = "rust1", since = "1.0.0")]
10581054
pub fn remove<Q: ?Sized>(&mut self, value: &Q) -> bool
10591055
where
1060-
T: Borrow<Q> + Ord,
1061-
Q: Ord,
1056+
T: Comparable<Q> + Ord,
10621057
{
10631058
self.map.remove(value).is_some()
10641059
}
@@ -1082,8 +1077,7 @@ impl<T, A: Allocator + Clone> BTreeSet<T, A> {
10821077
#[stable(feature = "set_recovery", since = "1.9.0")]
10831078
pub fn take<Q: ?Sized>(&mut self, value: &Q) -> Option<T>
10841079
where
1085-
T: Borrow<Q> + Ord,
1086-
Q: Ord,
1080+
T: Comparable<Q> + Ord,
10871081
{
10881082
self.map.remove_entry(value).map(|(k, _)| k)
10891083
}
@@ -1179,9 +1173,9 @@ impl<T, A: Allocator + Clone> BTreeSet<T, A> {
11791173
/// assert!(b.contains(&41));
11801174
/// ```
11811175
#[stable(feature = "btree_split_off", since = "1.11.0")]
1182-
pub fn split_off<Q: ?Sized + Ord>(&mut self, value: &Q) -> Self
1176+
pub fn split_off<Q: ?Sized>(&mut self, value: &Q) -> Self
11831177
where
1184-
T: Borrow<Q> + Ord,
1178+
T: Comparable<Q> + Ord,
11851179
A: Clone,
11861180
{
11871181
BTreeSet { map: self.map.split_off(value) }
@@ -1335,8 +1329,7 @@ impl<T, A: Allocator + Clone> BTreeSet<T, A> {
13351329
#[unstable(feature = "btree_cursors", issue = "107540")]
13361330
pub fn lower_bound<Q: ?Sized>(&self, bound: Bound<&Q>) -> Cursor<'_, T>
13371331
where
1338-
T: Borrow<Q> + Ord,
1339-
Q: Ord,
1332+
T: Comparable<Q> + Ord,
13401333
{
13411334
Cursor { inner: self.map.lower_bound(bound) }
13421335
}
@@ -1378,8 +1371,7 @@ impl<T, A: Allocator + Clone> BTreeSet<T, A> {
13781371
#[unstable(feature = "btree_cursors", issue = "107540")]
13791372
pub fn lower_bound_mut<Q: ?Sized>(&mut self, bound: Bound<&Q>) -> CursorMut<'_, T, A>
13801373
where
1381-
T: Borrow<Q> + Ord,
1382-
Q: Ord,
1374+
T: Comparable<Q> + Ord,
13831375
{
13841376
CursorMut { inner: self.map.lower_bound_mut(bound) }
13851377
}
@@ -1421,8 +1413,7 @@ impl<T, A: Allocator + Clone> BTreeSet<T, A> {
14211413
#[unstable(feature = "btree_cursors", issue = "107540")]
14221414
pub fn upper_bound<Q: ?Sized>(&self, bound: Bound<&Q>) -> Cursor<'_, T>
14231415
where
1424-
T: Borrow<Q> + Ord,
1425-
Q: Ord,
1416+
T: Comparable<Q> + Ord,
14261417
{
14271418
Cursor { inner: self.map.upper_bound(bound) }
14281419
}
@@ -1464,8 +1455,7 @@ impl<T, A: Allocator + Clone> BTreeSet<T, A> {
14641455
#[unstable(feature = "btree_cursors", issue = "107540")]
14651456
pub fn upper_bound_mut<Q: ?Sized>(&mut self, bound: Bound<&Q>) -> CursorMut<'_, T, A>
14661457
where
1467-
T: Borrow<Q> + Ord,
1468-
Q: Ord,
1458+
T: Comparable<Q> + Ord,
14691459
{
14701460
CursorMut { inner: self.map.upper_bound_mut(bound) }
14711461
}

library/alloc/src/collections/btree/split.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use core::alloc::Allocator;
2-
use core::borrow::Borrow;
2+
use core::cmp::Comparable;
33

44
use super::node::ForceResult::*;
55
use super::node::Root;
@@ -31,13 +31,9 @@ impl<K, V> Root<K, V> {
3131
/// and if the ordering of `Q` corresponds to that of `K`.
3232
/// If `self` respects all `BTreeMap` tree invariants, then both
3333
/// `self` and the returned tree will respect those invariants.
34-
pub(super) fn split_off<Q: ?Sized + Ord, A: Allocator + Clone>(
35-
&mut self,
36-
key: &Q,
37-
alloc: A,
38-
) -> Self
34+
pub(super) fn split_off<Q: ?Sized, A: Allocator + Clone>(&mut self, key: &Q, alloc: A) -> Self
3935
where
40-
K: Borrow<Q>,
36+
K: Comparable<Q>,
4137
{
4238
let left_root = self;
4339
let mut right_root = Root::new_pillar(left_root.height(), alloc.clone());

0 commit comments

Comments
 (0)