rhai/src/func/hashing.rs

138 lines
3.6 KiB
Rust
Raw Normal View History

2021-06-16 12:36:33 +02:00
//! Module containing utilities to hash functions and function calls.
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
use std::{
any::TypeId,
hash::{BuildHasher, Hash, Hasher},
iter::empty,
};
/// A hasher that only takes one single [`u64`] and returns it as a hash key.
///
/// # Panics
///
/// Panics when hashing any data type other than a [`u64`].
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
2021-11-16 05:26:37 +01:00
struct StraightHasher(u64);
2021-06-16 12:36:33 +02:00
impl Hasher for StraightHasher {
#[inline(always)]
fn finish(&self) -> u64 {
self.0
}
#[inline]
2021-06-16 12:36:33 +02:00
fn write(&mut self, bytes: &[u8]) {
assert_eq!(bytes.len(), 8, "StraightHasher can only hash u64 values");
let mut key = [0_u8; 8];
key.copy_from_slice(bytes);
self.0 = u64::from_ne_bytes(key);
}
}
/// A hash builder for `StraightHasher`.
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, Default)]
2021-11-16 05:26:37 +01:00
struct StraightHasherBuilder;
2021-06-16 12:36:33 +02:00
impl BuildHasher for StraightHasherBuilder {
type Hasher = StraightHasher;
#[inline(always)]
fn build_hasher(&self) -> Self::Hasher {
StraightHasher(42)
}
}
/// Create an instance of the default hasher.
#[inline(always)]
#[must_use]
pub fn get_hasher() -> ahash::AHasher {
2021-11-27 07:24:06 +01:00
ahash::AHasher::default()
2021-06-16 12:36:33 +02:00
}
2021-06-30 10:28:37 +02:00
/// Calculate a [`u64`] hash key from a namespace-qualified variable name.
///
/// Module names are passed in via `&str` references from an iterator.
/// Parameter types are passed in via [`TypeId`] values from an iterator.
///
/// # Note
///
/// The first module name is skipped. Hashing starts from the _second_ module in the chain.
#[inline]
#[must_use]
2021-07-10 04:11:14 +02:00
pub fn calc_qualified_var_hash<'a>(modules: impl Iterator<Item = &'a str>, var_name: &str) -> u64 {
2021-06-30 10:28:37 +02:00
let s = &mut get_hasher();
// We always skip the first module
let mut len = 0;
modules
.inspect(|_| len += 1)
.skip(1)
.for_each(|m| m.hash(s));
len.hash(s);
2021-07-10 04:11:14 +02:00
var_name.hash(s);
2021-06-30 10:28:37 +02:00
s.finish()
}
2021-06-16 12:36:33 +02:00
/// Calculate a [`u64`] hash key from a namespace-qualified function name
/// and the number of parameters, but no parameter types.
///
/// Module names are passed in via `&str` references from an iterator.
/// Parameter types are passed in via [`TypeId`] values from an iterator.
///
/// # Note
///
/// The first module name is skipped. Hashing starts from the _second_ module in the chain.
#[inline]
#[must_use]
pub fn calc_qualified_fn_hash<'a>(
modules: impl Iterator<Item = &'a str>,
2021-07-10 04:11:14 +02:00
fn_name: &str,
2021-06-16 12:36:33 +02:00
num: usize,
) -> u64 {
let s = &mut get_hasher();
// We always skip the first module
let mut len = 0;
modules
.inspect(|_| len += 1)
.skip(1)
.for_each(|m| m.hash(s));
len.hash(s);
2021-07-10 04:11:14 +02:00
fn_name.hash(s);
2021-06-16 12:36:33 +02:00
num.hash(s);
s.finish()
}
/// Calculate a [`u64`] hash key from a non-namespace-qualified function name
/// and the number of parameters, but no parameter types.
///
/// Parameter types are passed in via [`TypeId`] values from an iterator.
#[inline(always)]
#[must_use]
2021-07-10 04:11:14 +02:00
pub fn calc_fn_hash(fn_name: &str, num: usize) -> u64 {
2021-06-16 12:36:33 +02:00
calc_qualified_fn_hash(empty(), fn_name, num)
}
/// Calculate a [`u64`] hash key from a list of parameter types.
///
/// Parameter types are passed in via [`TypeId`] values from an iterator.
#[inline]
#[must_use]
pub fn calc_fn_params_hash(params: impl Iterator<Item = TypeId>) -> u64 {
let s = &mut get_hasher();
let mut len = 0;
2021-11-16 06:15:43 +01:00
params.inspect(|_| len += 1).for_each(|t| t.hash(s));
2021-06-16 12:36:33 +02:00
len.hash(s);
s.finish()
}
/// Combine two [`u64`] hashes by taking the XOR of them.
#[inline(always)]
#[must_use]
2021-11-16 05:26:37 +01:00
pub const fn combine_hashes(a: u64, b: u64) -> u64 {
2021-06-16 12:36:33 +02:00
a ^ b
}