nohash_hasher/lib.rs
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 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
// Copyright 2018-2020 Parity Technologies (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 or MIT license, at your option.
//
// A copy of the Apache License, Version 2.0 is included in the software as
// LICENSE-APACHE and a copy of the MIT license is included in the software
// as LICENSE-MIT. You may also obtain a copy of the Apache License, Version 2.0
// at https://www.apache.org/licenses/LICENSE-2.0 and a copy of the MIT license
// at https://opensource.org/licenses/MIT.
#![cfg_attr(not(feature = "std"), no_std)]
use core::{fmt, hash::{BuildHasherDefault, Hasher}, marker::PhantomData};
/// A `HashMap` with an integer domain, using `NoHashHasher` to perform no hashing at all.
///
/// # Examples
///
/// See [`IsEnabled`] for use with custom types.
///
/// ```
/// use nohash_hasher::IntMap;
///
/// let mut m: IntMap<u32, bool> = IntMap::default();
///
/// m.insert(0, false);
/// m.insert(1, true);
///
/// assert!(m.contains_key(&0));
/// assert!(m.contains_key(&1));
/// ```
#[cfg(feature = "std")]
pub type IntMap<K, V> = std::collections::HashMap<K, V, BuildNoHashHasher<K>>;
/// A `HashSet` of integers, using `NoHashHasher` to perform no hashing at all.
///
/// # Examples
///
/// See [`IsEnabled`] for use with custom types.
///
/// ```
/// use nohash_hasher::IntSet;
///
/// let mut m = IntSet::default();
///
/// m.insert(0u32);
/// m.insert(1u32);
///
/// assert!(m.contains(&0));
/// assert!(m.contains(&1));
/// ```
#[cfg(feature = "std")]
pub type IntSet<T> = std::collections::HashSet<T, BuildNoHashHasher<T>>;
/// An alias for `BuildHasherDefault` for use with `NoHashHasher`.
///
/// # Examples
///
/// See also [`IntMap`] and [`IntSet`] for some easier usage examples.
///
/// ```
/// use nohash_hasher::BuildNoHashHasher;
/// use std::collections::HashMap;
///
/// let mut m: HashMap::<u8, char, BuildNoHashHasher<u8>> =
/// HashMap::with_capacity_and_hasher(2, BuildNoHashHasher::default());
///
/// m.insert(0, 'a');
/// m.insert(1, 'b');
///
/// assert_eq!(Some(&'a'), m.get(&0));
/// assert_eq!(Some(&'b'), m.get(&1));
/// ```
pub type BuildNoHashHasher<T> = BuildHasherDefault<NoHashHasher<T>>;
/// For an enabled type `T`, a `NoHashHasher<T>` implements `std::hash::Hasher` and
/// uses the value set by one of the `write_{u8, u16, u32, u64, usize, i8, i16, i32,
/// i64, isize}` methods as its hash output.
///
/// `NoHashHasher` does not implement any hashing algorithm and can only be used
/// with types which can be mapped directly to a numeric value. Out of the box
/// `NoHashHasher` is enabled for `u8`, `u16`, `u32`, `u64`, `usize`, `i8`, `i16`,
/// `i32`, `i64`, and `isize`. Types that should be used with `NoHashHasher` need
/// to implement [`IsEnabled`] and by doing so assert that their `Hash` impl invokes
/// *only one* of the `Hasher::write_{u8, u16, u32, u64, usize, i8, i16, i32, i64,
/// isize}` methods *exactly once*.
///
/// # Examples
///
/// See also [`BuildNoHashHasher`], [`IntMap`] and [`IntSet`] for some easier
/// usage examples. See [`IsEnabled`] for use with custom types.
///
/// ```
/// use nohash_hasher::NoHashHasher;
/// use std::{collections::HashMap, hash::BuildHasherDefault};
///
/// let mut m: HashMap::<u8, char, BuildHasherDefault<NoHashHasher<u8>>> =
/// HashMap::with_capacity_and_hasher(2, BuildHasherDefault::default());
///
/// m.insert(0, 'a');
/// m.insert(1, 'b');
///
/// assert_eq!(Some(&'a'), m.get(&0));
/// assert_eq!(Some(&'b'), m.get(&1));
/// ```
#[cfg(debug_assertions)]
pub struct NoHashHasher<T>(u64, bool, PhantomData<T>);
#[cfg(not(debug_assertions))]
pub struct NoHashHasher<T>(u64, PhantomData<T>);
impl<T> fmt::Debug for NoHashHasher<T> {
#[cfg(debug_assertions)]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_tuple("NoHashHasher").field(&self.0).field(&self.1).finish()
}
#[cfg(not(debug_assertions))]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_tuple("NoHashHasher").field(&self.0).finish()
}
}
impl<T> Default for NoHashHasher<T> {
#[cfg(debug_assertions)]
fn default() -> Self {
NoHashHasher(0, false, PhantomData)
}
#[cfg(not(debug_assertions))]
fn default() -> Self {
NoHashHasher(0, PhantomData)
}
}
impl<T> Clone for NoHashHasher<T> {
#[cfg(debug_assertions)]
fn clone(&self) -> Self {
NoHashHasher(self.0, self.1, self.2)
}
#[cfg(not(debug_assertions))]
fn clone(&self) -> Self {
NoHashHasher(self.0, self.1)
}
}
impl<T> Copy for NoHashHasher<T> {}
/// Types which are safe to use with `NoHashHasher`.
///
/// This marker trait is an option for types to enable themselves for use
/// with `NoHashHasher`. In order to be safe, the `Hash` impl needs to
/// satisfy the following constraint:
///
/// > **One of the `Hasher::write_{u8,u16,u32,u64,usize,i8,i16,i32,i64,isize}`
/// methods is invoked exactly once.**
///
/// The best way to ensure this is to write a custom `Hash` impl even when
/// deriving `Hash` for a simple newtype of a single type which itself
/// implements `IsEnabled` may work as well.
///
/// # Example
///
/// ```
/// #[derive(PartialEq, Eq)]
/// struct SomeType(u32);
///
/// impl std::hash::Hash for SomeType {
/// fn hash<H: std::hash::Hasher>(&self, hasher: &mut H) {
/// hasher.write_u32(self.0)
/// }
/// }
///
/// impl nohash_hasher::IsEnabled for SomeType {}
///
/// let mut m = nohash_hasher::IntMap::default();
///
/// m.insert(SomeType(1), 't');
/// m.insert(SomeType(0), 'f');
///
/// assert_eq!(Some(&'t'), m.get(&SomeType(1)));
/// assert_eq!(Some(&'f'), m.get(&SomeType(0)));
/// ```
pub trait IsEnabled {}
impl IsEnabled for u8 {}
impl IsEnabled for u16 {}
impl IsEnabled for u32 {}
impl IsEnabled for u64 {}
impl IsEnabled for usize {}
impl IsEnabled for i8 {}
impl IsEnabled for i16 {}
impl IsEnabled for i32 {}
impl IsEnabled for i64 {}
impl IsEnabled for isize {}
#[cfg(not(debug_assertions))]
impl<T: IsEnabled> Hasher for NoHashHasher<T> {
fn write(&mut self, _: &[u8]) {
panic!("Invalid use of NoHashHasher")
}
fn write_u8(&mut self, n: u8) { self.0 = u64::from(n) }
fn write_u16(&mut self, n: u16) { self.0 = u64::from(n) }
fn write_u32(&mut self, n: u32) { self.0 = u64::from(n) }
fn write_u64(&mut self, n: u64) { self.0 = n }
fn write_usize(&mut self, n: usize) { self.0 = n as u64 }
fn write_i8(&mut self, n: i8) { self.0 = n as u64 }
fn write_i16(&mut self, n: i16) { self.0 = n as u64 }
fn write_i32(&mut self, n: i32) { self.0 = n as u64 }
fn write_i64(&mut self, n: i64) { self.0 = n as u64 }
fn write_isize(&mut self, n: isize) { self.0 = n as u64 }
fn finish(&self) -> u64 { self.0 }
}
#[cfg(debug_assertions)]
impl<T: IsEnabled> Hasher for NoHashHasher<T> {
fn write(&mut self, _: &[u8]) {
panic!("Invalid use of NoHashHasher")
}
fn write_u8(&mut self, n: u8) {
assert!(!self.1, "NoHashHasher: second write attempt detected.");
self.0 = u64::from(n);
self.1 = true
}
fn write_u16(&mut self, n: u16) {
assert!(!self.1, "NoHashHasher: second write attempt detected.");
self.0 = u64::from(n);
self.1 = true
}
fn write_u32(&mut self, n: u32) {
assert!(!self.1, "NoHashHasher: second write attempt detected.");
self.0 = u64::from(n);
self.1 = true
}
fn write_u64(&mut self, n: u64) {
assert!(!self.1, "NoHashHasher: second write attempt detected.");
self.0 = n;
self.1 = true
}
fn write_usize(&mut self, n: usize) {
assert!(!self.1, "NoHashHasher: second write attempt detected.");
self.0 = n as u64;
self.1 = true
}
fn write_i8(&mut self, n: i8) {
assert!(!self.1, "NoHashHasher: second write attempt detected.");
self.0 = n as u64;
self.1 = true
}
fn write_i16(&mut self, n: i16) {
assert!(!self.1, "NoHashHasher: second write attempt detected.");
self.0 = n as u64;
self.1 = true
}
fn write_i32(&mut self, n: i32) {
assert!(!self.1, "NoHashHasher: second write attempt detected.");
self.0 = n as u64;
self.1 = true
}
fn write_i64(&mut self, n: i64) {
assert!(!self.1, "NoHashHasher: second write attempt detected.");
self.0 = n as u64;
self.1 = true
}
fn write_isize(&mut self, n: isize) {
assert!(!self.1, "NoHashHasher: second write attempt detected.");
self.0 = n as u64;
self.1 = true
}
fn finish(&self) -> u64 {
self.0
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ok() {
let mut h1 = NoHashHasher::<u8>::default();
h1.write_u8(42);
assert_eq!(42, h1.finish());
let mut h2 = NoHashHasher::<u16>::default();
h2.write_u16(42);
assert_eq!(42, h2.finish());
let mut h3 = NoHashHasher::<u32>::default();
h3.write_u32(42);
assert_eq!(42, h3.finish());
let mut h4 = NoHashHasher::<u64>::default();
h4.write_u64(42);
assert_eq!(42, h4.finish());
let mut h5 = NoHashHasher::<usize>::default();
h5.write_usize(42);
assert_eq!(42, h5.finish());
let mut h6 = NoHashHasher::<i8>::default();
h6.write_i8(42);
assert_eq!(42, h6.finish());
let mut h7 = NoHashHasher::<i16>::default();
h7.write_i16(42);
assert_eq!(42, h7.finish());
let mut h8 = NoHashHasher::<i32>::default();
h8.write_i32(42);
assert_eq!(42, h8.finish());
let mut h9 = NoHashHasher::<i64>::default();
h9.write_i64(42);
assert_eq!(42, h9.finish());
let mut h10 = NoHashHasher::<isize>::default();
h10.write_isize(42);
assert_eq!(42, h10.finish())
}
#[cfg(debug_assertions)]
#[test]
#[should_panic]
fn u8_double_usage() {
let mut h = NoHashHasher::<u8>::default();
h.write_u8(42);
h.write_u8(43);
}
#[cfg(debug_assertions)]
#[test]
#[should_panic]
fn u16_double_usage() {
let mut h = NoHashHasher::<u16>::default();
h.write_u16(42);
h.write_u16(43);
}
#[cfg(debug_assertions)]
#[test]
#[should_panic]
fn u32_double_usage() {
let mut h = NoHashHasher::<u32>::default();
h.write_u32(42);
h.write_u32(43);
}
#[cfg(debug_assertions)]
#[test]
#[should_panic]
fn u64_double_usage() {
let mut h = NoHashHasher::<u64>::default();
h.write_u64(42);
h.write_u64(43);
}
#[cfg(debug_assertions)]
#[test]
#[should_panic]
fn usize_double_usage() {
let mut h = NoHashHasher::<usize>::default();
h.write_usize(42);
h.write_usize(43);
}
#[cfg(debug_assertions)]
#[test]
#[should_panic]
fn i8_double_usage() {
let mut h = NoHashHasher::<i8>::default();
h.write_i8(42);
h.write_i8(43);
}
#[cfg(debug_assertions)]
#[test]
#[should_panic]
fn i16_double_usage() {
let mut h = NoHashHasher::<i16>::default();
h.write_i16(42);
h.write_i16(43);
}
#[cfg(debug_assertions)]
#[test]
#[should_panic]
fn i32_double_usage() {
let mut h = NoHashHasher::<i32>::default();
h.write_i32(42);
h.write_i32(43);
}
#[cfg(debug_assertions)]
#[test]
#[should_panic]
fn i64_double_usage() {
let mut h = NoHashHasher::<i64>::default();
h.write_i64(42);
h.write_i64(43);
}
#[cfg(debug_assertions)]
#[test]
#[should_panic]
fn isize_double_usage() {
let mut h = NoHashHasher::<isize>::default();
h.write_isize(42);
h.write_isize(43);
}
}