Skip to content

Commit

Permalink
Auto merge of #79245 - ssomers:btree_curb_ord_bound, r=dtolnay
Browse files Browse the repository at this point in the history
BTree: remove Ord bound where it is absent elsewhere

Some btree methods don't really need an Ord bound and don't have one, while some methods that more obviously don't need it, do have one.

An example of the former is `iter`, even though it explicitly exposes the work of the Ord implementation (["sorted by key"](https://doc.rust-lang.org/std/collections/struct.BTreeMap.html#method.iter) - but I'm not suggesting it should have the Ord bound). An example of the latter is `new`, which doesn't involve any keys whatsoever.
  • Loading branch information
bors committed Feb 8, 2021
2 parents 4940dd4 + 9066c73 commit 0b96f60
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
17 changes: 4 additions & 13 deletions library/alloc/src/collections/btree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,11 +500,8 @@ impl<K, V> BTreeMap<K, V> {
/// assert!(a.is_empty());
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn clear(&mut self)
where
K: Ord,
{
*self = BTreeMap::new();
pub fn clear(&mut self) {
*self = BTreeMap { root: None, length: 0 };
}

/// Returns a reference to the value corresponding to the key.
Expand Down Expand Up @@ -1227,10 +1224,7 @@ impl<K, V> BTreeMap<K, V> {
/// ```
#[inline]
#[unstable(feature = "map_into_keys_values", issue = "75294")]
pub fn into_keys(self) -> IntoKeys<K, V>
where
K: Ord,
{
pub fn into_keys(self) -> IntoKeys<K, V> {
IntoKeys { inner: self.into_iter() }
}

Expand All @@ -1253,10 +1247,7 @@ impl<K, V> BTreeMap<K, V> {
/// ```
#[inline]
#[unstable(feature = "map_into_keys_values", issue = "75294")]
pub fn into_values(self) -> IntoValues<K, V>
where
K: Ord,
{
pub fn into_values(self) -> IntoValues<K, V> {
IntoValues { inner: self.into_iter() }
}
}
Expand Down
17 changes: 15 additions & 2 deletions library/alloc/src/collections/btree/map/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1711,12 +1711,19 @@ fn test_ord_absence() {
fn map<K>(mut map: BTreeMap<K, ()>) {
map.is_empty();
map.len();
map.clear();
map.iter();
map.iter_mut();
map.keys();
map.values();
map.values_mut();
map.into_iter();
if true {
map.into_values();
} else if true {
map.into_iter();
} else {
map.into_keys();
}
}

fn map_debug<K: Debug>(mut map: BTreeMap<K, ()>) {
Expand All @@ -1726,7 +1733,13 @@ fn test_ord_absence() {
format!("{:?}", map.keys());
format!("{:?}", map.values());
format!("{:?}", map.values_mut());
format!("{:?}", map.into_iter());
if true {
format!("{:?}", map.into_iter());
} else if true {
format!("{:?}", map.into_keys());
} else {
format!("{:?}", map.into_values());
}
}

fn map_clone<K: Clone>(mut map: BTreeMap<K, ()>) {
Expand Down
5 changes: 1 addition & 4 deletions library/alloc/src/collections/btree/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,10 +457,7 @@ impl<T> BTreeSet<T> {
/// assert!(v.is_empty());
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn clear(&mut self)
where
T: Ord,
{
pub fn clear(&mut self) {
self.map.clear()
}

Expand Down
3 changes: 2 additions & 1 deletion library/alloc/src/collections/btree/set/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -641,9 +641,10 @@ fn test_send() {

#[allow(dead_code)]
fn test_ord_absence() {
fn set<K>(set: BTreeSet<K>) {
fn set<K>(mut set: BTreeSet<K>) {
set.is_empty();
set.len();
set.clear();
set.iter();
set.into_iter();
}
Expand Down

0 comments on commit 0b96f60

Please sign in to comment.