Skip to content

Commit 3601f9d

Browse files
authored
Rollup merge of #78581 - a1phyr:const_btree_more, r=dtolnay
Constantify more BTreeMap and BTreeSet functions Just because we can: - `BTreeMap::len` - `BTreeMap::is_empty` - `BTreeSet::len` - `BTreeSet::is_empty` Note that I put the `const` under `const_btree_new`, because I don't think their is a need to create another feature flag for that. cc #71835
2 parents 841f0e7 + 307cc11 commit 3601f9d

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -2188,7 +2188,8 @@ impl<K, V> BTreeMap<K, V> {
21882188
/// assert_eq!(a.len(), 1);
21892189
/// ```
21902190
#[stable(feature = "rust1", since = "1.0.0")]
2191-
pub fn len(&self) -> usize {
2191+
#[rustc_const_unstable(feature = "const_btree_new", issue = "71835")]
2192+
pub const fn len(&self) -> usize {
21922193
self.length
21932194
}
21942195

@@ -2207,7 +2208,8 @@ impl<K, V> BTreeMap<K, V> {
22072208
/// assert!(!a.is_empty());
22082209
/// ```
22092210
#[stable(feature = "rust1", since = "1.0.0")]
2210-
pub fn is_empty(&self) -> bool {
2211+
#[rustc_const_unstable(feature = "const_btree_new", issue = "71835")]
2212+
pub const fn is_empty(&self) -> bool {
22112213
self.len() == 0
22122214
}
22132215

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

+7
Original file line numberDiff line numberDiff line change
@@ -1527,6 +1527,13 @@ fn test_send() {
15271527
}
15281528
}
15291529

1530+
#[allow(dead_code)]
1531+
fn test_const() {
1532+
const MAP: &'static BTreeMap<(), ()> = &BTreeMap::new();
1533+
const LEN: usize = MAP.len();
1534+
const IS_EMPTY: bool = MAP.is_empty();
1535+
}
1536+
15301537
#[test]
15311538
fn test_occupied_entry_key() {
15321539
let mut a = BTreeMap::new();

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,8 @@ impl<T> BTreeSet<T> {
950950
/// assert_eq!(v.len(), 1);
951951
/// ```
952952
#[stable(feature = "rust1", since = "1.0.0")]
953-
pub fn len(&self) -> usize {
953+
#[rustc_const_unstable(feature = "const_btree_new", issue = "71835")]
954+
pub const fn len(&self) -> usize {
954955
self.map.len()
955956
}
956957

@@ -967,7 +968,8 @@ impl<T> BTreeSet<T> {
967968
/// assert!(!v.is_empty());
968969
/// ```
969970
#[stable(feature = "rust1", since = "1.0.0")]
970-
pub fn is_empty(&self) -> bool {
971+
#[rustc_const_unstable(feature = "const_btree_new", issue = "71835")]
972+
pub const fn is_empty(&self) -> bool {
971973
self.len() == 0
972974
}
973975
}

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

+7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ fn test_clone_eq() {
1515
assert_eq!(m.clone(), m);
1616
}
1717

18+
#[allow(dead_code)]
19+
fn test_const() {
20+
const SET: &'static BTreeSet<()> = &BTreeSet::new();
21+
const LEN: usize = SET.len();
22+
const IS_EMPTY: bool = SET.is_empty();
23+
}
24+
1825
#[test]
1926
fn test_iter_min_max() {
2027
let mut a = BTreeSet::new();

0 commit comments

Comments
 (0)