Skip to content

Commit 56e59bc

Browse files
committed
Test const Hash, fix nits
1 parent cebce1e commit 56e59bc

File tree

6 files changed

+60
-21
lines changed

6 files changed

+60
-21
lines changed

library/core/src/hash/mod.rs

+15-9
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
#![stable(feature = "rust1", since = "1.0.0")]
8787

8888
use crate::fmt;
89+
use crate::intrinsics::const_eval_select;
8990
use crate::marker::{self, Destruct};
9091

9192
#[stable(feature = "rust1", since = "1.0.0")]
@@ -239,16 +240,21 @@ pub trait Hash {
239240
where
240241
Self: Sized,
241242
{
242-
//FIXME(const_iter_slice): Revert to for loop
243-
//for piece in data {
244-
// piece.hash(state);
245-
//}
246-
247-
let mut i = 0;
248-
while i < data.len() {
249-
data[i].hash(state);
250-
i += 1;
243+
//FIXME(const_trait_impl): revert to only a for loop
244+
fn rt<T: Hash, H: Hasher>(data: &[T], state: &mut H) {
245+
for piece in data {
246+
piece.hash(state)
247+
}
248+
}
249+
const fn ct<T: ~const Hash, H: ~const Hasher>(data: &[T], state: &mut H) {
250+
let mut i = 0;
251+
while i < data.len() {
252+
data[i].hash(state);
253+
i += 1;
254+
}
251255
}
256+
// SAFETY: same behavior, CT just uses while instead of for
257+
unsafe { const_eval_select((data, state), ct, rt) };
252258
}
253259
}
254260

library/core/src/hash/sip.rs

+1
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ const 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+
//FIXME(fee1-dead): use debug_assert_eq
141142
debug_assert!(i == len);
142143
out
143144
}

library/core/tests/hash/mod.rs

+28-10
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,19 @@ struct MyHasher {
99
hash: u64,
1010
}
1111

12-
impl Default for MyHasher {
12+
impl const Default for MyHasher {
1313
fn default() -> MyHasher {
1414
MyHasher { hash: 0 }
1515
}
1616
}
1717

18-
impl Hasher for MyHasher {
18+
impl const Hasher for MyHasher {
1919
fn write(&mut self, buf: &[u8]) {
20-
for byte in buf {
21-
self.hash += *byte as u64;
20+
// FIXME(const_trait_impl): change to for loop
21+
let mut i = 0;
22+
while i < buf.len() {
23+
self.hash += buf[i] as u64;
24+
i += 1;
2225
}
2326
}
2427
fn write_str(&mut self, s: &str) {
@@ -32,12 +35,25 @@ impl Hasher for MyHasher {
3235

3336
#[test]
3437
fn test_writer_hasher() {
35-
fn hash<T: Hash>(t: &T) -> u64 {
38+
const fn hash<T: ~const Hash>(t: &T) -> u64 {
3639
let mut s = MyHasher { hash: 0 };
3740
t.hash(&mut s);
3841
s.finish()
3942
}
4043

44+
const {
45+
// FIXME(fee1-dead): assert_eq
46+
assert!(hash(&()) == 0);
47+
assert!(hash(&5_u8) == 5);
48+
assert!(hash(&5_u16) == 5);
49+
assert!(hash(&5_u32) == 5);
50+
51+
assert!(hash(&'a') == 97);
52+
53+
let s: &str = "a";
54+
assert!(hash(&s) == 97 + 0xFF);
55+
};
56+
4157
assert_eq!(hash(&()), 0);
4258

4359
assert_eq!(hash(&5_u8), 5);
@@ -97,7 +113,7 @@ struct CustomHasher {
97113
output: u64,
98114
}
99115

100-
impl Hasher for CustomHasher {
116+
impl const Hasher for CustomHasher {
101117
fn finish(&self) -> u64 {
102118
self.output
103119
}
@@ -109,27 +125,29 @@ impl Hasher for CustomHasher {
109125
}
110126
}
111127

112-
impl Default for CustomHasher {
128+
impl const Default for CustomHasher {
113129
fn default() -> CustomHasher {
114130
CustomHasher { output: 0 }
115131
}
116132
}
117133

118-
impl Hash for Custom {
119-
fn hash<H: Hasher>(&self, state: &mut H) {
134+
impl const Hash for Custom {
135+
fn hash<H: ~const Hasher>(&self, state: &mut H) {
120136
state.write_u64(self.hash);
121137
}
122138
}
123139

124140
#[test]
125141
fn test_custom_state() {
126-
fn hash<T: Hash>(t: &T) -> u64 {
142+
const fn hash<T: ~const Hash>(t: &T) -> u64 {
127143
let mut c = CustomHasher { output: 0 };
128144
t.hash(&mut c);
129145
c.finish()
130146
}
131147

132148
assert_eq!(hash(&Custom { hash: 5 }), 5);
149+
150+
const { assert!(hash(&Custom { hash: 6 }) == 6) };
133151
}
134152

135153
// FIXME: Instantiated functions with i128 in the signature is not supported in Emscripten.

library/core/tests/hash/sip.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use core::{mem, slice};
88
struct Bytes<'a>(&'a [u8]);
99

1010
impl<'a> Hash for Bytes<'a> {
11-
#[allow(unused_must_use)]
1211
fn hash<H: Hasher>(&self, state: &mut H) {
1312
let Bytes(v) = *self;
1413
state.write(v);
@@ -24,6 +23,20 @@ fn hash<T: Hash>(x: &T) -> u64 {
2423
hash_with(SipHasher::new(), x)
2524
}
2625

26+
#[test]
27+
const fn test_const_sip() {
28+
let val1 = 0x45;
29+
let val2 = 0xfeed;
30+
31+
const fn const_hash<T: ~const Hash>(x: &T) -> u64 {
32+
let mut st = SipHasher::new();
33+
x.hash(&mut st);
34+
st.finish()
35+
}
36+
37+
assert!(const_hash(&(val1)) != const_hash(&(val2)));
38+
}
39+
2740
#[test]
2841
#[allow(unused_must_use)]
2942
fn test_siphash_1_3() {

library/core/tests/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#![feature(const_caller_location)]
1212
#![feature(const_cell_into_inner)]
1313
#![feature(const_convert)]
14+
#![feature(const_hash)]
1415
#![feature(const_heap)]
1516
#![feature(const_maybe_uninit_as_mut_ptr)]
1617
#![feature(const_maybe_uninit_assume_init_read)]

library/std/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,6 @@
314314
#![feature(maybe_uninit_uninit_array)]
315315
#![feature(const_maybe_uninit_uninit_array)]
316316
#![feature(const_waker)]
317-
#![feature(const_hash)]
318317
//
319318
// Library features (alloc):
320319
#![feature(alloc_layout_extra)]
@@ -353,6 +352,7 @@
353352
//
354353
// Only for const-ness:
355354
#![feature(const_collections_with_hasher)]
355+
#![feature(const_hash)]
356356
#![feature(const_io_structs)]
357357
#![feature(const_ip)]
358358
#![feature(const_ipv4)]

0 commit comments

Comments
 (0)