1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#![cfg_attr(feature = "nightly", feature(core_intrinsics))]
#![cfg_attr(not(feature = "std"), allow(deprecated))]
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(feature = "std")]
use std::collections::hash_map::DefaultHasher;

#[cfg(not(feature = "std"))]
// TODO: why is DefaultHasher not in core‽
use core::hash::SipHasher as DefaultHasher;

use core::fmt::{self, Debug, Formatter, LowerHex};
use core::hash::{Hash, Hasher};
use core::marker::PhantomData;

#[cfg(feature = "truncate")]
pub use num_traits::AsPrimitive;

#[cfg(not(feature = "truncate"))]
pub trait AsPrimitive<T> {
    fn as_(self) -> T;
}

#[cfg(not(feature = "truncate"))]
impl<T> AsPrimitive<T> for T {
    fn as_(self) -> T {
        self
    }
}

#[cfg(feature = "nightly")]
fn type_name<T: ?Sized>() -> &'static str {
    unsafe { core::intrinsics::type_name::<T>() }
}

#[cfg(not(feature = "nightly"))]
fn type_name<T: ?Sized>() -> &'static str {
    "?"
}

// TODO: be generic over Hasher

#[repr(transparent)]
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
pub struct HashedGeneric<T, V>
where
    T: ?Sized + Hash,
    V: AsPrimitive<u64> + Copy,
    u64: AsPrimitive<V>,
{
    value: V,
    hashee_type: PhantomData<T>,
}

pub type Hashed<T> = HashedGeneric<T, u64>;

#[cfg(feature = "truncate")]
pub type Hashed32<T> = HashedGeneric<T, u32>;
#[cfg(feature = "truncate")]
pub type Hashed16<T> = HashedGeneric<T, u16>;
#[cfg(feature = "truncate")]
pub type Hashed8<T> = HashedGeneric<T, u8>;

impl<T, V> Debug for HashedGeneric<T, V>
where
    T: ?Sized + Hash,
    V: AsPrimitive<u64> + LowerHex + Copy,
    u64: AsPrimitive<V>,
{
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "Hashed<{}>({:x})", type_name::<T>(), self.value)
    }
}

impl<T, V> Default for HashedGeneric<T, V>
where
    T: Hash + Default,
    V: AsPrimitive<u64> + Copy,
    u64: AsPrimitive<V>,
{
    fn default() -> Self {
        T::default().into()
    }
}

impl<T, V> From<T> for HashedGeneric<T, V>
where
    T: Hash,
    V: AsPrimitive<u64> + Copy,
    u64: AsPrimitive<V>,
{
    fn from(hashee: T) -> Self {
        Self::new(&hashee)
    }
}

// Due to the limitations of the Into trait, this always converts into a u64.
// To obtain the inner value as its "native" data type (it may be truncated),
// use the Hashed::value() function instead.
impl<T, V> Into<u64> for HashedGeneric<T, V>
where
    T: ?Sized + Hash,
    V: AsPrimitive<u64> + Copy,
    u64: AsPrimitive<V>,
{
    fn into(self) -> u64 {
        self.value.as_()
    }
}

impl<T, V> HashedGeneric<T, V>
where
    T: ?Sized + Hash,
    V: AsPrimitive<u64> + Copy,
    u64: AsPrimitive<V>,
{
    /// Create a new Hashed<T> from &T. Note that this function doesn't consume
    /// the input, so if it is later modified the hash will not update. To stop
    /// these kinds of errors, it is recommended to use the From or Into traits
    /// instead of this function whenever possible.
    pub fn new(hashee: &T) -> Self {
        let mut hasher = DefaultHasher::new();
        hashee.hash(&mut hasher);
        Self {
            value: hasher.finish().as_(),
            hashee_type: PhantomData,
        }
    }

    /// Get the actual hash value the Hashed<T> is wrapping.
    pub fn value(&self) -> V {
        self.value
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn from_hashed() {
        let _ = Hashed::from("hello");
    }

    #[test]
    fn into_hashed() {
        let _: Hashed<_> = "hello".into();
    }

    #[test]
    #[cfg(feature = "std")]
    fn new_hashed_str() {
        let mut x = String::from("hello");

        let a = Hashed::new(&x); // Hashed::new() allows us to keep using the hashee

        x.push_str("world");

        let b = Hashed::new(&x);

        assert_ne!(a, b);
    }

    #[test]
    fn new_hashed_int() {
        let mut x = 1337;

        let a = Hashed::new(&x);

        x += 30000;

        let b = Hashed::new(&x);

        assert_ne!(a, b);
    }

    #[test]
    fn hashed_eq() {
        let a = 1337;
        let b = 1337;

        assert_eq!(a, b);
        assert_eq!(Hashed::from(a), Hashed::from(b));
    }

    #[test]
    fn hashed_not_eq() {
        let a = "hello";
        let b = "world";

        assert_ne!(a, b);
        assert_ne!(Hashed::from(a), Hashed::from(b));
    }

    #[test]
    fn into_u64() {
        let a = 1337;
        let b = 1337;

        let a_hash_value: u64 = Hashed::from(a).into();
        let b_hash_value: u64 = Hashed::from(b).into();

        assert_eq!(a, b);
        assert_eq!(a_hash_value, b_hash_value);
    }

    #[test]
    #[cfg(feature = "truncate")]
    fn hashed_eq_32() {
        let a = 1337;
        let b = 1337;

        assert_eq!(a, b);
        assert_eq!(Hashed32::from(a), Hashed32::from(b));
    }

    #[test]
    #[cfg(feature = "truncate")]
    fn hashed_eq_16() {
        let a = 1337;
        let b = 1337;

        assert_eq!(a, b);
        assert_eq!(Hashed16::from(a), Hashed16::from(b));
    }

    #[test]
    #[cfg(feature = "truncate")]
    fn hashed_not_eq_32() {
        let a = "hello";
        let b = "world";

        assert_ne!(a, b);
        assert_ne!(Hashed32::from(a), Hashed32::from(b));
    }

    #[test]
    #[cfg(feature = "truncate")]
    fn hashed_not_eq_16() {
        let a = "hello";
        let b = "world";

        assert_ne!(a, b);
        assert_ne!(Hashed16::from(a), Hashed16::from(b));
    }

    // not doing a hashed_eq_8 because 8-bit hashes are rather likely to collide

    #[test]
    fn default_impl() {
        let x: Hashed<_> = Some(1337).into();
        let default = Hashed::default();  // default for Option is None

        assert_ne!(x, default);
    }
}