Skip to content

Commit f33f266

Browse files
committed
BTree: remove Ord bound from new
1 parent adf1688 commit f33f266

File tree

5 files changed

+24
-44
lines changed

5 files changed

+24
-44
lines changed

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

+4-9
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,7 @@ impl<K: Clone, V: Clone> Clone for BTreeMap<K, V> {
233233
}
234234

235235
if self.is_empty() {
236-
// Ideally we'd call `BTreeMap::new` here, but that has the `K:
237-
// Ord` constraint, which this method lacks.
238-
BTreeMap { root: None, length: 0 }
236+
BTreeMap::new()
239237
} else {
240238
clone_subtree(self.root.as_ref().unwrap().reborrow()) // unwrap succeeds because not empty
241239
}
@@ -499,10 +497,7 @@ impl<K, V> BTreeMap<K, V> {
499497
/// ```
500498
#[stable(feature = "rust1", since = "1.0.0")]
501499
#[rustc_const_unstable(feature = "const_btree_new", issue = "71835")]
502-
pub const fn new() -> BTreeMap<K, V>
503-
where
504-
K: Ord,
505-
{
500+
pub const fn new() -> BTreeMap<K, V> {
506501
BTreeMap { root: None, length: 0 }
507502
}
508503

@@ -522,7 +517,7 @@ impl<K, V> BTreeMap<K, V> {
522517
/// ```
523518
#[stable(feature = "rust1", since = "1.0.0")]
524519
pub fn clear(&mut self) {
525-
*self = BTreeMap { root: None, length: 0 };
520+
*self = BTreeMap::new();
526521
}
527522

528523
/// Returns a reference to the value corresponding to the key.
@@ -1957,7 +1952,7 @@ impl<K: Hash, V: Hash> Hash for BTreeMap<K, V> {
19571952
}
19581953

19591954
#[stable(feature = "rust1", since = "1.0.0")]
1960-
impl<K: Ord, V> Default for BTreeMap<K, V> {
1955+
impl<K, V> Default for BTreeMap<K, V> {
19611956
/// Creates an empty `BTreeMap`.
19621957
fn default() -> BTreeMap<K, V> {
19631958
BTreeMap::new()

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -1745,7 +1745,7 @@ fn test_send() {
17451745
}
17461746
}
17471747

1748-
#[allow(dead_code)]
1748+
#[test]
17491749
fn test_ord_absence() {
17501750
fn map<K>(mut map: BTreeMap<K, ()>) {
17511751
map.is_empty();
@@ -1784,6 +1784,12 @@ fn test_ord_absence() {
17841784
fn map_clone<K: Clone>(mut map: BTreeMap<K, ()>) {
17851785
map.clone_from(&map.clone());
17861786
}
1787+
1788+
#[derive(Debug, Clone)]
1789+
struct NonOrd;
1790+
map(BTreeMap::<NonOrd, _>::new());
1791+
map_debug(BTreeMap::<NonOrd, _>::new());
1792+
map_clone(BTreeMap::<NonOrd, _>::default());
17871793
}
17881794

17891795
#[test]

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

+2-5
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,7 @@ impl<T> BTreeSet<T> {
246246
/// ```
247247
#[stable(feature = "rust1", since = "1.0.0")]
248248
#[rustc_const_unstable(feature = "const_btree_new", issue = "71835")]
249-
pub const fn new() -> BTreeSet<T>
250-
where
251-
T: Ord,
252-
{
249+
pub const fn new() -> BTreeSet<T> {
253250
BTreeSet { map: BTreeMap::new() }
254251
}
255252

@@ -1192,7 +1189,7 @@ impl<'a, T: 'a + Ord + Copy> Extend<&'a T> for BTreeSet<T> {
11921189
}
11931190

11941191
#[stable(feature = "rust1", since = "1.0.0")]
1195-
impl<T: Ord> Default for BTreeSet<T> {
1192+
impl<T> Default for BTreeSet<T> {
11961193
/// Creates an empty `BTreeSet`.
11971194
fn default() -> BTreeSet<T> {
11981195
BTreeSet::new()

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ fn test_send() {
607607
}
608608
}
609609

610-
#[allow(dead_code)]
610+
#[test]
611611
fn test_ord_absence() {
612612
fn set<K>(mut set: BTreeSet<K>) {
613613
set.is_empty();
@@ -626,6 +626,12 @@ fn test_ord_absence() {
626626
fn set_clone<K: Clone>(mut set: BTreeSet<K>) {
627627
set.clone_from(&set.clone());
628628
}
629+
630+
#[derive(Debug, Clone)]
631+
struct NonOrd;
632+
set(BTreeSet::<NonOrd>::new());
633+
set_debug(BTreeSet::<NonOrd>::new());
634+
set_clone(BTreeSet::<NonOrd>::default());
629635
}
630636

631637
#[test]

library/alloc/tests/const_fns.rs

+4-28
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,5 @@
11
// Test const functions in the library
22

3-
use core::cmp::Ordering;
4-
5-
// FIXME remove this struct once we put `K: ?const Ord` on BTreeMap::new.
6-
#[derive(PartialEq, Eq, PartialOrd)]
7-
pub struct MyType;
8-
9-
impl const Ord for MyType {
10-
fn cmp(&self, _: &Self) -> Ordering {
11-
Ordering::Equal
12-
}
13-
14-
fn max(self, _: Self) -> Self {
15-
Self
16-
}
17-
18-
fn min(self, _: Self) -> Self {
19-
Self
20-
}
21-
22-
fn clamp(self, _: Self, _: Self) -> Self {
23-
Self
24-
}
25-
}
26-
273
pub const MY_VEC: Vec<usize> = Vec::new();
284
pub const MY_VEC2: Vec<usize> = Default::default();
295

@@ -32,13 +8,13 @@ pub const MY_STRING2: String = Default::default();
328

339
use std::collections::{BTreeMap, BTreeSet};
3410

35-
pub const MY_BTREEMAP: BTreeMap<MyType, MyType> = BTreeMap::new();
36-
pub const MAP: &'static BTreeMap<MyType, MyType> = &MY_BTREEMAP;
11+
pub const MY_BTREEMAP: BTreeMap<usize, usize> = BTreeMap::new();
12+
pub const MAP: &'static BTreeMap<usize, usize> = &MY_BTREEMAP;
3713
pub const MAP_LEN: usize = MAP.len();
3814
pub const MAP_IS_EMPTY: bool = MAP.is_empty();
3915

40-
pub const MY_BTREESET: BTreeSet<MyType> = BTreeSet::new();
41-
pub const SET: &'static BTreeSet<MyType> = &MY_BTREESET;
16+
pub const MY_BTREESET: BTreeSet<usize> = BTreeSet::new();
17+
pub const SET: &'static BTreeSet<usize> = &MY_BTREESET;
4218
pub const SET_LEN: usize = SET.len();
4319
pub const SET_IS_EMPTY: bool = SET.is_empty();
4420

0 commit comments

Comments
 (0)