Skip to content

Commit 8ee7f33

Browse files
committed
expose comparable in btreemap/btreeset bounds
1 parent 38aeaa0 commit 8ee7f33

File tree

6 files changed

+37
-57
lines changed

6 files changed

+37
-57
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};
@@ -316,8 +315,7 @@ impl<K, A: Allocator + Clone> BTreeMap<K, SetValZST, A> {
316315

317316
pub(super) fn get_or_insert_with<Q: ?Sized, F>(&mut self, q: &Q, f: F) -> &K
318317
where
319-
K: Borrow<Q> + Ord,
320-
Q: Ord,
318+
K: Comparable<Q> + Ord,
321319
F: FnOnce(&Q) -> K,
322320
{
323321
let (map, dormant_map) = DormantMutRef::new(self);
@@ -327,7 +325,7 @@ impl<K, A: Allocator + Clone> BTreeMap<K, SetValZST, A> {
327325
Found(handle) => handle.into_kv_mut().0,
328326
GoDown(handle) => {
329327
let key = f(q);
330-
assert!(*key.borrow() == *q, "new value is not equal");
328+
assert!(key.equivalent(q), "new value is not equal");
331329
VacantEntry {
332330
key,
333331
handle: Some(handle),
@@ -694,8 +692,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
694692
#[stable(feature = "rust1", since = "1.0.0")]
695693
pub fn get<Q: ?Sized>(&self, key: &Q) -> Option<&V>
696694
where
697-
K: Borrow<Q> + Ord,
698-
Q: Ord,
695+
K: Comparable<Q> + Ord,
699696
{
700697
let root_node = self.root.as_ref()?.reborrow();
701698
match root_node.search_tree(key) {
@@ -760,8 +757,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
760757
#[stable(feature = "map_get_key_value", since = "1.40.0")]
761758
pub fn get_key_value<Q: ?Sized>(&self, k: &Q) -> Option<(&K, &V)>
762759
where
763-
K: Borrow<Q> + Ord,
764-
Q: Ord,
760+
K: Comparable<Q> + Ord,
765761
{
766762
let root_node = self.root.as_ref()?.reborrow();
767763
match root_node.search_tree(k) {
@@ -956,8 +952,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
956952
#[cfg_attr(not(test), rustc_diagnostic_item = "btreemap_contains_key")]
957953
pub fn contains_key<Q: ?Sized>(&self, key: &Q) -> bool
958954
where
959-
K: Borrow<Q> + Ord,
960-
Q: Ord,
955+
K: Comparable<Q> + Ord,
961956
{
962957
self.get(key).is_some()
963958
}
@@ -983,8 +978,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
983978
#[stable(feature = "rust1", since = "1.0.0")]
984979
pub fn get_mut<Q: ?Sized>(&mut self, key: &Q) -> Option<&mut V>
985980
where
986-
K: Borrow<Q> + Ord,
987-
Q: Ord,
981+
K: Comparable<Q> + Ord,
988982
{
989983
let root_node = self.root.as_mut()?.borrow_mut();
990984
match root_node.search_tree(key) {
@@ -1085,8 +1079,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
10851079
#[rustc_confusables("delete", "take")]
10861080
pub fn remove<Q: ?Sized>(&mut self, key: &Q) -> Option<V>
10871081
where
1088-
K: Borrow<Q> + Ord,
1089-
Q: Ord,
1082+
K: Comparable<Q> + Ord,
10901083
{
10911084
self.remove_entry(key).map(|(_, v)| v)
10921085
}
@@ -1110,8 +1103,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
11101103
#[stable(feature = "btreemap_remove_entry", since = "1.45.0")]
11111104
pub fn remove_entry<Q: ?Sized>(&mut self, key: &Q) -> Option<(K, V)>
11121105
where
1113-
K: Borrow<Q> + Ord,
1114-
Q: Ord,
1106+
K: Comparable<Q> + Ord,
11151107
{
11161108
let (map, dormant_map) = DormantMutRef::new(self);
11171109
let root_node = map.root.as_mut()?.borrow_mut();
@@ -1244,7 +1236,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
12441236
pub fn range<T: ?Sized, R>(&self, range: R) -> Range<'_, K, V>
12451237
where
12461238
T: Ord,
1247-
K: Borrow<T> + Ord,
1239+
K: Comparable<T>,
12481240
R: RangeBounds<T>,
12491241
{
12501242
if let Some(root) = &self.root {
@@ -1284,7 +1276,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
12841276
pub fn range_mut<T: ?Sized, R>(&mut self, range: R) -> RangeMut<'_, K, V>
12851277
where
12861278
T: Ord,
1287-
K: Borrow<T> + Ord,
1279+
K: Comparable<T>,
12881280
R: RangeBounds<T>,
12891281
{
12901282
if let Some(root) = &mut self.root {
@@ -1372,9 +1364,9 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
13721364
/// assert_eq!(b[&41], "e");
13731365
/// ```
13741366
#[stable(feature = "btree_split_off", since = "1.11.0")]
1375-
pub fn split_off<Q: ?Sized + Ord>(&mut self, key: &Q) -> Self
1367+
pub fn split_off<Q: ?Sized>(&mut self, key: &Q) -> Self
13761368
where
1377-
K: Borrow<Q> + Ord,
1369+
K: Comparable<Q> + Ord,
13781370
A: Clone,
13791371
{
13801372
if self.is_empty() {
@@ -2424,8 +2416,7 @@ impl<K: Debug, V: Debug, A: Allocator + Clone> Debug for BTreeMap<K, V, A> {
24242416
#[stable(feature = "rust1", since = "1.0.0")]
24252417
impl<K, Q: ?Sized, V, A: Allocator + Clone> Index<&Q> for BTreeMap<K, V, A>
24262418
where
2427-
K: Borrow<Q> + Ord,
2428-
Q: Ord,
2419+
K: Comparable<Q> + Ord,
24292420
{
24302421
type Output = V;
24312422

@@ -2678,8 +2669,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
26782669
#[unstable(feature = "btree_cursors", issue = "107540")]
26792670
pub fn lower_bound<Q: ?Sized>(&self, bound: Bound<&Q>) -> Cursor<'_, K, V>
26802671
where
2681-
K: Borrow<Q> + Ord,
2682-
Q: Ord,
2672+
K: Comparable<Q> + Ord,
26832673
{
26842674
let root_node = match self.root.as_ref() {
26852675
None => return Cursor { current: None, root: None },
@@ -2731,8 +2721,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
27312721
#[unstable(feature = "btree_cursors", issue = "107540")]
27322722
pub fn lower_bound_mut<Q: ?Sized>(&mut self, bound: Bound<&Q>) -> CursorMut<'_, K, V, A>
27332723
where
2734-
K: Borrow<Q> + Ord,
2735-
Q: Ord,
2724+
K: Comparable<Q> + Ord,
27362725
{
27372726
let (root, dormant_root) = DormantMutRef::new(&mut self.root);
27382727
let root_node = match root.as_mut() {
@@ -2801,8 +2790,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
28012790
#[unstable(feature = "btree_cursors", issue = "107540")]
28022791
pub fn upper_bound<Q: ?Sized>(&self, bound: Bound<&Q>) -> Cursor<'_, K, V>
28032792
where
2804-
K: Borrow<Q> + Ord,
2805-
Q: Ord,
2793+
K: Comparable<Q> + Ord,
28062794
{
28072795
let root_node = match self.root.as_ref() {
28082796
None => return Cursor { current: None, root: None },
@@ -2854,8 +2842,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
28542842
#[unstable(feature = "btree_cursors", issue = "107540")]
28552843
pub fn upper_bound_mut<Q: ?Sized>(&mut self, bound: Bound<&Q>) -> CursorMut<'_, K, V, A>
28562844
where
2857-
K: Borrow<Q> + Ord,
2858-
Q: Ord,
2845+
K: Comparable<Q> + Ord,
28592846
{
28602847
let (root, dormant_root) = DormantMutRef::new(&mut self.root);
28612848
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::{max, min, Comparable};
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 & 3 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,13 @@ 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>(
34+
pub(super) fn split_off<Q: ?Sized, A: Allocator + Clone>(
3535
&mut self,
3636
key: &Q,
3737
alloc: A,
3838
) -> Self
3939
where
40-
K: Borrow<Q>,
40+
K: Comparable<Q>,
4141
{
4242
let left_root = self;
4343
let mut right_root = Root::new_pillar(left_root.height(), alloc.clone());

library/alloctests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#![feature(allocator_api)]
1919
#![feature(array_into_iter_constructors)]
2020
#![feature(assert_matches)]
21+
#![feature(comparable_trait)]
2122
#![feature(core_intrinsics)]
2223
#![feature(exact_size_is_empty)]
2324
#![feature(extend_one)]

library/std/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@
329329
#![feature(bstr_internals)]
330330
#![feature(char_internals)]
331331
#![feature(clone_to_uninit)]
332+
#![feature(comparable_trait)]
332333
#![feature(core_intrinsics)]
333334
#![feature(core_io_borrowed_buf)]
334335
#![feature(duration_constants)]

0 commit comments

Comments
 (0)