Test const Hash
, fix nits
This commit is contained in:
parent
cebce1e616
commit
56e59bcb27
6 changed files with 60 additions and 21 deletions
|
@ -86,6 +86,7 @@
|
||||||
#![stable(feature = "rust1", since = "1.0.0")]
|
#![stable(feature = "rust1", since = "1.0.0")]
|
||||||
|
|
||||||
use crate::fmt;
|
use crate::fmt;
|
||||||
|
use crate::intrinsics::const_eval_select;
|
||||||
use crate::marker::{self, Destruct};
|
use crate::marker::{self, Destruct};
|
||||||
|
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
|
@ -239,17 +240,22 @@ pub trait Hash {
|
||||||
where
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
{
|
{
|
||||||
//FIXME(const_iter_slice): Revert to for loop
|
//FIXME(const_trait_impl): revert to only a for loop
|
||||||
//for piece in data {
|
fn rt<T: Hash, H: Hasher>(data: &[T], state: &mut H) {
|
||||||
// piece.hash(state);
|
for piece in data {
|
||||||
//}
|
piece.hash(state)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const fn ct<T: ~const Hash, H: ~const Hasher>(data: &[T], state: &mut H) {
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
while i < data.len() {
|
while i < data.len() {
|
||||||
data[i].hash(state);
|
data[i].hash(state);
|
||||||
i += 1;
|
i += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// SAFETY: same behavior, CT just uses while instead of for
|
||||||
|
unsafe { const_eval_select((data, state), ct, rt) };
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Separate module to reexport the macro `Hash` from prelude without the trait `Hash`.
|
// Separate module to reexport the macro `Hash` from prelude without the trait `Hash`.
|
||||||
|
|
|
@ -138,6 +138,7 @@ const unsafe fn u8to64_le(buf: &[u8], start: usize, len: usize) -> u64 {
|
||||||
out |= (unsafe { *buf.get_unchecked(start + i) } as u64) << (i * 8);
|
out |= (unsafe { *buf.get_unchecked(start + i) } as u64) << (i * 8);
|
||||||
i += 1;
|
i += 1;
|
||||||
}
|
}
|
||||||
|
//FIXME(fee1-dead): use debug_assert_eq
|
||||||
debug_assert!(i == len);
|
debug_assert!(i == len);
|
||||||
out
|
out
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,16 +9,19 @@ struct MyHasher {
|
||||||
hash: u64,
|
hash: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for MyHasher {
|
impl const Default for MyHasher {
|
||||||
fn default() -> MyHasher {
|
fn default() -> MyHasher {
|
||||||
MyHasher { hash: 0 }
|
MyHasher { hash: 0 }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Hasher for MyHasher {
|
impl const Hasher for MyHasher {
|
||||||
fn write(&mut self, buf: &[u8]) {
|
fn write(&mut self, buf: &[u8]) {
|
||||||
for byte in buf {
|
// FIXME(const_trait_impl): change to for loop
|
||||||
self.hash += *byte as u64;
|
let mut i = 0;
|
||||||
|
while i < buf.len() {
|
||||||
|
self.hash += buf[i] as u64;
|
||||||
|
i += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn write_str(&mut self, s: &str) {
|
fn write_str(&mut self, s: &str) {
|
||||||
|
@ -32,12 +35,25 @@ impl Hasher for MyHasher {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_writer_hasher() {
|
fn test_writer_hasher() {
|
||||||
fn hash<T: Hash>(t: &T) -> u64 {
|
const fn hash<T: ~const Hash>(t: &T) -> u64 {
|
||||||
let mut s = MyHasher { hash: 0 };
|
let mut s = MyHasher { hash: 0 };
|
||||||
t.hash(&mut s);
|
t.hash(&mut s);
|
||||||
s.finish()
|
s.finish()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const {
|
||||||
|
// FIXME(fee1-dead): assert_eq
|
||||||
|
assert!(hash(&()) == 0);
|
||||||
|
assert!(hash(&5_u8) == 5);
|
||||||
|
assert!(hash(&5_u16) == 5);
|
||||||
|
assert!(hash(&5_u32) == 5);
|
||||||
|
|
||||||
|
assert!(hash(&'a') == 97);
|
||||||
|
|
||||||
|
let s: &str = "a";
|
||||||
|
assert!(hash(&s) == 97 + 0xFF);
|
||||||
|
};
|
||||||
|
|
||||||
assert_eq!(hash(&()), 0);
|
assert_eq!(hash(&()), 0);
|
||||||
|
|
||||||
assert_eq!(hash(&5_u8), 5);
|
assert_eq!(hash(&5_u8), 5);
|
||||||
|
@ -97,7 +113,7 @@ struct CustomHasher {
|
||||||
output: u64,
|
output: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Hasher for CustomHasher {
|
impl const Hasher for CustomHasher {
|
||||||
fn finish(&self) -> u64 {
|
fn finish(&self) -> u64 {
|
||||||
self.output
|
self.output
|
||||||
}
|
}
|
||||||
|
@ -109,27 +125,29 @@ impl Hasher for CustomHasher {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for CustomHasher {
|
impl const Default for CustomHasher {
|
||||||
fn default() -> CustomHasher {
|
fn default() -> CustomHasher {
|
||||||
CustomHasher { output: 0 }
|
CustomHasher { output: 0 }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Hash for Custom {
|
impl const Hash for Custom {
|
||||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
fn hash<H: ~const Hasher>(&self, state: &mut H) {
|
||||||
state.write_u64(self.hash);
|
state.write_u64(self.hash);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_custom_state() {
|
fn test_custom_state() {
|
||||||
fn hash<T: Hash>(t: &T) -> u64 {
|
const fn hash<T: ~const Hash>(t: &T) -> u64 {
|
||||||
let mut c = CustomHasher { output: 0 };
|
let mut c = CustomHasher { output: 0 };
|
||||||
t.hash(&mut c);
|
t.hash(&mut c);
|
||||||
c.finish()
|
c.finish()
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_eq!(hash(&Custom { hash: 5 }), 5);
|
assert_eq!(hash(&Custom { hash: 5 }), 5);
|
||||||
|
|
||||||
|
const { assert!(hash(&Custom { hash: 6 }) == 6) };
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: Instantiated functions with i128 in the signature is not supported in Emscripten.
|
// FIXME: Instantiated functions with i128 in the signature is not supported in Emscripten.
|
||||||
|
|
|
@ -8,7 +8,6 @@ use core::{mem, slice};
|
||||||
struct Bytes<'a>(&'a [u8]);
|
struct Bytes<'a>(&'a [u8]);
|
||||||
|
|
||||||
impl<'a> Hash for Bytes<'a> {
|
impl<'a> Hash for Bytes<'a> {
|
||||||
#[allow(unused_must_use)]
|
|
||||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||||
let Bytes(v) = *self;
|
let Bytes(v) = *self;
|
||||||
state.write(v);
|
state.write(v);
|
||||||
|
@ -24,6 +23,20 @@ fn hash<T: Hash>(x: &T) -> u64 {
|
||||||
hash_with(SipHasher::new(), x)
|
hash_with(SipHasher::new(), x)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
const fn test_const_sip() {
|
||||||
|
let val1 = 0x45;
|
||||||
|
let val2 = 0xfeed;
|
||||||
|
|
||||||
|
const fn const_hash<T: ~const Hash>(x: &T) -> u64 {
|
||||||
|
let mut st = SipHasher::new();
|
||||||
|
x.hash(&mut st);
|
||||||
|
st.finish()
|
||||||
|
}
|
||||||
|
|
||||||
|
assert!(const_hash(&(val1)) != const_hash(&(val2)));
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[allow(unused_must_use)]
|
#[allow(unused_must_use)]
|
||||||
fn test_siphash_1_3() {
|
fn test_siphash_1_3() {
|
||||||
|
|
|
@ -11,6 +11,7 @@
|
||||||
#![feature(const_caller_location)]
|
#![feature(const_caller_location)]
|
||||||
#![feature(const_cell_into_inner)]
|
#![feature(const_cell_into_inner)]
|
||||||
#![feature(const_convert)]
|
#![feature(const_convert)]
|
||||||
|
#![feature(const_hash)]
|
||||||
#![feature(const_heap)]
|
#![feature(const_heap)]
|
||||||
#![feature(const_maybe_uninit_as_mut_ptr)]
|
#![feature(const_maybe_uninit_as_mut_ptr)]
|
||||||
#![feature(const_maybe_uninit_assume_init_read)]
|
#![feature(const_maybe_uninit_assume_init_read)]
|
||||||
|
|
|
@ -314,7 +314,6 @@
|
||||||
#![feature(maybe_uninit_uninit_array)]
|
#![feature(maybe_uninit_uninit_array)]
|
||||||
#![feature(const_maybe_uninit_uninit_array)]
|
#![feature(const_maybe_uninit_uninit_array)]
|
||||||
#![feature(const_waker)]
|
#![feature(const_waker)]
|
||||||
#![feature(const_hash)]
|
|
||||||
//
|
//
|
||||||
// Library features (alloc):
|
// Library features (alloc):
|
||||||
#![feature(alloc_layout_extra)]
|
#![feature(alloc_layout_extra)]
|
||||||
|
@ -353,6 +352,7 @@
|
||||||
//
|
//
|
||||||
// Only for const-ness:
|
// Only for const-ness:
|
||||||
#![feature(const_collections_with_hasher)]
|
#![feature(const_collections_with_hasher)]
|
||||||
|
#![feature(const_hash)]
|
||||||
#![feature(const_io_structs)]
|
#![feature(const_io_structs)]
|
||||||
#![feature(const_ip)]
|
#![feature(const_ip)]
|
||||||
#![feature(const_ipv4)]
|
#![feature(const_ipv4)]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue