未验证 提交 5878780e 编写于 作者: M Mara Bos 提交者: GitHub

Rollup merge of #88040 - nbdd0121:btreemap, r=m-ou-se

BTree: remove Ord bound from new

`K: Ord` bound is unnecessary on `BTree{Map,Set}::new` and their `Default` impl. No elements exist so there are nothing to compare anyway, so I don't think "future proof" would be a blocker here. This is analogous to `HashMap::new` not having a `K: Eq + Hash` bound.

#79245 originally does this and for some reason drops the change to `new` and `Default`. I can see why changes to other methods like `entry` or `symmetric_difference` need to be careful but I couldn't find out any reason not to do it on `new`.

Removing the bound also makes the stabilisation of `const fn new` not depending on const trait bounds.

cc `@steffahn` who suggests me to make this PR.

r? `@dtolnay`
...@@ -233,9 +233,7 @@ fn clone_subtree<'a, K: Clone, V: Clone>( ...@@ -233,9 +233,7 @@ fn clone_subtree<'a, K: Clone, V: Clone>(
} }
if self.is_empty() { if self.is_empty() {
// Ideally we'd call `BTreeMap::new` here, but that has the `K: BTreeMap::new()
// Ord` constraint, which this method lacks.
BTreeMap { root: None, length: 0 }
} else { } else {
clone_subtree(self.root.as_ref().unwrap().reborrow()) // unwrap succeeds because not empty clone_subtree(self.root.as_ref().unwrap().reborrow()) // unwrap succeeds because not empty
} }
...@@ -499,10 +497,7 @@ impl<K, V> BTreeMap<K, V> { ...@@ -499,10 +497,7 @@ impl<K, V> BTreeMap<K, V> {
/// ``` /// ```
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_btree_new", issue = "71835")] #[rustc_const_unstable(feature = "const_btree_new", issue = "71835")]
pub const fn new() -> BTreeMap<K, V> pub const fn new() -> BTreeMap<K, V> {
where
K: Ord,
{
BTreeMap { root: None, length: 0 } BTreeMap { root: None, length: 0 }
} }
...@@ -522,7 +517,7 @@ pub const fn new() -> BTreeMap<K, V> ...@@ -522,7 +517,7 @@ pub const fn new() -> BTreeMap<K, V>
/// ``` /// ```
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub fn clear(&mut self) { pub fn clear(&mut self) {
*self = BTreeMap { root: None, length: 0 }; *self = BTreeMap::new();
} }
/// Returns a reference to the value corresponding to the key. /// Returns a reference to the value corresponding to the key.
...@@ -1957,7 +1952,7 @@ fn hash<H: Hasher>(&self, state: &mut H) { ...@@ -1957,7 +1952,7 @@ fn hash<H: Hasher>(&self, state: &mut H) {
} }
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<K: Ord, V> Default for BTreeMap<K, V> { impl<K, V> Default for BTreeMap<K, V> {
/// Creates an empty `BTreeMap`. /// Creates an empty `BTreeMap`.
fn default() -> BTreeMap<K, V> { fn default() -> BTreeMap<K, V> {
BTreeMap::new() BTreeMap::new()
......
...@@ -1745,7 +1745,7 @@ fn vacant_entry<T: Send + Ord + Default>(v: &mut BTreeMap<T, T>) -> impl Send + ...@@ -1745,7 +1745,7 @@ fn vacant_entry<T: Send + Ord + Default>(v: &mut BTreeMap<T, T>) -> impl Send +
} }
} }
#[allow(dead_code)] #[test]
fn test_ord_absence() { fn test_ord_absence() {
fn map<K>(mut map: BTreeMap<K, ()>) { fn map<K>(mut map: BTreeMap<K, ()>) {
map.is_empty(); map.is_empty();
...@@ -1784,6 +1784,12 @@ fn map_debug<K: Debug>(mut map: BTreeMap<K, ()>) { ...@@ -1784,6 +1784,12 @@ fn map_debug<K: Debug>(mut map: BTreeMap<K, ()>) {
fn map_clone<K: Clone>(mut map: BTreeMap<K, ()>) { fn map_clone<K: Clone>(mut map: BTreeMap<K, ()>) {
map.clone_from(&map.clone()); map.clone_from(&map.clone());
} }
#[derive(Debug, Clone)]
struct NonOrd;
map(BTreeMap::<NonOrd, _>::new());
map_debug(BTreeMap::<NonOrd, _>::new());
map_clone(BTreeMap::<NonOrd, _>::default());
} }
#[test] #[test]
......
...@@ -246,10 +246,7 @@ impl<T> BTreeSet<T> { ...@@ -246,10 +246,7 @@ impl<T> BTreeSet<T> {
/// ``` /// ```
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_btree_new", issue = "71835")] #[rustc_const_unstable(feature = "const_btree_new", issue = "71835")]
pub const fn new() -> BTreeSet<T> pub const fn new() -> BTreeSet<T> {
where
T: Ord,
{
BTreeSet { map: BTreeMap::new() } BTreeSet { map: BTreeMap::new() }
} }
...@@ -1192,7 +1189,7 @@ fn extend_one(&mut self, &elem: &'a T) { ...@@ -1192,7 +1189,7 @@ fn extend_one(&mut self, &elem: &'a T) {
} }
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<T: Ord> Default for BTreeSet<T> { impl<T> Default for BTreeSet<T> {
/// Creates an empty `BTreeSet`. /// Creates an empty `BTreeSet`.
fn default() -> BTreeSet<T> { fn default() -> BTreeSet<T> {
BTreeSet::new() BTreeSet::new()
......
...@@ -607,7 +607,7 @@ fn union<T: Send + Sync + Ord>(v: &BTreeSet<T>) -> impl Send + '_ { ...@@ -607,7 +607,7 @@ fn union<T: Send + Sync + Ord>(v: &BTreeSet<T>) -> impl Send + '_ {
} }
} }
#[allow(dead_code)] #[test]
fn test_ord_absence() { fn test_ord_absence() {
fn set<K>(mut set: BTreeSet<K>) { fn set<K>(mut set: BTreeSet<K>) {
set.is_empty(); set.is_empty();
...@@ -626,6 +626,12 @@ fn set_debug<K: Debug>(set: BTreeSet<K>) { ...@@ -626,6 +626,12 @@ fn set_debug<K: Debug>(set: BTreeSet<K>) {
fn set_clone<K: Clone>(mut set: BTreeSet<K>) { fn set_clone<K: Clone>(mut set: BTreeSet<K>) {
set.clone_from(&set.clone()); set.clone_from(&set.clone());
} }
#[derive(Debug, Clone)]
struct NonOrd;
set(BTreeSet::<NonOrd>::new());
set_debug(BTreeSet::<NonOrd>::new());
set_clone(BTreeSet::<NonOrd>::default());
} }
#[test] #[test]
......
// Test const functions in the library // Test const functions in the library
use core::cmp::Ordering;
// FIXME remove this struct once we put `K: ?const Ord` on BTreeMap::new.
#[derive(PartialEq, Eq, PartialOrd)]
pub struct MyType;
impl const Ord for MyType {
fn cmp(&self, _: &Self) -> Ordering {
Ordering::Equal
}
fn max(self, _: Self) -> Self {
Self
}
fn min(self, _: Self) -> Self {
Self
}
fn clamp(self, _: Self, _: Self) -> Self {
Self
}
}
pub const MY_VEC: Vec<usize> = Vec::new(); pub const MY_VEC: Vec<usize> = Vec::new();
pub const MY_VEC2: Vec<usize> = Default::default(); pub const MY_VEC2: Vec<usize> = Default::default();
...@@ -32,13 +8,13 @@ fn clamp(self, _: Self, _: Self) -> Self { ...@@ -32,13 +8,13 @@ fn clamp(self, _: Self, _: Self) -> Self {
use std::collections::{BTreeMap, BTreeSet}; use std::collections::{BTreeMap, BTreeSet};
pub const MY_BTREEMAP: BTreeMap<MyType, MyType> = BTreeMap::new(); pub const MY_BTREEMAP: BTreeMap<usize, usize> = BTreeMap::new();
pub const MAP: &'static BTreeMap<MyType, MyType> = &MY_BTREEMAP; pub const MAP: &'static BTreeMap<usize, usize> = &MY_BTREEMAP;
pub const MAP_LEN: usize = MAP.len(); pub const MAP_LEN: usize = MAP.len();
pub const MAP_IS_EMPTY: bool = MAP.is_empty(); pub const MAP_IS_EMPTY: bool = MAP.is_empty();
pub const MY_BTREESET: BTreeSet<MyType> = BTreeSet::new(); pub const MY_BTREESET: BTreeSet<usize> = BTreeSet::new();
pub const SET: &'static BTreeSet<MyType> = &MY_BTREESET; pub const SET: &'static BTreeSet<usize> = &MY_BTREESET;
pub const SET_LEN: usize = SET.len(); pub const SET_LEN: usize = SET.len();
pub const SET_IS_EMPTY: bool = SET.is_empty(); pub const SET_IS_EMPTY: bool = SET.is_empty();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册