Made Sip const Hasher

This commit is contained in:
onestacked 2022-11-06 17:46:38 +01:00
parent 3ea4165a77
commit 5f9899b289
4 changed files with 32 additions and 17 deletions

View File

@ -118,7 +118,7 @@ macro_rules! load_int_le {
/// Safety: this performs unchecked indexing of `buf` at `start..start+len`, so
/// that must be in-bounds.
#[inline]
unsafe fn u8to64_le(buf: &[u8], start: usize, len: usize) -> u64 {
const unsafe fn u8to64_le(buf: &[u8], start: usize, len: usize) -> u64 {
debug_assert!(len < 8);
let mut i = 0; // current byte index (from LSB) in the output u64
let mut out = 0;
@ -138,7 +138,7 @@ unsafe fn u8to64_le(buf: &[u8], start: usize, len: usize) -> u64 {
out |= (unsafe { *buf.get_unchecked(start + i) } as u64) << (i * 8);
i += 1;
}
debug_assert_eq!(i, len);
debug_assert!(i == len);
out
}
@ -150,8 +150,9 @@ impl SipHasher {
since = "1.13.0",
note = "use `std::collections::hash_map::DefaultHasher` instead"
)]
#[rustc_const_unstable(feature = "const_hash", issue = "none")]
#[must_use]
pub fn new() -> SipHasher {
pub const fn new() -> SipHasher {
SipHasher::new_with_keys(0, 0)
}
@ -162,8 +163,9 @@ impl SipHasher {
since = "1.13.0",
note = "use `std::collections::hash_map::DefaultHasher` instead"
)]
#[rustc_const_unstable(feature = "const_hash", issue = "none")]
#[must_use]
pub fn new_with_keys(key0: u64, key1: u64) -> SipHasher {
pub const fn new_with_keys(key0: u64, key1: u64) -> SipHasher {
SipHasher(SipHasher24 { hasher: Hasher::new_with_keys(key0, key1) })
}
}
@ -176,7 +178,8 @@ impl SipHasher13 {
since = "1.13.0",
note = "use `std::collections::hash_map::DefaultHasher` instead"
)]
pub fn new() -> SipHasher13 {
#[rustc_const_unstable(feature = "const_hash", issue = "none")]
pub const fn new() -> SipHasher13 {
SipHasher13::new_with_keys(0, 0)
}
@ -187,14 +190,15 @@ impl SipHasher13 {
since = "1.13.0",
note = "use `std::collections::hash_map::DefaultHasher` instead"
)]
pub fn new_with_keys(key0: u64, key1: u64) -> SipHasher13 {
#[rustc_const_unstable(feature = "const_hash", issue = "none")]
pub const fn new_with_keys(key0: u64, key1: u64) -> SipHasher13 {
SipHasher13 { hasher: Hasher::new_with_keys(key0, key1) }
}
}
impl<S: Sip> Hasher<S> {
#[inline]
fn new_with_keys(key0: u64, key1: u64) -> Hasher<S> {
const fn new_with_keys(key0: u64, key1: u64) -> Hasher<S> {
let mut state = Hasher {
k0: key0,
k1: key1,
@ -209,7 +213,7 @@ impl<S: Sip> Hasher<S> {
}
#[inline]
fn reset(&mut self) {
const fn reset(&mut self) {
self.length = 0;
self.state.v0 = self.k0 ^ 0x736f6d6570736575;
self.state.v1 = self.k1 ^ 0x646f72616e646f6d;
@ -220,7 +224,8 @@ impl<S: Sip> Hasher<S> {
}
#[stable(feature = "rust1", since = "1.0.0")]
impl super::Hasher for SipHasher {
#[rustc_const_unstable(feature = "const_hash", issue = "none")]
impl const super::Hasher for SipHasher {
#[inline]
fn write(&mut self, msg: &[u8]) {
self.0.hasher.write(msg)
@ -238,7 +243,11 @@ impl super::Hasher for SipHasher {
}
#[unstable(feature = "hashmap_internals", issue = "none")]
impl super::Hasher for SipHasher13 {
#[rustc_const_unstable(feature = "const_hash", issue = "none")]
impl const super::Hasher for SipHasher13
where
Hasher<Sip13Rounds>: ~const super::Hasher,
{
#[inline]
fn write(&mut self, msg: &[u8]) {
self.hasher.write(msg)
@ -255,7 +264,7 @@ impl super::Hasher for SipHasher13 {
}
}
impl<S: Sip> super::Hasher for Hasher<S> {
impl<S: ~const Sip> const super::Hasher for Hasher<S> {
// Note: no integer hashing methods (`write_u*`, `write_i*`) are defined
// for this type. We could add them, copy the `short_write` implementation
// in librustc_data_structures/sip128.rs, and add `write_u*`/`write_i*`
@ -335,7 +344,7 @@ impl<S: Sip> super::Hasher for Hasher<S> {
}
}
impl<S: Sip> Clone for Hasher<S> {
impl<S: Sip> const Clone for Hasher<S> {
#[inline]
fn clone(&self) -> Hasher<S> {
Hasher {
@ -359,6 +368,7 @@ impl<S: Sip> Default for Hasher<S> {
}
#[doc(hidden)]
#[const_trait]
trait Sip {
fn c_rounds(_: &mut State);
fn d_rounds(_: &mut State);
@ -367,7 +377,7 @@ trait Sip {
#[derive(Debug, Clone, Default)]
struct Sip13Rounds;
impl Sip for Sip13Rounds {
impl const Sip for Sip13Rounds {
#[inline]
fn c_rounds(state: &mut State) {
compress!(state);
@ -384,7 +394,7 @@ impl Sip for Sip13Rounds {
#[derive(Debug, Clone, Default)]
struct Sip24Rounds;
impl Sip for Sip24Rounds {
impl const Sip for Sip24Rounds {
#[inline]
fn c_rounds(state: &mut State) {
compress!(state);

View File

@ -112,6 +112,7 @@
#![feature(const_float_bits_conv)]
#![feature(const_float_classify)]
#![feature(const_fmt_arguments_new)]
#![feature(const_hash)]
#![feature(const_heap)]
#![feature(const_convert)]
#![feature(const_index_range_slice_index)]

View File

@ -3161,14 +3161,16 @@ impl DefaultHasher {
#[stable(feature = "hashmap_default_hasher", since = "1.13.0")]
#[inline]
#[allow(deprecated)]
#[rustc_const_unstable(feature = "const_hash", issue = "none")]
#[must_use]
pub fn new() -> DefaultHasher {
pub const fn new() -> DefaultHasher {
DefaultHasher(SipHasher13::new_with_keys(0, 0))
}
}
#[stable(feature = "hashmap_default_hasher", since = "1.13.0")]
impl Default for DefaultHasher {
#[rustc_const_unstable(feature = "const_hash", issue = "none")]
impl const Default for DefaultHasher {
/// Creates a new `DefaultHasher` using [`new`].
/// See its documentation for more.
///
@ -3180,7 +3182,8 @@ impl Default for DefaultHasher {
}
#[stable(feature = "hashmap_default_hasher", since = "1.13.0")]
impl Hasher for DefaultHasher {
#[rustc_const_unstable(feature = "const_hash", issue = "none")]
impl const Hasher for DefaultHasher {
// The underlying `SipHasher13` doesn't override the other
// `write_*` methods, so it's ok not to forward them here.

View File

@ -314,6 +314,7 @@
#![feature(maybe_uninit_uninit_array)]
#![feature(const_maybe_uninit_uninit_array)]
#![feature(const_waker)]
#![feature(const_hash)]
//
// Library features (alloc):
#![feature(alloc_layout_extra)]