Skip to content

Commit f16c27f

Browse files
authored
Rollup merge of #71839 - LG3696:master, r=cramertj
Make BTreeMap::new and BTreeSet::new const
2 parents 62374ee + 707cfd1 commit f16c27f

File tree

4 files changed

+14
-2
lines changed

4 files changed

+14
-2
lines changed

src/liballoc/collections/btree/map.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
556556
/// map.insert(1, "a");
557557
/// ```
558558
#[stable(feature = "rust1", since = "1.0.0")]
559-
pub fn new() -> BTreeMap<K, V> {
559+
#[rustc_const_unstable(feature = "const_btree_new", issue = "71835")]
560+
pub const fn new() -> BTreeMap<K, V> {
560561
BTreeMap { root: None, length: 0 }
561562
}
562563

src/liballoc/collections/btree/set.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,8 @@ impl<T: Ord> BTreeSet<T> {
309309
/// let mut set: BTreeSet<i32> = BTreeSet::new();
310310
/// ```
311311
#[stable(feature = "rust1", since = "1.0.0")]
312-
pub fn new() -> BTreeSet<T> {
312+
#[rustc_const_unstable(feature = "const_btree_new", issue = "71835")]
313+
pub const fn new() -> BTreeSet<T> {
313314
BTreeSet { map: BTreeMap::new() }
314315
}
315316

src/liballoc/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
#![feature(cfg_sanitize)]
8383
#![feature(cfg_target_has_atomic)]
8484
#![feature(coerce_unsized)]
85+
#![feature(const_btree_new)]
8586
#![feature(const_generic_impls_guard)]
8687
#![feature(const_generics)]
8788
#![feature(const_in_array_repeat_expressions)]

src/test/ui/collections-const-new.rs

+9
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,18 @@
33
// Test several functions can be used for constants
44
// 1. Vec::new()
55
// 2. String::new()
6+
// 3. BTreeMap::new()
7+
// 4. BTreeSet::new()
8+
9+
#![feature(const_btree_new)]
610

711
const MY_VEC: Vec<usize> = Vec::new();
812

913
const MY_STRING: String = String::new();
1014

15+
use std::collections::{BTreeMap, BTreeSet};
16+
const MY_BTREEMAP: BTreeMap<u32, u32> = BTreeMap::new();
17+
18+
const MY_BTREESET: BTreeSet<u32> = BTreeSet::new();
19+
1120
fn main() {}

0 commit comments

Comments
 (0)