Skip to content

Commit 1020784

Browse files
committed
BTreeMap: make Ord bound explicit, compile-test its absence
1 parent d60b29d commit 1020784

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) => {
@@ -826,7 +853,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
826853
#[stable(feature = "rust1", since = "1.0.0")]
827854
pub fn remove<Q: ?Sized>(&mut self, key: &Q) -> Option<V>
828855
where
829-
K: Borrow<Q>,
856+
K: Borrow<Q> + Ord,
830857
Q: Ord,
831858
{
832859
self.remove_entry(key).map(|(_, v)| v)
@@ -853,7 +880,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
853880
#[stable(feature = "btreemap_remove_entry", since = "1.45.0")]
854881
pub fn remove_entry<Q: ?Sized>(&mut self, key: &Q) -> Option<(K, V)>
855882
where
856-
K: Borrow<Q>,
883+
K: Borrow<Q> + Ord,
857884
Q: Ord,
858885
{
859886
let (map, dormant_map) = DormantMutRef::new(self);
@@ -885,6 +912,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
885912
#[unstable(feature = "btree_retain", issue = "79025")]
886913
pub fn retain<F>(&mut self, mut f: F)
887914
where
915+
K: Ord,
888916
F: FnMut(&K, &mut V) -> bool,
889917
{
890918
self.drain_filter(|k, v| !f(k, v));
@@ -919,7 +947,10 @@ impl<K: Ord, V> BTreeMap<K, V> {
919947
/// assert_eq!(a[&5], "f");
920948
/// ```
921949
#[stable(feature = "btree_append", since = "1.11.0")]
922-
pub fn append(&mut self, other: &mut Self) {
950+
pub fn append(&mut self, other: &mut Self)
951+
where
952+
K: Ord,
953+
{
923954
// Do we have to append anything at all?
924955
if other.is_empty() {
925956
return;
@@ -970,7 +1001,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
9701001
pub fn range<T: ?Sized, R>(&self, range: R) -> Range<'_, K, V>
9711002
where
9721003
T: Ord,
973-
K: Borrow<T>,
1004+
K: Borrow<T> + Ord,
9741005
R: RangeBounds<T>,
9751006
{
9761007
if let Some(root) = &self.root {
@@ -1016,7 +1047,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
10161047
pub fn range_mut<T: ?Sized, R>(&mut self, range: R) -> RangeMut<'_, K, V>
10171048
where
10181049
T: Ord,
1019-
K: Borrow<T>,
1050+
K: Borrow<T> + Ord,
10201051
R: RangeBounds<T>,
10211052
{
10221053
if let Some(root) = &mut self.root {
@@ -1047,7 +1078,10 @@ impl<K: Ord, V> BTreeMap<K, V> {
10471078
/// assert_eq!(count["a"], 3);
10481079
/// ```
10491080
#[stable(feature = "rust1", since = "1.0.0")]
1050-
pub fn entry(&mut self, key: K) -> Entry<'_, K, V> {
1081+
pub fn entry(&mut self, key: K) -> Entry<'_, K, V>
1082+
where
1083+
K: Ord,
1084+
{
10511085
// FIXME(@porglezomp) Avoid allocating if we don't insert
10521086
let (map, dormant_map) = DormantMutRef::new(self);
10531087
let root_node = Self::ensure_is_owned(&mut map.root).borrow_mut();
@@ -1091,7 +1125,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
10911125
#[stable(feature = "btree_split_off", since = "1.11.0")]
10921126
pub fn split_off<Q: ?Sized + Ord>(&mut self, key: &Q) -> Self
10931127
where
1094-
K: Borrow<Q>,
1128+
K: Borrow<Q> + Ord,
10951129
{
10961130
if self.is_empty() {
10971131
return Self::new();
@@ -1149,12 +1183,16 @@ impl<K: Ord, V> BTreeMap<K, V> {
11491183
#[unstable(feature = "btree_drain_filter", issue = "70530")]
11501184
pub fn drain_filter<F>(&mut self, pred: F) -> DrainFilter<'_, K, V, F>
11511185
where
1186+
K: Ord,
11521187
F: FnMut(&K, &mut V) -> bool,
11531188
{
11541189
DrainFilter { pred, inner: self.drain_filter_inner() }
11551190
}
11561191

1157-
pub(super) fn drain_filter_inner(&mut self) -> DrainFilterInner<'_, K, V> {
1192+
pub(super) fn drain_filter_inner(&mut self) -> DrainFilterInner<'_, K, V>
1193+
where
1194+
K: Ord,
1195+
{
11581196
if let Some(root) = self.root.as_mut() {
11591197
let (root, dormant_root) = DormantMutRef::new(root);
11601198
let front = root.borrow_mut().first_leaf_edge();
@@ -1187,7 +1225,10 @@ impl<K: Ord, V> BTreeMap<K, V> {
11871225
/// ```
11881226
#[inline]
11891227
#[unstable(feature = "map_into_keys_values", issue = "75294")]
1190-
pub fn into_keys(self) -> IntoKeys<K, V> {
1228+
pub fn into_keys(self) -> IntoKeys<K, V>
1229+
where
1230+
K: Ord,
1231+
{
11911232
IntoKeys { inner: self.into_iter() }
11921233
}
11931234

@@ -1210,7 +1251,10 @@ impl<K: Ord, V> BTreeMap<K, V> {
12101251
/// ```
12111252
#[inline]
12121253
#[unstable(feature = "map_into_keys_values", issue = "75294")]
1213-
pub fn into_values(self) -> IntoValues<K, V> {
1254+
pub fn into_values(self) -> IntoValues<K, V>
1255+
where
1256+
K: Ord,
1257+
{
12141258
IntoValues { inner: self.into_iter() }
12151259
}
12161260
}
@@ -1967,9 +2011,9 @@ impl<K: Debug, V: Debug> Debug for BTreeMap<K, V> {
19672011
}
19682012

19692013
#[stable(feature = "rust1", since = "1.0.0")]
1970-
impl<K: Ord, Q: ?Sized, V> Index<&Q> for BTreeMap<K, V>
2014+
impl<K, Q: ?Sized, V> Index<&Q> for BTreeMap<K, V>
19712015
where
1972-
K: Borrow<Q>,
2016+
K: Borrow<Q> + Ord,
19732017
Q: Ord,
19742018
{
19752019
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)