stabilize const_collections_with_hasher and build_hasher_default_const_new
This commit is contained in:
parent
3bff51ea91
commit
3a9bc76f80
4 changed files with 49 additions and 6 deletions
|
@ -752,10 +752,10 @@ pub struct BuildHasherDefault<H>(marker::PhantomData<fn() -> H>);
|
||||||
|
|
||||||
impl<H> BuildHasherDefault<H> {
|
impl<H> BuildHasherDefault<H> {
|
||||||
/// Creates a new BuildHasherDefault for Hasher `H`.
|
/// Creates a new BuildHasherDefault for Hasher `H`.
|
||||||
#[unstable(
|
#[stable(feature = "build_hasher_default_const_new", since = "CURRENT_RUSTC_VERSION")]
|
||||||
|
#[rustc_const_stable(
|
||||||
feature = "build_hasher_default_const_new",
|
feature = "build_hasher_default_const_new",
|
||||||
issue = "123197",
|
since = "CURRENT_RUSTC_VERSION"
|
||||||
reason = "recently added"
|
|
||||||
)]
|
)]
|
||||||
pub const fn new() -> Self {
|
pub const fn new() -> Self {
|
||||||
BuildHasherDefault(marker::PhantomData)
|
BuildHasherDefault(marker::PhantomData)
|
||||||
|
|
|
@ -204,6 +204,25 @@ use crate::ops::Index;
|
||||||
/// println!("{viking:?} has {health} hp");
|
/// println!("{viking:?} has {health} hp");
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
|
///
|
||||||
|
/// # Usage in `const` and `static`
|
||||||
|
///
|
||||||
|
/// As explained above, `HashMap` is randomly seeded: each `HashMap` instance uses a different seed,
|
||||||
|
/// which means that `HashMap::new` cannot be used in const context. To construct a `HashMap` in the
|
||||||
|
/// initializer of a `const` or `static` item, you will have to use a different hasher that does not
|
||||||
|
/// involve a random seed, as demonstrated in the following example. **`HashMap` constructed this
|
||||||
|
/// way are not resistant against HashDoS!**
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// use std::collections::HashMap;
|
||||||
|
/// use std::hash::{BuildHasherDefault, DefaultHasher};
|
||||||
|
/// use std::sync::Mutex;
|
||||||
|
///
|
||||||
|
/// const EMPTY_MAP: HashMap<String, Vec<i32>, BuildHasherDefault<DefaultHasher>> =
|
||||||
|
/// HashMap::with_hasher(BuildHasherDefault::new());
|
||||||
|
/// static MAP: Mutex<HashMap<String, Vec<i32>, BuildHasherDefault<DefaultHasher>>> =
|
||||||
|
/// Mutex::new(HashMap::with_hasher(BuildHasherDefault::new()));
|
||||||
|
/// ```
|
||||||
|
|
||||||
#[cfg_attr(not(test), rustc_diagnostic_item = "HashMap")]
|
#[cfg_attr(not(test), rustc_diagnostic_item = "HashMap")]
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
|
@ -277,7 +296,10 @@ impl<K, V, S> HashMap<K, V, S> {
|
||||||
/// ```
|
/// ```
|
||||||
#[inline]
|
#[inline]
|
||||||
#[stable(feature = "hashmap_build_hasher", since = "1.7.0")]
|
#[stable(feature = "hashmap_build_hasher", since = "1.7.0")]
|
||||||
#[rustc_const_unstable(feature = "const_collections_with_hasher", issue = "102575")]
|
#[rustc_const_stable(
|
||||||
|
feature = "const_collections_with_hasher",
|
||||||
|
since = "CURRENT_RUSTC_VERSION"
|
||||||
|
)]
|
||||||
pub const fn with_hasher(hash_builder: S) -> HashMap<K, V, S> {
|
pub const fn with_hasher(hash_builder: S) -> HashMap<K, V, S> {
|
||||||
HashMap { base: base::HashMap::with_hasher(hash_builder) }
|
HashMap { base: base::HashMap::with_hasher(hash_builder) }
|
||||||
}
|
}
|
||||||
|
|
|
@ -101,6 +101,25 @@ use crate::ops::{BitAnd, BitOr, BitXor, Sub};
|
||||||
/// [`HashMap`]: crate::collections::HashMap
|
/// [`HashMap`]: crate::collections::HashMap
|
||||||
/// [`RefCell`]: crate::cell::RefCell
|
/// [`RefCell`]: crate::cell::RefCell
|
||||||
/// [`Cell`]: crate::cell::Cell
|
/// [`Cell`]: crate::cell::Cell
|
||||||
|
///
|
||||||
|
/// # Usage in `const` and `static`
|
||||||
|
///
|
||||||
|
/// Like `HashMap`, `HashSet` is randomly seeded: each `HashSet` instance uses a different seed,
|
||||||
|
/// which means that `HashSet::new` cannot be used in const context. To construct a `HashSet` in the
|
||||||
|
/// initializer of a `const` or `static` item, you will have to use a different hasher that does not
|
||||||
|
/// involve a random seed, as demonstrated in the following example. **`HashSet` constructed this
|
||||||
|
/// way are not resistant against HashDoS!**
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// use std::collections::HashSet;
|
||||||
|
/// use std::hash::{BuildHasherDefault, DefaultHasher};
|
||||||
|
/// use std::sync::Mutex;
|
||||||
|
///
|
||||||
|
/// const EMPTY_SET: HashSet<String, BuildHasherDefault<DefaultHasher>> =
|
||||||
|
/// HashSet::with_hasher(BuildHasherDefault::new());
|
||||||
|
/// static SET: Mutex<HashSet<String, BuildHasherDefault<DefaultHasher>>> =
|
||||||
|
/// Mutex::new(HashSet::with_hasher(BuildHasherDefault::new()));
|
||||||
|
/// ```
|
||||||
#[cfg_attr(not(test), rustc_diagnostic_item = "HashSet")]
|
#[cfg_attr(not(test), rustc_diagnostic_item = "HashSet")]
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
pub struct HashSet<T, S = RandomState> {
|
pub struct HashSet<T, S = RandomState> {
|
||||||
|
@ -369,7 +388,10 @@ impl<T, S> HashSet<T, S> {
|
||||||
/// ```
|
/// ```
|
||||||
#[inline]
|
#[inline]
|
||||||
#[stable(feature = "hashmap_build_hasher", since = "1.7.0")]
|
#[stable(feature = "hashmap_build_hasher", since = "1.7.0")]
|
||||||
#[rustc_const_unstable(feature = "const_collections_with_hasher", issue = "102575")]
|
#[rustc_const_stable(
|
||||||
|
feature = "const_collections_with_hasher",
|
||||||
|
since = "CURRENT_RUSTC_VERSION"
|
||||||
|
)]
|
||||||
pub const fn with_hasher(hasher: S) -> HashSet<T, S> {
|
pub const fn with_hasher(hasher: S) -> HashSet<T, S> {
|
||||||
HashSet { base: base::HashSet::with_hasher(hasher) }
|
HashSet { base: base::HashSet::with_hasher(hasher) }
|
||||||
}
|
}
|
||||||
|
|
|
@ -320,7 +320,6 @@
|
||||||
// Library features (core):
|
// Library features (core):
|
||||||
// tidy-alphabetical-start
|
// tidy-alphabetical-start
|
||||||
#![feature(array_chunks)]
|
#![feature(array_chunks)]
|
||||||
#![feature(build_hasher_default_const_new)]
|
|
||||||
#![feature(c_str_module)]
|
#![feature(c_str_module)]
|
||||||
#![feature(char_internals)]
|
#![feature(char_internals)]
|
||||||
#![feature(clone_to_uninit)]
|
#![feature(clone_to_uninit)]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue