Skip to content

Commit 5f9899b

Browse files
committed
Made Sip const Hasher
1 parent 3ea4165 commit 5f9899b

File tree

4 files changed

+32
-17
lines changed

4 files changed

+32
-17
lines changed

library/core/src/hash/sip.rs

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ macro_rules! load_int_le {
118118
/// Safety: this performs unchecked indexing of `buf` at `start..start+len`, so
119119
/// that must be in-bounds.
120120
#[inline]
121-
unsafe fn u8to64_le(buf: &[u8], start: usize, len: usize) -> u64 {
121+
const unsafe fn u8to64_le(buf: &[u8], start: usize, len: usize) -> u64 {
122122
debug_assert!(len < 8);
123123
let mut i = 0; // current byte index (from LSB) in the output u64
124124
let mut out = 0;
@@ -138,7 +138,7 @@ unsafe fn u8to64_le(buf: &[u8], start: usize, len: usize) -> u64 {
138138
out |= (unsafe { *buf.get_unchecked(start + i) } as u64) << (i * 8);
139139
i += 1;
140140
}
141-
debug_assert_eq!(i, len);
141+
debug_assert!(i == len);
142142
out
143143
}
144144

@@ -150,8 +150,9 @@ impl SipHasher {
150150
since = "1.13.0",
151151
note = "use `std::collections::hash_map::DefaultHasher` instead"
152152
)]
153+
#[rustc_const_unstable(feature = "const_hash", issue = "none")]
153154
#[must_use]
154-
pub fn new() -> SipHasher {
155+
pub const fn new() -> SipHasher {
155156
SipHasher::new_with_keys(0, 0)
156157
}
157158

@@ -162,8 +163,9 @@ impl SipHasher {
162163
since = "1.13.0",
163164
note = "use `std::collections::hash_map::DefaultHasher` instead"
164165
)]
166+
#[rustc_const_unstable(feature = "const_hash", issue = "none")]
165167
#[must_use]
166-
pub fn new_with_keys(key0: u64, key1: u64) -> SipHasher {
168+
pub const fn new_with_keys(key0: u64, key1: u64) -> SipHasher {
167169
SipHasher(SipHasher24 { hasher: Hasher::new_with_keys(key0, key1) })
168170
}
169171
}
@@ -176,7 +178,8 @@ impl SipHasher13 {
176178
since = "1.13.0",
177179
note = "use `std::collections::hash_map::DefaultHasher` instead"
178180
)]
179-
pub fn new() -> SipHasher13 {
181+
#[rustc_const_unstable(feature = "const_hash", issue = "none")]
182+
pub const fn new() -> SipHasher13 {
180183
SipHasher13::new_with_keys(0, 0)
181184
}
182185

@@ -187,14 +190,15 @@ impl SipHasher13 {
187190
since = "1.13.0",
188191
note = "use `std::collections::hash_map::DefaultHasher` instead"
189192
)]
190-
pub fn new_with_keys(key0: u64, key1: u64) -> SipHasher13 {
193+
#[rustc_const_unstable(feature = "const_hash", issue = "none")]
194+
pub const fn new_with_keys(key0: u64, key1: u64) -> SipHasher13 {
191195
SipHasher13 { hasher: Hasher::new_with_keys(key0, key1) }
192196
}
193197
}
194198

195199
impl<S: Sip> Hasher<S> {
196200
#[inline]
197-
fn new_with_keys(key0: u64, key1: u64) -> Hasher<S> {
201+
const fn new_with_keys(key0: u64, key1: u64) -> Hasher<S> {
198202
let mut state = Hasher {
199203
k0: key0,
200204
k1: key1,
@@ -209,7 +213,7 @@ impl<S: Sip> Hasher<S> {
209213
}
210214

211215
#[inline]
212-
fn reset(&mut self) {
216+
const fn reset(&mut self) {
213217
self.length = 0;
214218
self.state.v0 = self.k0 ^ 0x736f6d6570736575;
215219
self.state.v1 = self.k1 ^ 0x646f72616e646f6d;
@@ -220,7 +224,8 @@ impl<S: Sip> Hasher<S> {
220224
}
221225

222226
#[stable(feature = "rust1", since = "1.0.0")]
223-
impl super::Hasher for SipHasher {
227+
#[rustc_const_unstable(feature = "const_hash", issue = "none")]
228+
impl const super::Hasher for SipHasher {
224229
#[inline]
225230
fn write(&mut self, msg: &[u8]) {
226231
self.0.hasher.write(msg)
@@ -238,7 +243,11 @@ impl super::Hasher for SipHasher {
238243
}
239244

240245
#[unstable(feature = "hashmap_internals", issue = "none")]
241-
impl super::Hasher for SipHasher13 {
246+
#[rustc_const_unstable(feature = "const_hash", issue = "none")]
247+
impl const super::Hasher for SipHasher13
248+
where
249+
Hasher<Sip13Rounds>: ~const super::Hasher,
250+
{
242251
#[inline]
243252
fn write(&mut self, msg: &[u8]) {
244253
self.hasher.write(msg)
@@ -255,7 +264,7 @@ impl super::Hasher for SipHasher13 {
255264
}
256265
}
257266

258-
impl<S: Sip> super::Hasher for Hasher<S> {
267+
impl<S: ~const Sip> const super::Hasher for Hasher<S> {
259268
// Note: no integer hashing methods (`write_u*`, `write_i*`) are defined
260269
// for this type. We could add them, copy the `short_write` implementation
261270
// in librustc_data_structures/sip128.rs, and add `write_u*`/`write_i*`
@@ -335,7 +344,7 @@ impl<S: Sip> super::Hasher for Hasher<S> {
335344
}
336345
}
337346

338-
impl<S: Sip> Clone for Hasher<S> {
347+
impl<S: Sip> const Clone for Hasher<S> {
339348
#[inline]
340349
fn clone(&self) -> Hasher<S> {
341350
Hasher {
@@ -359,6 +368,7 @@ impl<S: Sip> Default for Hasher<S> {
359368
}
360369

361370
#[doc(hidden)]
371+
#[const_trait]
362372
trait Sip {
363373
fn c_rounds(_: &mut State);
364374
fn d_rounds(_: &mut State);
@@ -367,7 +377,7 @@ trait Sip {
367377
#[derive(Debug, Clone, Default)]
368378
struct Sip13Rounds;
369379

370-
impl Sip for Sip13Rounds {
380+
impl const Sip for Sip13Rounds {
371381
#[inline]
372382
fn c_rounds(state: &mut State) {
373383
compress!(state);
@@ -384,7 +394,7 @@ impl Sip for Sip13Rounds {
384394
#[derive(Debug, Clone, Default)]
385395
struct Sip24Rounds;
386396

387-
impl Sip for Sip24Rounds {
397+
impl const Sip for Sip24Rounds {
388398
#[inline]
389399
fn c_rounds(state: &mut State) {
390400
compress!(state);

library/core/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
#![feature(const_float_bits_conv)]
113113
#![feature(const_float_classify)]
114114
#![feature(const_fmt_arguments_new)]
115+
#![feature(const_hash)]
115116
#![feature(const_heap)]
116117
#![feature(const_convert)]
117118
#![feature(const_index_range_slice_index)]

library/std/src/collections/hash/map.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3161,14 +3161,16 @@ impl DefaultHasher {
31613161
#[stable(feature = "hashmap_default_hasher", since = "1.13.0")]
31623162
#[inline]
31633163
#[allow(deprecated)]
3164+
#[rustc_const_unstable(feature = "const_hash", issue = "none")]
31643165
#[must_use]
3165-
pub fn new() -> DefaultHasher {
3166+
pub const fn new() -> DefaultHasher {
31663167
DefaultHasher(SipHasher13::new_with_keys(0, 0))
31673168
}
31683169
}
31693170

31703171
#[stable(feature = "hashmap_default_hasher", since = "1.13.0")]
3171-
impl Default for DefaultHasher {
3172+
#[rustc_const_unstable(feature = "const_hash", issue = "none")]
3173+
impl const Default for DefaultHasher {
31723174
/// Creates a new `DefaultHasher` using [`new`].
31733175
/// See its documentation for more.
31743176
///
@@ -3180,7 +3182,8 @@ impl Default for DefaultHasher {
31803182
}
31813183

31823184
#[stable(feature = "hashmap_default_hasher", since = "1.13.0")]
3183-
impl Hasher for DefaultHasher {
3185+
#[rustc_const_unstable(feature = "const_hash", issue = "none")]
3186+
impl const Hasher for DefaultHasher {
31843187
// The underlying `SipHasher13` doesn't override the other
31853188
// `write_*` methods, so it's ok not to forward them here.
31863189

library/std/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@
314314
#![feature(maybe_uninit_uninit_array)]
315315
#![feature(const_maybe_uninit_uninit_array)]
316316
#![feature(const_waker)]
317+
#![feature(const_hash)]
317318
//
318319
// Library features (alloc):
319320
#![feature(alloc_layout_extra)]

0 commit comments

Comments
 (0)