Skip to content

Commit 78be1aa

Browse files
authored
Rollup merge of #81610 - ssomers:btree_emphasize_ord_bound, r=dtolnay
BTreeMap: make Ord bound explicit, compile-test its absence Most `BTreeMap` and `BTreeSet` members are subject to an `Ord` bound but a fair number of methods are not. To better convey and perhaps later tune the `Ord` bound, make it stand out in individual `where` clauses, instead of once far away at the beginning of an `impl` block. This PR does not introduce or remove any bounds. Also adds compilation test cases checking that the bound doesn't creep in unintended on the historically unbounded methods.
2 parents 43b3adb + 1020784 commit 78be1aa

File tree

4 files changed

+191
-54
lines changed

4 files changed

+191
-54
lines changed

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

+70-26
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for RangeMut<'_, K, V> {
460460
}
461461
}
462462

463-
impl<K: Ord, V> BTreeMap<K, V> {
463+
impl<K, V> BTreeMap<K, V> {
464464
/// Makes a new, empty `BTreeMap`.
465465
///
466466
/// Does not allocate anything on its own.
@@ -479,7 +479,10 @@ impl<K: Ord, V> BTreeMap<K, V> {
479479
/// ```
480480
#[stable(feature = "rust1", since = "1.0.0")]
481481
#[rustc_const_unstable(feature = "const_btree_new", issue = "71835")]
482-
pub const fn new() -> BTreeMap<K, V> {
482+
pub const fn new() -> BTreeMap<K, V>
483+
where
484+
K: Ord,
485+
{
483486
BTreeMap { root: None, length: 0 }
484487
}
485488

@@ -498,7 +501,10 @@ impl<K: Ord, V> BTreeMap<K, V> {
498501
/// assert!(a.is_empty());
499502
/// ```
500503
#[stable(feature = "rust1", since = "1.0.0")]
501-
pub fn clear(&mut self) {
504+
pub fn clear(&mut self)
505+
where
506+
K: Ord,
507+
{
502508
*self = BTreeMap::new();
503509
}
504510

@@ -522,7 +528,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
522528
#[stable(feature = "rust1", since = "1.0.0")]
523529
pub fn get<Q: ?Sized>(&self, key: &Q) -> Option<&V>
524530
where
525-
K: Borrow<Q>,
531+
K: Borrow<Q> + Ord,
526532
Q: Ord,
527533
{
528534
let root_node = self.root.as_ref()?.reborrow();
@@ -550,7 +556,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
550556
#[stable(feature = "map_get_key_value", since = "1.40.0")]
551557
pub fn get_key_value<Q: ?Sized>(&self, k: &Q) -> Option<(&K, &V)>
552558
where
553-
K: Borrow<Q>,
559+
K: Borrow<Q> + Ord,
554560
Q: Ord,
555561
{
556562
let root_node = self.root.as_ref()?.reborrow();
@@ -578,7 +584,10 @@ impl<K: Ord, V> BTreeMap<K, V> {
578584
/// assert_eq!(map.first_key_value(), Some((&1, &"b")));
579585
/// ```
580586
#[unstable(feature = "map_first_last", issue = "62924")]
581-
pub fn first_key_value(&self) -> Option<(&K, &V)> {
587+
pub fn first_key_value(&self) -> Option<(&K, &V)>
588+
where
589+
K: Ord,
590+
{
582591
let root_node = self.root.as_ref()?.reborrow();
583592
root_node.first_leaf_edge().right_kv().ok().map(Handle::into_kv)
584593
}
@@ -604,7 +613,10 @@ impl<K: Ord, V> BTreeMap<K, V> {
604613
/// assert_eq!(*map.get(&2).unwrap(), "b");
605614
/// ```
606615
#[unstable(feature = "map_first_last", issue = "62924")]
607-
pub fn first_entry(&mut self) -> Option<OccupiedEntry<'_, K, V>> {
616+
pub fn first_entry(&mut self) -> Option<OccupiedEntry<'_, K, V>>
617+
where
618+
K: Ord,
619+
{
608620
let (map, dormant_map) = DormantMutRef::new(self);
609621
let root_node = map.root.as_mut()?.borrow_mut();
610622
let kv = root_node.first_leaf_edge().right_kv().ok()?;
@@ -631,7 +643,10 @@ impl<K: Ord, V> BTreeMap<K, V> {
631643
/// assert!(map.is_empty());
632644
/// ```
633645
#[unstable(feature = "map_first_last", issue = "62924")]
634-
pub fn pop_first(&mut self) -> Option<(K, V)> {
646+
pub fn pop_first(&mut self) -> Option<(K, V)>
647+
where
648+
K: Ord,
649+
{
635650
self.first_entry().map(|entry| entry.remove_entry())
636651
}
637652

@@ -652,7 +667,10 @@ impl<K: Ord, V> BTreeMap<K, V> {
652667
/// assert_eq!(map.last_key_value(), Some((&2, &"a")));
653668
/// ```
654669
#[unstable(feature = "map_first_last", issue = "62924")]
655-
pub fn last_key_value(&self) -> Option<(&K, &V)> {
670+
pub fn last_key_value(&self) -> Option<(&K, &V)>
671+
where
672+
K: Ord,
673+
{
656674
let root_node = self.root.as_ref()?.reborrow();
657675
root_node.last_leaf_edge().left_kv().ok().map(Handle::into_kv)
658676
}
@@ -678,7 +696,10 @@ impl<K: Ord, V> BTreeMap<K, V> {
678696
/// assert_eq!(*map.get(&2).unwrap(), "last");
679697
/// ```
680698
#[unstable(feature = "map_first_last", issue = "62924")]
681-
pub fn last_entry(&mut self) -> Option<OccupiedEntry<'_, K, V>> {
699+
pub fn last_entry(&mut self) -> Option<OccupiedEntry<'_, K, V>>
700+
where
701+
K: Ord,
702+
{
682703
let (map, dormant_map) = DormantMutRef::new(self);
683704
let root_node = map.root.as_mut()?.borrow_mut();
684705
let kv = root_node.last_leaf_edge().left_kv().ok()?;
@@ -705,7 +726,10 @@ impl<K: Ord, V> BTreeMap<K, V> {
705726
/// assert!(map.is_empty());
706727
/// ```
707728
#[unstable(feature = "map_first_last", issue = "62924")]
708-
pub fn pop_last(&mut self) -> Option<(K, V)> {
729+
pub fn pop_last(&mut self) -> Option<(K, V)>
730+
where
731+
K: Ord,
732+
{
709733
self.last_entry().map(|entry| entry.remove_entry())
710734
}
711735

@@ -729,7 +753,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
729753
#[stable(feature = "rust1", since = "1.0.0")]
730754
pub fn contains_key<Q: ?Sized>(&self, key: &Q) -> bool
731755
where
732-
K: Borrow<Q>,
756+
K: Borrow<Q> + Ord,
733757
Q: Ord,
734758
{
735759
self.get(key).is_some()
@@ -758,7 +782,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
758782
#[stable(feature = "rust1", since = "1.0.0")]
759783
pub fn get_mut<Q: ?Sized>(&mut self, key: &Q) -> Option<&mut V>
760784
where
761-
K: Borrow<Q>,
785+
K: Borrow<Q> + Ord,
762786
Q: Ord,
763787
{
764788
let root_node = self.root.as_mut()?.borrow_mut();
@@ -795,7 +819,10 @@ impl<K: Ord, V> BTreeMap<K, V> {
795819
/// assert_eq!(map[&37], "c");
796820
/// ```
797821
#[stable(feature = "rust1", since = "1.0.0")]
798-
pub fn insert(&mut self, key: K, value: V) -> Option<V> {
822+
pub fn insert(&mut self, key: K, value: V) -> Option<V>
823+
where
824+
K: Ord,
825+
{
799826
match self.entry(key) {
800827
Occupied(mut entry) => Some(entry.insert(value)),
801828
Vacant(entry) => {
@@ -827,7 +854,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
827854
#[stable(feature = "rust1", since = "1.0.0")]
828855
pub fn remove<Q: ?Sized>(&mut self, key: &Q) -> Option<V>
829856
where
830-
K: Borrow<Q>,
857+
K: Borrow<Q> + Ord,
831858
Q: Ord,
832859
{
833860
self.remove_entry(key).map(|(_, v)| v)
@@ -854,7 +881,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
854881
#[stable(feature = "btreemap_remove_entry", since = "1.45.0")]
855882
pub fn remove_entry<Q: ?Sized>(&mut self, key: &Q) -> Option<(K, V)>
856883
where
857-
K: Borrow<Q>,
884+
K: Borrow<Q> + Ord,
858885
Q: Ord,
859886
{
860887
let (map, dormant_map) = DormantMutRef::new(self);
@@ -886,6 +913,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
886913
#[unstable(feature = "btree_retain", issue = "79025")]
887914
pub fn retain<F>(&mut self, mut f: F)
888915
where
916+
K: Ord,
889917
F: FnMut(&K, &mut V) -> bool,
890918
{
891919
self.drain_filter(|k, v| !f(k, v));
@@ -920,7 +948,10 @@ impl<K: Ord, V> BTreeMap<K, V> {
920948
/// assert_eq!(a[&5], "f");
921949
/// ```
922950
#[stable(feature = "btree_append", since = "1.11.0")]
923-
pub fn append(&mut self, other: &mut Self) {
951+
pub fn append(&mut self, other: &mut Self)
952+
where
953+
K: Ord,
954+
{
924955
// Do we have to append anything at all?
925956
if other.is_empty() {
926957
return;
@@ -971,7 +1002,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
9711002
pub fn range<T: ?Sized, R>(&self, range: R) -> Range<'_, K, V>
9721003
where
9731004
T: Ord,
974-
K: Borrow<T>,
1005+
K: Borrow<T> + Ord,
9751006
R: RangeBounds<T>,
9761007
{
9771008
if let Some(root) = &self.root {
@@ -1017,7 +1048,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
10171048
pub fn range_mut<T: ?Sized, R>(&mut self, range: R) -> RangeMut<'_, K, V>
10181049
where
10191050
T: Ord,
1020-
K: Borrow<T>,
1051+
K: Borrow<T> + Ord,
10211052
R: RangeBounds<T>,
10221053
{
10231054
if let Some(root) = &mut self.root {
@@ -1048,7 +1079,10 @@ impl<K: Ord, V> BTreeMap<K, V> {
10481079
/// assert_eq!(count["a"], 3);
10491080
/// ```
10501081
#[stable(feature = "rust1", since = "1.0.0")]
1051-
pub fn entry(&mut self, key: K) -> Entry<'_, K, V> {
1082+
pub fn entry(&mut self, key: K) -> Entry<'_, K, V>
1083+
where
1084+
K: Ord,
1085+
{
10521086
// FIXME(@porglezomp) Avoid allocating if we don't insert
10531087
let (map, dormant_map) = DormantMutRef::new(self);
10541088
let root_node = Self::ensure_is_owned(&mut map.root).borrow_mut();
@@ -1092,7 +1126,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
10921126
#[stable(feature = "btree_split_off", since = "1.11.0")]
10931127
pub fn split_off<Q: ?Sized + Ord>(&mut self, key: &Q) -> Self
10941128
where
1095-
K: Borrow<Q>,
1129+
K: Borrow<Q> + Ord,
10961130
{
10971131
if self.is_empty() {
10981132
return Self::new();
@@ -1150,12 +1184,16 @@ impl<K: Ord, V> BTreeMap<K, V> {
11501184
#[unstable(feature = "btree_drain_filter", issue = "70530")]
11511185
pub fn drain_filter<F>(&mut self, pred: F) -> DrainFilter<'_, K, V, F>
11521186
where
1187+
K: Ord,
11531188
F: FnMut(&K, &mut V) -> bool,
11541189
{
11551190
DrainFilter { pred, inner: self.drain_filter_inner() }
11561191
}
11571192

1158-
pub(super) fn drain_filter_inner(&mut self) -> DrainFilterInner<'_, K, V> {
1193+
pub(super) fn drain_filter_inner(&mut self) -> DrainFilterInner<'_, K, V>
1194+
where
1195+
K: Ord,
1196+
{
11591197
if let Some(root) = self.root.as_mut() {
11601198
let (root, dormant_root) = DormantMutRef::new(root);
11611199
let front = root.borrow_mut().first_leaf_edge();
@@ -1188,7 +1226,10 @@ impl<K: Ord, V> BTreeMap<K, V> {
11881226
/// ```
11891227
#[inline]
11901228
#[unstable(feature = "map_into_keys_values", issue = "75294")]
1191-
pub fn into_keys(self) -> IntoKeys<K, V> {
1229+
pub fn into_keys(self) -> IntoKeys<K, V>
1230+
where
1231+
K: Ord,
1232+
{
11921233
IntoKeys { inner: self.into_iter() }
11931234
}
11941235

@@ -1211,7 +1252,10 @@ impl<K: Ord, V> BTreeMap<K, V> {
12111252
/// ```
12121253
#[inline]
12131254
#[unstable(feature = "map_into_keys_values", issue = "75294")]
1214-
pub fn into_values(self) -> IntoValues<K, V> {
1255+
pub fn into_values(self) -> IntoValues<K, V>
1256+
where
1257+
K: Ord,
1258+
{
12151259
IntoValues { inner: self.into_iter() }
12161260
}
12171261
}
@@ -1968,9 +2012,9 @@ impl<K: Debug, V: Debug> Debug for BTreeMap<K, V> {
19682012
}
19692013

19702014
#[stable(feature = "rust1", since = "1.0.0")]
1971-
impl<K: Ord, Q: ?Sized, V> Index<&Q> for BTreeMap<K, V>
2015+
impl<K, Q: ?Sized, V> Index<&Q> for BTreeMap<K, V>
19722016
where
1973-
K: Borrow<Q>,
2017+
K: Borrow<Q> + Ord,
19742018
Q: Ord,
19752019
{
19762020
type Output = V;

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

+28
Original file line numberDiff line numberDiff line change
@@ -1706,6 +1706,34 @@ fn test_send() {
17061706
}
17071707
}
17081708

1709+
#[allow(dead_code)]
1710+
fn test_ord_absence() {
1711+
fn map<K>(mut map: BTreeMap<K, ()>) {
1712+
map.is_empty();
1713+
map.len();
1714+
map.iter();
1715+
map.iter_mut();
1716+
map.keys();
1717+
map.values();
1718+
map.values_mut();
1719+
map.into_iter();
1720+
}
1721+
1722+
fn map_debug<K: Debug>(mut map: BTreeMap<K, ()>) {
1723+
format!("{:?}", map);
1724+
format!("{:?}", map.iter());
1725+
format!("{:?}", map.iter_mut());
1726+
format!("{:?}", map.keys());
1727+
format!("{:?}", map.values());
1728+
format!("{:?}", map.values_mut());
1729+
format!("{:?}", map.into_iter());
1730+
}
1731+
1732+
fn map_clone<K: Clone>(mut map: BTreeMap<K, ()>) {
1733+
map.clone_from(&map.clone());
1734+
}
1735+
}
1736+
17091737
#[allow(dead_code)]
17101738
fn test_const() {
17111739
const MAP: &'static BTreeMap<(), ()> = &BTreeMap::new();

0 commit comments

Comments
 (0)