Skip to main content

GlobalState

Struct GlobalState 

Source
pub struct GlobalState<const AVALANCHE: bool, const SPONGE: bool, const COMPACT: bool, const PROTECTED: bool> { /* private fields */ }
Expand description

A BuildHasher that uses a global seed and secrets, randomized only once on startup.

The global seed and secrets are randomized on the first instantiation, and then every subsequent instance of GlobalState will re-use the same seed and secrets, ensuring consistent hash outputs for the duration of the program.

The one-time randomization is derived from OS/platform entropy when the getrandom_04 feature is enabled, or the standard library’s secure RNG when the std feature is enabled, falling back to ASLR and weaker entropy sources otherwise. Because every instance shares a single seed and secret set, GlobalState only offers minimal HashDoS resistance: an attacker cannot predict the secrets, but every map in the process shares the same collision structure. Prefer RandomState when each map should be seeded independently.

See RandomState’s portability docs for enabling true randomisation on wasm32 and embedded targets via the getrandom feature.

§Performance

GlobalState is faster and smaller than RandomState while still providing some minimal DoS resistance, and so may be preferable to RandomState when instantiating many hashmaps in a tight loop.

§Example

use std::collections::HashMap;
use std::hash::Hasher;

use rapidhash::fast::GlobalState;

let mut map = HashMap::with_hasher(GlobalState::default());
map.insert(42, "the answer");

Implementations§

Source§

impl<const AVALANCHE: bool, const SPONGE: bool, const COMPACT: bool, const PROTECTED: bool> GlobalState<AVALANCHE, SPONGE, COMPACT, PROTECTED>

Source

pub fn new() -> Self

Create a new global state with a global seed and secrets.

The seed and secrets are randomized once, on the first instantiation of any GlobalState, and then cached for the lifetime of the program; all subsequent instances share them. With the std feature the one-time randomization is derived from the standard library’s secure RNG; without std it falls back to ASLR and other weaker sources of entropy.

Because every instance shares the same seed and secrets, all maps in the process share the same collision structure. Use RandomState if you need each map to be seeded independently.

On platforms which do not support atomic pointers, the secrets will be the default rapidhash secrets, which are not randomized. Therefore, GlobalState on targets without atomic pointer support has no HashDoS resistance guarantees: there is nowhere to store a randomized value, so even the getrandom_04 feature cannot help. Prefer RandomState with the getrandom_04 feature on these targets, which draws a fresh random seed per instance instead.

Trait Implementations§

Source§

impl<const AVALANCHE: bool, const SPONGE: bool, const COMPACT: bool, const PROTECTED: bool> BuildHasher for GlobalState<AVALANCHE, SPONGE, COMPACT, PROTECTED>

Source§

type Hasher = RapidHasher<'static, AVALANCHE, SPONGE, COMPACT, PROTECTED>

Type of the hasher that will be created.
Source§

fn build_hasher(&self) -> Self::Hasher

Creates a new hasher. Read more
1.71.0 · Source§

fn hash_one<T>(&self, x: T) -> u64
where T: Hash, Self: Sized, Self::Hasher: Hasher,

Calculates the hash of a single value. Read more
Source§

impl<const AVALANCHE: bool, const SPONGE: bool, const COMPACT: bool, const PROTECTED: bool> Clone for GlobalState<AVALANCHE, SPONGE, COMPACT, PROTECTED>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<const AVALANCHE: bool, const SPONGE: bool, const COMPACT: bool, const PROTECTED: bool> Copy for GlobalState<AVALANCHE, SPONGE, COMPACT, PROTECTED>

Source§

impl<const AVALANCHE: bool, const SPONGE: bool, const COMPACT: bool, const PROTECTED: bool> Debug for GlobalState<AVALANCHE, SPONGE, COMPACT, PROTECTED>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<const AVALANCHE: bool, const SPONGE: bool, const COMPACT: bool, const PROTECTED: bool> Default for GlobalState<AVALANCHE, SPONGE, COMPACT, PROTECTED>

Warning that GlobalState only randomizes the seed on platforms that support atomic pointers.

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<const AVALANCHE: bool, const SPONGE: bool, const COMPACT: bool, const PROTECTED: bool> Eq for GlobalState<AVALANCHE, SPONGE, COMPACT, PROTECTED>

Source§

impl<const AVALANCHE: bool, const SPONGE: bool, const COMPACT: bool, const PROTECTED: bool> PartialEq for GlobalState<AVALANCHE, SPONGE, COMPACT, PROTECTED>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<const AVALANCHE: bool, const SPONGE: bool, const COMPACT: bool, const PROTECTED: bool> StructuralPartialEq for GlobalState<AVALANCHE, SPONGE, COMPACT, PROTECTED>

Auto Trait Implementations§

§

impl<const AVALANCHE: bool, const SPONGE: bool, const COMPACT: bool, const PROTECTED: bool> Freeze for GlobalState<AVALANCHE, SPONGE, COMPACT, PROTECTED>

§

impl<const AVALANCHE: bool, const SPONGE: bool, const COMPACT: bool, const PROTECTED: bool> RefUnwindSafe for GlobalState<AVALANCHE, SPONGE, COMPACT, PROTECTED>

§

impl<const AVALANCHE: bool, const SPONGE: bool, const COMPACT: bool, const PROTECTED: bool> Send for GlobalState<AVALANCHE, SPONGE, COMPACT, PROTECTED>

§

impl<const AVALANCHE: bool, const SPONGE: bool, const COMPACT: bool, const PROTECTED: bool> Sync for GlobalState<AVALANCHE, SPONGE, COMPACT, PROTECTED>

§

impl<const AVALANCHE: bool, const SPONGE: bool, const COMPACT: bool, const PROTECTED: bool> Unpin for GlobalState<AVALANCHE, SPONGE, COMPACT, PROTECTED>

§

impl<const AVALANCHE: bool, const SPONGE: bool, const COMPACT: bool, const PROTECTED: bool> UnsafeUnpin for GlobalState<AVALANCHE, SPONGE, COMPACT, PROTECTED>

§

impl<const AVALANCHE: bool, const SPONGE: bool, const COMPACT: bool, const PROTECTED: bool> UnwindSafe for GlobalState<AVALANCHE, SPONGE, COMPACT, PROTECTED>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.