Skip to content

Commit 1a5bfd2

Browse files
committed
extend hasher mock
1 parent 4b86220 commit 1a5bfd2

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

src/main.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![feature(asm)]
2-
#![feature(test)]
2+
#![feature(test)]#![feature(sip_hash_13)]
33
#![cfg_attr(test, feature(hashmap_hasher))]
44
#![allow(unused_imports, dead_code)]
55

@@ -31,6 +31,19 @@ fn main() {
3131
do_it().unwrap();
3232
}
3333

34+
#[test]
35+
fn test_diff() {
36+
use std::hash::Hasher;
37+
use std::hash::Hash;
38+
let mut hasher1 = sip_opt::SipHasher::default();
39+
let a = (b"12", b"34");
40+
a.hash(&mut hasher1);
41+
let mut hasher2 = sip_opt::SipHasher::default();
42+
let b = (b"123", b"4");
43+
b.hash(&mut hasher2);
44+
assert!(hasher1.finish() != hasher2.finish());
45+
}
46+
3447
struct DataPoint {
3548
magnitude: u64,
3649
average: u64,
@@ -121,7 +134,7 @@ fn do_it() -> IoResult<()> {
121134

122135
macro_rules! hash_benches {
123136
($Impl: ty) => {
124-
use std::hash::SipHasher as Sip;
137+
use std::hash::SipHasher13 as Sip;
125138
use twox_hash::XxHash as Xx;
126139
// use murmurhash64 as murmur2;
127140
// use murmurhash3::Murmur3State as Murmur3State;
@@ -304,10 +317,10 @@ macro_rules! tree_benches {
304317
}
305318

306319
#[cfg(test)] mod sip { hash_benches!{Sip} }
307-
#[cfg(test)] mod xx { hash_benches!{Xx} }
308-
#[cfg(test)] mod farm { hash_benches!{Farm} }
309-
#[cfg(test)] mod fnv { hash_benches!{Fnv} }
310-
#[cfg(test)] mod horner { hash_benches!{HornerHasher} }
320+
// #[cfg(test)] mod xx { hash_benches!{Xx} }
321+
// #[cfg(test)] mod farm { hash_benches!{Farm} }
322+
// #[cfg(test)] mod fnv { hash_benches!{Fnv} }
323+
// #[cfg(test)] mod horner { hash_benches!{HornerHasher} }
311324
#[cfg(test)] mod sip_opt_b { hash_benches!{SipOpt} }
312325

313326
// one day?

src/sip_opt.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ pub struct SipHasher {
4343
v3: u64,
4444
tail: u64, // unprocessed bytes le
4545
ntail: usize, // how many bytes in tail are valid
46+
delimiters: usize,
4647
}
4748

4849
// sadly, these macro definitions can't appear later,
@@ -121,6 +122,7 @@ impl SipHasher {
121122
v3: 0,
122123
tail: 0,
123124
ntail: 0,
125+
delimiters: 0,
124126
};
125127
state.reset();
126128
state
@@ -138,8 +140,21 @@ impl SipHasher {
138140
}
139141

140142
impl Hasher for SipHasher {
143+
#[inline]
144+
fn write_usize(&mut self, i: usize) {
145+
self.delimiters += 1;
146+
if self.delimiters > 1 {
147+
use std;
148+
let bytes = unsafe {
149+
std::slice::from_raw_parts(&i as *const usize as *const u8, std::mem::size_of::<usize>())
150+
};
151+
self.write(bytes);
152+
}
153+
}
154+
141155
#[inline]
142156
fn write(&mut self, msg: &[u8]) {
157+
// println!("write {:?}", msg);
143158
let length = msg.len();
144159
self.length += length;
145160

@@ -220,6 +235,7 @@ impl Clone for SipHasher {
220235
v3: self.v3,
221236
tail: self.tail,
222237
ntail: self.ntail,
238+
delimiters: 0,
223239
}
224240
}
225241
}

0 commit comments

Comments
 (0)