Skip to content

Commit

Permalink
Rollup merge of #71839 - LG3696:master, r=cramertj
Browse files Browse the repository at this point in the history
Make BTreeMap::new and BTreeSet::new const
  • Loading branch information
Dylan-DPC authored May 9, 2020
2 parents 62374ee + 707cfd1 commit f16c27f
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/liballoc/collections/btree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
/// map.insert(1, "a");
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn new() -> BTreeMap<K, V> {
#[rustc_const_unstable(feature = "const_btree_new", issue = "71835")]
pub const fn new() -> BTreeMap<K, V> {
BTreeMap { root: None, length: 0 }
}

Expand Down
3 changes: 2 additions & 1 deletion src/liballoc/collections/btree/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,8 @@ impl<T: Ord> BTreeSet<T> {
/// let mut set: BTreeSet<i32> = BTreeSet::new();
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn new() -> BTreeSet<T> {
#[rustc_const_unstable(feature = "const_btree_new", issue = "71835")]
pub const fn new() -> BTreeSet<T> {
BTreeSet { map: BTreeMap::new() }
}

Expand Down
1 change: 1 addition & 0 deletions src/liballoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
#![feature(cfg_sanitize)]
#![feature(cfg_target_has_atomic)]
#![feature(coerce_unsized)]
#![feature(const_btree_new)]
#![feature(const_generic_impls_guard)]
#![feature(const_generics)]
#![feature(const_in_array_repeat_expressions)]
Expand Down
9 changes: 9 additions & 0 deletions src/test/ui/collections-const-new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,18 @@
// Test several functions can be used for constants
// 1. Vec::new()
// 2. String::new()
// 3. BTreeMap::new()
// 4. BTreeSet::new()

#![feature(const_btree_new)]

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

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

use std::collections::{BTreeMap, BTreeSet};
const MY_BTREEMAP: BTreeMap<u32, u32> = BTreeMap::new();

const MY_BTREESET: BTreeSet<u32> = BTreeSet::new();

fn main() {}

0 comments on commit f16c27f

Please sign in to comment.