Skip to content

Commit cf29f00

Browse files
committed
no std support
1 parent 66b5863 commit cf29f00

3 files changed

Lines changed: 41 additions & 17 deletions

File tree

tests/hash.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
#![cfg_attr(not(feature = "std"), no_std)]
22

3-
fn do_hash<T: derive_more::core::hash::Hash>(t: &T) -> u64 {
4-
use std::hash::{DefaultHasher, Hasher};
5-
let mut hasher = DefaultHasher::default();
6-
t.hash(&mut hasher);
7-
hasher.finish()
8-
}
9-
mod utils {
10-
pub fn alternate_u32_hash_function<H: derive_more::core::hash::Hasher>(
3+
mod hash_utils;
4+
use hash_utils::do_hash;
5+
6+
pub mod utils {
7+
pub fn alternate_u32_hash_function<H: core::hash::Hasher>(
118
value: &u32,
129
state: &mut H,
1310
) {
@@ -51,6 +48,7 @@ mod structs {
5148
}
5249

5350
mod multi_field {
51+
use super::super::utils;
5452
use crate::do_hash;
5553
use derive_more::Hash;
5654

@@ -66,7 +64,7 @@ mod structs {
6664

6765
#[derive(Hash)]
6866
struct StructWithAlternateHashFunction {
69-
#[hash(with(crate::utils::alternate_u32_hash_function))]
67+
#[hash(with(utils::alternate_u32_hash_function))]
7068
a: u32,
7169
b: &'static str,
7270
c: bool,
@@ -144,6 +142,7 @@ mod structs {
144142
}
145143

146144
mod enums {
145+
use super::utils;
147146
use crate::do_hash;
148147
use derive_more::Hash;
149148

@@ -176,7 +175,7 @@ mod enums {
176175
_skipped: &'static str,
177176
},
178177
B(
179-
#[hash(with(crate::utils::alternate_u32_hash_function))] u32,
178+
#[hash(with(utils::alternate_u32_hash_function))] u32,
180179
#[hash(skip)]
181180
#[allow(unused)]
182181
&'static str,

tests/hash_and_eq.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
#![cfg_attr(not(feature = "std"), no_std)]
22

3-
use core::hash::{DefaultHasher, Hash, Hasher};
4-
5-
fn do_hash<T: Hash>(t: &T) -> u64 {
6-
let mut hasher = DefaultHasher::default();
7-
t.hash(&mut hasher);
8-
hasher.finish()
9-
}
3+
mod hash_utils;
4+
use hash_utils::do_hash;
105

116
mod hash_respects_eq_skip {
127
use super::*;

tests/hash_utils.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#[cfg(feature = "std")]
2+
pub fn do_hash<T: core::hash::Hash>(t: &T) -> u64 {
3+
use std::hash::{DefaultHasher, Hasher};
4+
let mut hasher = DefaultHasher::default();
5+
t.hash(&mut hasher);
6+
hasher.finish()
7+
}
8+
9+
#[cfg(not(feature = "std"))]
10+
pub fn do_hash<T: core::hash::Hash>(t: &T) -> u64 {
11+
use core::hash::Hasher;
12+
// Simple FNV-1a hasher for no_std, for testing purposes only.
13+
struct FnvHasher(u64);
14+
15+
impl core::hash::Hasher for FnvHasher {
16+
fn write(&mut self, bytes: &[u8]) {
17+
for byte in bytes {
18+
self.0 ^= *byte as u64;
19+
self.0 = self.0.wrapping_mul(0x100000001b3);
20+
}
21+
}
22+
fn finish(&self) -> u64 {
23+
self.0
24+
}
25+
}
26+
27+
let mut hasher = FnvHasher(0xcbf29ce484222325);
28+
t.hash(&mut hasher);
29+
hasher.finish()
30+
}

0 commit comments

Comments
 (0)