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
//
// Copyright (C) 2023 Nathan Sharp.
//
// This Source Code Form is subject to the terms of the Mozilla Public License,
// v. 2.0. If a copy of the MPL was not distributed with this file, You can
// obtain one at https://mozilla.org/MPL/2.0/
//
// This Source Code Form is "Incompatible With Secondary Licenses", as defined
// by the Mozilla Public License, v. 2.0.
//
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(feature = "doc_cfg", feature(doc_cfg))]
//! The `prehash` crate provides the type [`Prehashed`], which stores a value of
//! any type along with a precomputed hash. This makes it possible to avoid
//! computing large expensive hashes many times, for example when searching for
//! a particular value in a variety of hash tables.
//!
//! The crate also defines an extremely simple [`Hasher`], [`Passthru`], which
//! is tailor built for use with [`Prehashed`]. If the `std` feature is
//! selected (which is done by default), type definitions and constructors are
//! provided for the standard [`HashMap`] and [`HashSet`] collections which
//! combine [`Prehashed`] keys/elements with the [`Passthru`] hasher for optimum
//! performance.
//!
//! # Example
//! ```
//! # #[cfg(feature = "std")] {
//! use prehash::{new_prehashed_set, DefaultPrehasher, Prehasher};
//!
//! let prehasher = DefaultPrehasher::new();
//! let mut zeros = new_prehashed_set();
//! let mut evens = new_prehashed_set();
//! let mut odds = new_prehashed_set();
//!
//! // 'evens' and 'odds' are going to re-hash a couple times, but they'll only
//! // need to recompute the hashes of their keys once. (The rehashing process
//! // does provide some resilience against denial-of-service attacks in some
//! // theoretical cases, but the primary form of resilience comes from using an
//! // unpredictable hash function in the first place.)
//!
//! zeros.insert(prehasher.prehash(0));
//!
//! for num in 0..100000 {
//! if num % 2 == 0 {
//! evens.insert(prehasher.prehash(num));
//! } else {
//! odds.insert(prehasher.prehash(num));
//! }
//! }
//!
//! for num in 0..100000 {
//! // We only have to compute the hash of 'num' once, despite checking it
//! // against three different sets. This would be a big deal if computing
//! // the hash of 'num' is expensive.
//! let prehashed = prehasher.prehash(num);
//!
//! if zeros.contains(&prehashed) {
//! assert_eq!(num, 0)
//! }
//!
//! if evens.contains(&prehashed) {
//! assert_eq!(num % 2, 0);
//! }
//!
//! if odds.contains(&prehashed) {
//! assert_eq!(num % 2, 1);
//! }
//! }
//!
//! # }
//! ```
//!
//! # License
//!`prehash` is licensed under the terms of the
//! [Mozilla Public License, v. 2.0][MPL]. All Source Code Forms are
//! "Incompatible With Secondary Licenses", as described in *§3.3* of the
//! license.
//!
//! # Development
//! `prehash` is developed at [GitLab].
//!
//! [GitLab]: https://gitlab.com/nwsharp/prehash
//! [`HashMap`]: std::collections::HashMap
//! [`HashSet`]: std::collections::HashSet
//! [MPL]: https://mozilla.org/MPL/2.0
use core::fmt::{self, Display, Formatter};
use core::hash::{BuildHasher, BuildHasherDefault, Hash, Hasher};
use core::ops::Deref;
#[cfg(feature = "std")]
mod std_utils;
#[cfg(feature = "std")]
pub use std_utils::*;
/// A value (of type `T`) stored along with a precomputed hash (of type `H`,
/// which defaults to [`u64`]).
///
/// When a [hash] of this value is requested, the stored precomputed hash is
/// hashed instead of the value itself. However, when an [equality] test is
/// requested, the contained value is used.
///
/// `Prehashed` values are unaware of the hash algorithm used to compute the
/// hash. Algorithms may misbehave if used with `Prehashed` values created by
/// different [hashers].
///
/// # Example
/// See the [crate-level documentation].
///
/// [crate-level documentation]: crate#Example
/// [equality]: core::cmp::PartialEq::eq
/// [hash]: core::hash::Hash::hash
/// [hashers]: core::hash::Hasher
#[derive(Copy, Debug)]
pub struct Prehashed<T: ?Sized, H = u64> {
hash: H,
value: T,
}
impl<T: ?Sized, H> Prehashed<T, H> {
/// Returns references to the value and its precomputed hash.
///
/// # Invocation
/// This is an associated function, so it is invoked like
/// `Prehashed::as_parts(prehashed)`. This is so this method does not
/// interfere with [automatic dereferencing] as the contained value.
///
/// # Notes
/// If the value or hash are [interiorly mutable], it is possible to cause a
/// mismatch between the precomputed hash and the value. This may cause
/// certain algorithms to malfunction.
///
/// [automatic dereferencing]: core::ops::Deref
/// [interiorly mutable]: core::cell
#[must_use]
pub fn as_parts(pre: &Self) -> (&T, &H) {
(&pre.value, &pre.hash)
}
/// Returns a reference to the contained value.
///
/// # Invocation
/// This is an associated function, so it is invoked like
/// `Prehashed::as_inner(prehashed)`. This is so this method does not
/// interfere with [automatic dereferencing] as the contained value.
///
/// # Notes
/// If the value is [interiorly mutable], it is possible to cause a mismatch
/// between the precomputed hash and the value. This may cause certain
/// algorithms to malfunction.
///
/// [automatic dereferencing]: core::ops::Deref
/// [interiorly mutable]: core::cell
#[must_use]
pub fn as_inner(pre: &Self) -> &T {
Self::as_parts(pre).0
}
/// Returns a reference to the contained hash.
///
/// # Invocation
/// This is an associated function, so it is invoked like
/// `Prehashed::as_hash(prehashed)`. This is so this method does not
/// interfere with [automatic dereferencing] as the contained value.
///
/// # Notes
/// If the hash is [interiorly mutable], it is possible to cause a mismatch
/// between the precomputed hash and the value. This may cause certain
/// algorithms to malfunction.
///
/// [automatic dereferencing]: core::ops::Deref
/// [interiorly mutable]: core::cell
#[must_use]
pub fn as_hash(pre: &Self) -> &H {
Self::as_parts(pre).1
}
}
impl<T, H> Prehashed<T, H> {
/// Creates a new `Prehashed` object from the specified value and
/// precomputed hash.
#[must_use]
pub const fn new(value: T, hash: H) -> Self {
Self { hash, value }
}
/// Deconstructs the `Prehashed` object into the value and its precomputed
/// hash.
///
/// These parts can be passed to [`new`] to reconstruct the original
/// `Prehashed` object.
///
/// # Invocation
/// This is an associated function, so it is invoked like
/// `Prehashed::into_parts(prehashed)`. This is so this method does not
/// interfere with [automatic dereferencing] as the contained value.
///
/// [automatic dereferencing]: core::ops::Deref
/// [`new`]: Prehashed::new
#[must_use]
pub fn into_parts(pre: Self) -> (T, H) {
(pre.value, pre.hash)
}
/// Deconstructs the `Prehashed` object and returns the contained value.
///
/// The precomputed hash is [discarded].
///
/// # Invocation
/// This is an associated function, so it is invoked like
/// `Prehashed::into_inner(prehashed)`. This is so this method does not
/// interfere with [automatic dereferencing] as the contained value.
///
/// [automatic dereferencing]: core::ops::Deref
/// [discarded]: core::ops::Drop::drop
#[must_use]
pub fn into_inner(pre: Self) -> T {
Self::into_parts(pre).0
}
}
impl<T: Hash> Prehashed<T, u64> {
/// Constructs a `Prehashed` object by hashing a value with the
/// [default hasher].
///
/// # Security
/// Consider using [`with_builder`] if [collision-based denial of service
/// attacks][DoS] are a potential concern.
///
/// [DoS]: https://en.wikipedia.org/w/index.php?title=Collision_attack&oldid=971803090#Usage_in_DoS_attacks
/// [default hasher]: std::collections::hash_map::DefaultHasher
/// [`with_builder`]: Self::with_builder
#[cfg(feature = "std")]
#[cfg_attr(feature = "doc_cfg", doc(cfg(feature = "std")))]
#[must_use]
pub fn with_default(value: T) -> Self {
Self::with_hasher::<std::collections::hash_map::DefaultHasher>(value)
}
/// Constructs a `Prehashed` object by hashing a value with a default
/// instance of the specified [`Hasher`] type, `H`.
///
/// # Invocation
/// You will need to use "turbofish operator" to invoke this method:
/// `Prehashed::with_hasher::<Hasher>(value)`
///
/// # Security
/// Consider using [`with_builder`] if [collision-based denial of service
/// attacks][DoS] are a potential concern.
///
/// [DoS]: https://en.wikipedia.org/w/index.php?title=Collision_attack&oldid=971803090#Usage_in_DoS_attacks
/// [`Hasher`]: core::hash::Hasher
/// [`with_builder`]: Self::with_builder
#[must_use]
pub fn with_hasher<H: Default + Hasher>(value: T) -> Self {
Self::with_builder(value, &BuildHasherDefault::<H>::default())
}
/// Constructs a `Prehashed` object by hashing a value with a hasher created
/// the specified builder.
#[must_use]
pub fn with_builder<B: BuildHasher + ?Sized>(value: T, build: &B) -> Self {
let mut hasher = build.build_hasher();
value.hash(&mut hasher);
Self::new(value, hasher.finish())
}
}
impl<T: PartialEq + ?Sized, H: Eq> Prehashed<T, H> {
/// Compares two `Prehashed` values for equality, using the precomputed hash
/// to quickly test for inequality.
///
/// # Invocation
/// This is an associated function, so it is invoked like
/// `Prehashed::fast_eq(a, b)`. This is so this method does not interfere
/// with [automatic dereferencing] as the contained value.
///
/// # Notes
/// The [`PartialEq`] implementation for `Prehashed` does not check the hash
/// value for early inequality because this would slow down algorithms which
/// execute a deep equality test only once a hash equality test has
/// indicated possible equivalence. In that sense, `fast_eq` strictly
/// slower if the hashes have already been tested for equality.
///
/// [automatic dereferencing]: core::ops::Deref
/// [`PartialEq`]: core::cmp::PartialEq
#[must_use]
pub fn fast_eq(lhs: &Self, rhs: &Self) -> bool {
lhs.hash.eq(&rhs.hash) && lhs.value.eq(&rhs.value)
}
}
impl<T: ?Sized, H> AsRef<T> for Prehashed<T, H> {
fn as_ref(&self) -> &T {
Self::as_inner(self)
}
}
impl<T: Copy + ?Sized, H: Copy> Clone for Prehashed<T, H> {
fn clone(&self) -> Self {
*self
}
}
impl<T: ?Sized, H> Deref for Prehashed<T, H> {
type Target = T;
fn deref(&self) -> &Self::Target {
Self::as_inner(self)
}
}
impl<T: Display + ?Sized, H> Display for Prehashed<T, H> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
self.value.fmt(f)
}
}
impl<T: Eq + ?Sized, H> Eq for Prehashed<T, H> {}
impl<T: ?Sized, H: Hash> Hash for Prehashed<T, H> {
fn hash<S: Hasher>(&self, state: &mut S) {
self.hash.hash(state)
}
}
impl<T: PartialEq + ?Sized, H> PartialEq for Prehashed<T, H> {
fn eq(&self, other: &Self) -> bool {
self.value.eq(&other.value)
}
}
impl<T: PartialEq + ?Sized, H> PartialEq<T> for Prehashed<T, H> {
fn eq(&self, other: &T) -> bool {
self.value.eq(other)
}
}
/// A convenience trait for producing `Prehashed` values from any
/// [hasher builder].
///
/// To take advantage of this convenience trait, simply write:
/// ```
/// use prehash::Prehasher;
/// ```
///
/// [hasher builder]: core::hash::BuildHasher
pub trait Prehasher {
/// The type produced by hashing.
type Hash;
/// Create a prehashed value by hashing the specified value.
#[must_use]
fn prehash<T: Hash>(&self, value: T) -> Prehashed<T, Self::Hash>;
}
impl<B: BuildHasher> Prehasher for B {
type Hash = u64;
fn prehash<T: Hash>(&self, value: T) -> Prehashed<T, Self::Hash> {
Prehashed::with_builder(value, self)
}
}
/// A [`Hasher`] implementation which returns the last [`u64`] written.
///
/// ⚠️ All methods besides [`write_u64`] and [`finish`] panic if invoked.
///
/// [`finish`]: Passthru::finish
/// [`Hasher`]: core::hash::Hasher
/// [`write_u64`]: Passthru::write_u64
#[derive(Debug)]
pub struct Passthru {
hash: u64,
}
impl Passthru {
/// Constructs a new pass-through hasher.
///
/// # Notes
/// The hasher will return `0` from [`finish`] if [`write_u64`] is never
/// called.
///
/// [`finish`]: Passthru::finish
/// [`write_u64`]: Passthru::write_u64
#[must_use]
pub fn new() -> Self {
Self { hash: 0 }
}
}
impl Default for Passthru {
fn default() -> Self {
Self::new()
}
}
impl Hasher for Passthru {
fn write(&mut self, _bytes: &[u8]) {
panic!("unsupported operation");
}
fn write_u64(&mut self, i: u64) {
self.hash = i;
}
fn finish(&self) -> u64 {
self.hash
}
}