Skip to content

Commit

Permalink
Add fn const BuildHasherDefault::new
Browse files Browse the repository at this point in the history
Because `HashMap::with_hasher` constness is being stabilized this will
in turn allow creating empty HashMap<K,V,BuildHasherDefault<H>> in const
context for any H: Default + Hasher.
  • Loading branch information
krtab committed Mar 29, 2024
1 parent 45796d1 commit 54ab425
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion library/core/src/hash/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,18 @@ pub trait BuildHasher {
#[stable(since = "1.7.0", feature = "build_hasher")]
pub struct BuildHasherDefault<H>(marker::PhantomData<fn() -> H>);

impl<H> BuildHasherDefault<H> {
/// Creates a new BuildHasherDefault for Hasher `H`.
#[unstable(
feature = "build_hasher_default_const_new",
issue = "123197",
reason = "recently added"
)]
pub const fn new() -> Self {
BuildHasherDefault(marker::PhantomData)
}
}

#[stable(since = "1.9.0", feature = "core_impl_debug")]
impl<H> fmt::Debug for BuildHasherDefault<H> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand All @@ -778,7 +790,7 @@ impl<H> Clone for BuildHasherDefault<H> {
#[stable(since = "1.7.0", feature = "build_hasher")]
impl<H> Default for BuildHasherDefault<H> {
fn default() -> BuildHasherDefault<H> {
BuildHasherDefault(marker::PhantomData)
Self::new()
}
}

Expand Down

0 comments on commit 54ab425

Please sign in to comment.