Rollup merge of #71839 - LG3696:master, r=cramertj

Make BTreeMap::new and BTreeSet::new const
This commit is contained in:
Dylan DPC 2020-05-09 03:10:05 +02:00 committed by GitHub
commit f16c27f1c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 2 deletions

View File

@ -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 }
}

View File

@ -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() }
}

View File

@ -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)]

View File

@ -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() {}