Skip to content

Commit f080fdf

Browse files
committed
expose comparable in btreemap/btreeset bounds
1 parent 59d683c commit f080fdf

4 files changed

Lines changed: 60 additions & 86 deletions

File tree

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

Lines changed: 32 additions & 45 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};
@@ -333,20 +332,19 @@ impl<K, A: Allocator + Clone> BTreeMap<K, SetValZST, A> {
333332
}
334333
}
335334

336-
pub(super) fn get_or_insert_with<Q: ?Sized, F>(&mut self, q: &Q, f: F) -> &K
335+
pub(super) fn get_or_insert_with<Q: Clone, F>(&mut self, q: Q, f: F) -> &K
337336
where
338-
K: Borrow<Q> + Ord,
339-
Q: Ord,
340-
F: FnOnce(&Q) -> K,
337+
K: Comparable<Q> + Ord,
338+
F: FnOnce(Q) -> K,
341339
{
342340
let (map, dormant_map) = DormantMutRef::new(self);
343341
let root_node =
344342
map.root.get_or_insert_with(|| Root::new((*map.alloc).clone())).borrow_mut();
345-
match root_node.search_tree(q) {
343+
match root_node.search_tree(q.clone()) {
346344
Found(handle) => handle.into_kv_mut().0,
347345
GoDown(handle) => {
348-
let key = f(q);
349-
assert!(*key.borrow() == *q, "new value is not equal");
346+
let key = f(q.clone());
347+
assert!(key.compare(q) == Ordering::Equal, "new value is not equal");
350348
VacantEntry {
351349
key,
352350
handle: Some(handle),
@@ -716,10 +714,9 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
716714
/// assert_eq!(map.get(&2), None);
717715
/// ```
718716
#[stable(feature = "rust1", since = "1.0.0")]
719-
pub fn get<Q: ?Sized>(&self, key: &Q) -> Option<&V>
717+
pub fn get<Q: Clone>(&self, key: Q) -> Option<&V>
720718
where
721-
K: Borrow<Q> + Ord,
722-
Q: Ord,
719+
K: Comparable<Q> + Ord,
723720
{
724721
let root_node = self.root.as_ref()?.reborrow();
725722
match root_node.search_tree(key) {
@@ -782,10 +779,9 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
782779
/// assert_eq!(map.get_key_value(&p), None);
783780
/// ```
784781
#[stable(feature = "map_get_key_value", since = "1.40.0")]
785-
pub fn get_key_value<Q: ?Sized>(&self, k: &Q) -> Option<(&K, &V)>
782+
pub fn get_key_value<Q: Clone>(&self, k: Q) -> Option<(&K, &V)>
786783
where
787-
K: Borrow<Q> + Ord,
788-
Q: Ord,
784+
K: Comparable<Q> + Ord,
789785
{
790786
let root_node = self.root.as_ref()?.reborrow();
791787
match root_node.search_tree(k) {
@@ -978,10 +974,9 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
978974
/// ```
979975
#[stable(feature = "rust1", since = "1.0.0")]
980976
#[cfg_attr(not(test), rustc_diagnostic_item = "btreemap_contains_key")]
981-
pub fn contains_key<Q: ?Sized>(&self, key: &Q) -> bool
977+
pub fn contains_key<Q: Clone>(&self, key: Q) -> bool
982978
where
983-
K: Borrow<Q> + Ord,
984-
Q: Ord,
979+
K: Comparable<Q> + Ord,
985980
{
986981
self.get(key).is_some()
987982
}
@@ -1005,10 +1000,9 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
10051000
/// ```
10061001
// See `get` for implementation notes, this is basically a copy-paste with mut's added
10071002
#[stable(feature = "rust1", since = "1.0.0")]
1008-
pub fn get_mut<Q: ?Sized>(&mut self, key: &Q) -> Option<&mut V>
1003+
pub fn get_mut<Q: Clone>(&mut self, key: Q) -> Option<&mut V>
10091004
where
1010-
K: Borrow<Q> + Ord,
1011-
Q: Ord,
1005+
K: Comparable<Q> + Ord,
10121006
{
10131007
let root_node = self.root.as_mut()?.borrow_mut();
10141008
match root_node.search_tree(key) {
@@ -1107,10 +1101,9 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
11071101
/// ```
11081102
#[stable(feature = "rust1", since = "1.0.0")]
11091103
#[rustc_confusables("delete", "take")]
1110-
pub fn remove<Q: ?Sized>(&mut self, key: &Q) -> Option<V>
1104+
pub fn remove<Q: Clone>(&mut self, key: Q) -> Option<V>
11111105
where
1112-
K: Borrow<Q> + Ord,
1113-
Q: Ord,
1106+
K: Comparable<Q> + Ord,
11141107
{
11151108
self.remove_entry(key).map(|(_, v)| v)
11161109
}
@@ -1132,10 +1125,9 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
11321125
/// assert_eq!(map.remove_entry(&1), None);
11331126
/// ```
11341127
#[stable(feature = "btreemap_remove_entry", since = "1.45.0")]
1135-
pub fn remove_entry<Q: ?Sized>(&mut self, key: &Q) -> Option<(K, V)>
1128+
pub fn remove_entry<Q: Clone>(&mut self, key: Q) -> Option<(K, V)>
11361129
where
1137-
K: Borrow<Q> + Ord,
1138-
Q: Ord,
1130+
K: Comparable<Q> + Ord,
11391131
{
11401132
let (map, dormant_map) = DormantMutRef::new(self);
11411133
let root_node = map.root.as_mut()?.borrow_mut();
@@ -1410,7 +1402,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
14101402
pub fn range<T: ?Sized, R>(&self, range: R) -> Range<'_, K, V>
14111403
where
14121404
T: Ord,
1413-
K: Borrow<T> + Ord,
1405+
K: Comparable<&T>,
14141406
R: RangeBounds<T>,
14151407
{
14161408
if let Some(root) = &self.root {
@@ -1450,7 +1442,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
14501442
pub fn range_mut<T: ?Sized, R>(&mut self, range: R) -> RangeMut<'_, K, V>
14511443
where
14521444
T: Ord,
1453-
K: Borrow<T> + Ord,
1445+
K: Comparable<&T>,
14541446
R: RangeBounds<T>,
14551447
{
14561448
if let Some(root) = &mut self.root {
@@ -1539,9 +1531,9 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
15391531
/// assert_eq!(b[&41], "e");
15401532
/// ```
15411533
#[stable(feature = "btree_split_off", since = "1.11.0")]
1542-
pub fn split_off<Q: ?Sized + Ord>(&mut self, key: &Q) -> Self
1534+
pub fn split_off<Q: Clone>(&mut self, key: Q) -> Self
15431535
where
1544-
K: Borrow<Q> + Ord,
1536+
K: Comparable<Q> + Ord,
15451537
A: Clone,
15461538
{
15471539
if self.is_empty() {
@@ -2618,8 +2610,7 @@ impl<K: Debug, V: Debug, A: Allocator + Clone> Debug for BTreeMap<K, V, A> {
26182610
#[stable(feature = "rust1", since = "1.0.0")]
26192611
impl<K, Q: ?Sized, V, A: Allocator + Clone> Index<&Q> for BTreeMap<K, V, A>
26202612
where
2621-
K: Borrow<Q> + Ord,
2622-
Q: Ord,
2613+
K: Comparable<&Q> + Ord,
26232614
{
26242615
type Output = V;
26252616

@@ -2870,10 +2861,9 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
28702861
/// assert_eq!(cursor.peek_next(), Some((&1, &"a")));
28712862
/// ```
28722863
#[unstable(feature = "btree_cursors", issue = "107540")]
2873-
pub fn lower_bound<Q: ?Sized>(&self, bound: Bound<&Q>) -> Cursor<'_, K, V>
2864+
pub fn lower_bound<Q: Clone>(&self, bound: Bound<Q>) -> Cursor<'_, K, V>
28742865
where
2875-
K: Borrow<Q> + Ord,
2876-
Q: Ord,
2866+
K: Comparable<Q> + Ord,
28772867
{
28782868
let root_node = match self.root.as_ref() {
28792869
None => return Cursor { current: None, root: None },
@@ -2923,10 +2913,9 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
29232913
/// assert_eq!(cursor.peek_next(), Some((&1, &mut "a")));
29242914
/// ```
29252915
#[unstable(feature = "btree_cursors", issue = "107540")]
2926-
pub fn lower_bound_mut<Q: ?Sized>(&mut self, bound: Bound<&Q>) -> CursorMut<'_, K, V, A>
2916+
pub fn lower_bound_mut<Q: Clone>(&mut self, bound: Bound<Q>) -> CursorMut<'_, K, V, A>
29272917
where
2928-
K: Borrow<Q> + Ord,
2929-
Q: Ord,
2918+
K: Comparable<Q> + Ord,
29302919
{
29312920
let (root, dormant_root) = DormantMutRef::new(&mut self.root);
29322921
let root_node = match root.as_mut() {
@@ -2993,10 +2982,9 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
29932982
/// assert_eq!(cursor.peek_next(), None);
29942983
/// ```
29952984
#[unstable(feature = "btree_cursors", issue = "107540")]
2996-
pub fn upper_bound<Q: ?Sized>(&self, bound: Bound<&Q>) -> Cursor<'_, K, V>
2985+
pub fn upper_bound<Q: Clone>(&self, bound: Bound<Q>) -> Cursor<'_, K, V>
29972986
where
2998-
K: Borrow<Q> + Ord,
2999-
Q: Ord,
2987+
K: Comparable<Q> + Ord,
30002988
{
30012989
let root_node = match self.root.as_ref() {
30022990
None => return Cursor { current: None, root: None },
@@ -3046,10 +3034,9 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
30463034
/// assert_eq!(cursor.peek_next(), None);
30473035
/// ```
30483036
#[unstable(feature = "btree_cursors", issue = "107540")]
3049-
pub fn upper_bound_mut<Q: ?Sized>(&mut self, bound: Bound<&Q>) -> CursorMut<'_, K, V, A>
3037+
pub fn upper_bound_mut<Q: Clone>(&mut self, bound: Bound<Q>) -> CursorMut<'_, K, V, A>
30503038
where
3051-
K: Borrow<Q> + Ord,
3052-
Q: Ord,
3039+
K: Comparable<Q> + Ord,
30533040
{
30543041
let (root, dormant_root) = DormantMutRef::new(&mut self.root);
30553042
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;
2+
use std::borrow::Borrow;
23
use std::ops::Bound::{Excluded, Included, Unbounded};
34
use std::panic::{AssertUnwindSafe, catch_unwind};
45
use std::sync::atomic::AtomicUsize;

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

Lines changed: 23 additions & 33 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, TrustedLen};
@@ -397,7 +396,7 @@ impl<T, A: Allocator + Clone> BTreeSet<T, A> {
397396
pub fn range<K: ?Sized, R>(&self, range: R) -> Range<'_, T>
398397
where
399398
K: Ord,
400-
T: Borrow<K> + Ord,
399+
T: Comparable<&K> + Ord,
401400
R: RangeBounds<K>,
402401
{
403402
Range { iter: self.map.range(range) }
@@ -602,10 +601,9 @@ impl<T, A: Allocator + Clone> BTreeSet<T, A> {
602601
/// assert_eq!(set.contains(&4), false);
603602
/// ```
604603
#[stable(feature = "rust1", since = "1.0.0")]
605-
pub fn contains<Q: ?Sized>(&self, value: &Q) -> bool
604+
pub fn contains<Q: Clone>(&self, value: Q) -> bool
606605
where
607-
T: Borrow<Q> + Ord,
608-
Q: Ord,
606+
T: Comparable<Q> + Ord,
609607
{
610608
self.map.contains_key(value)
611609
}
@@ -627,10 +625,9 @@ impl<T, A: Allocator + Clone> BTreeSet<T, A> {
627625
/// assert_eq!(set.get(&4), None);
628626
/// ```
629627
#[stable(feature = "set_recovery", since = "1.9.0")]
630-
pub fn get<Q: ?Sized>(&self, value: &Q) -> Option<&T>
628+
pub fn get<Q: Clone>(&self, value: Q) -> Option<&T>
631629
where
632-
T: Borrow<Q> + Ord,
633-
Q: Ord,
630+
T: Comparable<Q> + Ord,
634631
{
635632
self.map.get_key_value(value).map(|(k, _)| k)
636633
}
@@ -974,11 +971,10 @@ impl<T, A: Allocator + Clone> BTreeSet<T, A> {
974971
/// ```
975972
#[inline]
976973
#[unstable(feature = "btree_set_entry", issue = "133549")]
977-
pub fn get_or_insert_with<Q: ?Sized, F>(&mut self, value: &Q, f: F) -> &T
974+
pub fn get_or_insert_with<Q: Clone, F>(&mut self, value: Q, f: F) -> &T
978975
where
979-
T: Borrow<Q> + Ord,
980-
Q: Ord,
981-
F: FnOnce(&Q) -> T,
976+
T: Comparable<Q> + Ord,
977+
F: FnOnce(Q) -> T,
982978
{
983979
self.map.get_or_insert_with(value, f)
984980
}
@@ -1049,10 +1045,9 @@ impl<T, A: Allocator + Clone> BTreeSet<T, A> {
10491045
/// assert_eq!(set.remove(&2), false);
10501046
/// ```
10511047
#[stable(feature = "rust1", since = "1.0.0")]
1052-
pub fn remove<Q: ?Sized>(&mut self, value: &Q) -> bool
1048+
pub fn remove<Q: Clone>(&mut self, value: Q) -> bool
10531049
where
1054-
T: Borrow<Q> + Ord,
1055-
Q: Ord,
1050+
T: Comparable<Q> + Ord,
10561051
{
10571052
self.map.remove(value).is_some()
10581053
}
@@ -1074,10 +1069,9 @@ impl<T, A: Allocator + Clone> BTreeSet<T, A> {
10741069
/// assert_eq!(set.take(&2), None);
10751070
/// ```
10761071
#[stable(feature = "set_recovery", since = "1.9.0")]
1077-
pub fn take<Q: ?Sized>(&mut self, value: &Q) -> Option<T>
1072+
pub fn take<Q: Clone>(&mut self, value: Q) -> Option<T>
10781073
where
1079-
T: Borrow<Q> + Ord,
1080-
Q: Ord,
1074+
T: Comparable<Q> + Ord,
10811075
{
10821076
self.map.remove_entry(value).map(|(k, _)| k)
10831077
}
@@ -1173,9 +1167,9 @@ impl<T, A: Allocator + Clone> BTreeSet<T, A> {
11731167
/// assert!(b.contains(&41));
11741168
/// ```
11751169
#[stable(feature = "btree_split_off", since = "1.11.0")]
1176-
pub fn split_off<Q: ?Sized + Ord>(&mut self, value: &Q) -> Self
1170+
pub fn split_off<Q: Clone>(&mut self, value: Q) -> Self
11771171
where
1178-
T: Borrow<Q> + Ord,
1172+
T: Comparable<Q> + Ord,
11791173
A: Clone,
11801174
{
11811175
BTreeSet { map: self.map.split_off(value) }
@@ -1327,10 +1321,9 @@ impl<T, A: Allocator + Clone> BTreeSet<T, A> {
13271321
/// assert_eq!(cursor.peek_next(), Some(&1));
13281322
/// ```
13291323
#[unstable(feature = "btree_cursors", issue = "107540")]
1330-
pub fn lower_bound<Q: ?Sized>(&self, bound: Bound<&Q>) -> Cursor<'_, T>
1324+
pub fn lower_bound<Q: Clone>(&self, bound: Bound<Q>) -> Cursor<'_, T>
13311325
where
1332-
T: Borrow<Q> + Ord,
1333-
Q: Ord,
1326+
T: Comparable<Q> + Ord,
13341327
{
13351328
Cursor { inner: self.map.lower_bound(bound) }
13361329
}
@@ -1370,10 +1363,9 @@ impl<T, A: Allocator + Clone> BTreeSet<T, A> {
13701363
/// assert_eq!(cursor.peek_next(), Some(&1));
13711364
/// ```
13721365
#[unstable(feature = "btree_cursors", issue = "107540")]
1373-
pub fn lower_bound_mut<Q: ?Sized>(&mut self, bound: Bound<&Q>) -> CursorMut<'_, T, A>
1366+
pub fn lower_bound_mut<Q: Clone>(&mut self, bound: Bound<Q>) -> CursorMut<'_, T, A>
13741367
where
1375-
T: Borrow<Q> + Ord,
1376-
Q: Ord,
1368+
T: Comparable<Q> + Ord,
13771369
{
13781370
CursorMut { inner: self.map.lower_bound_mut(bound) }
13791371
}
@@ -1413,10 +1405,9 @@ impl<T, A: Allocator + Clone> BTreeSet<T, A> {
14131405
/// assert_eq!(cursor.peek_next(), None);
14141406
/// ```
14151407
#[unstable(feature = "btree_cursors", issue = "107540")]
1416-
pub fn upper_bound<Q: ?Sized>(&self, bound: Bound<&Q>) -> Cursor<'_, T>
1408+
pub fn upper_bound<Q: Clone>(&self, bound: Bound<Q>) -> Cursor<'_, T>
14171409
where
1418-
T: Borrow<Q> + Ord,
1419-
Q: Ord,
1410+
T: Comparable<Q> + Ord,
14201411
{
14211412
Cursor { inner: self.map.upper_bound(bound) }
14221413
}
@@ -1456,10 +1447,9 @@ impl<T, A: Allocator + Clone> BTreeSet<T, A> {
14561447
/// assert_eq!(cursor.peek_next(), None);
14571448
/// ```
14581449
#[unstable(feature = "btree_cursors", issue = "107540")]
1459-
pub fn upper_bound_mut<Q: ?Sized>(&mut self, bound: Bound<&Q>) -> CursorMut<'_, T, A>
1450+
pub fn upper_bound_mut<Q: Clone>(&mut self, bound: Bound<Q>) -> CursorMut<'_, T, A>
14601451
where
1461-
T: Borrow<Q> + Ord,
1462-
Q: Ord,
1452+
T: Comparable<Q> + Ord,
14631453
{
14641454
CursorMut { inner: self.map.upper_bound_mut(bound) }
14651455
}

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

Lines changed: 4 additions & 8 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,21 +31,17 @@ 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: Clone, 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());
4440
let mut left_node = left_root.borrow_mut();
4541
let mut right_node = right_root.borrow_mut();
4642

4743
loop {
48-
let mut split_edge = match left_node.search_node(key) {
44+
let mut split_edge = match left_node.search_node(key.clone()) {
4945
// key is going to the right tree
5046
Found(kv) => kv.left_edge(),
5147
GoDown(edge) => edge,

0 commit comments

Comments
 (0)