pub struct StableHasher<H: ExtendedHasher> { /* private fields */ }
Expand description
A Stable Hasher adapted for cross-platform independent hash.
When hashing something that ends up affecting properties like symbol names, we want these symbol names to be calculated independently of other factors like what architecture you’re compiling from.
To that end we always convert integers to little-endian format before
hashing and the architecture dependent isize
and usize
types are
extended to 64 bits if needed.
§Example
use rustc_stable_hash::hashers::{StableSipHasher128, SipHasher128Hash};
use rustc_stable_hash::{StableHasher, FromStableHash};
use std::hash::Hasher;
struct Hash128([u64; 2]);
impl FromStableHash for Hash128 {
type Hash = SipHasher128Hash;
fn from(SipHasher128Hash(hash): SipHasher128Hash) -> Hash128 {
Hash128(hash)
}
}
let mut hasher = StableSipHasher128::new();
hasher.write_usize(0xFA);
let hash: Hash128 = hasher.finish();
Implementations§
Source§impl<H: ExtendedHasher + Default> StableHasher<H>
impl<H: ExtendedHasher + Default> StableHasher<H>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new StableHasher
.
To be used with the Hasher
implementation and StableHasher::finish
.
Source§impl<H: ExtendedHasher> StableHasher<H>
impl<H: ExtendedHasher> StableHasher<H>
Sourcepub fn with_hasher(state: H) -> Self
pub fn with_hasher(state: H) -> Self
Creates a new StableHasher
from an already created ExtendedHasher
.
Useful when wanting to initialize a hasher with different parameters/keys.
Important: Any use of the hasher before being given to a StableHasher
is not covered by this crate guarentees and will make the resulting hash
NOT platform independent.
Sourcepub fn finish<W: FromStableHash<Hash = H::Hash>>(self) -> W
pub fn finish<W: FromStableHash<Hash = H::Hash>>(self) -> W
Returns the typed-hash value for the values written.
The resulting typed-hash value is constructed from an
FromStableHash
implemenation.
To be used in-place of Hasher::finish
.
Trait Implementations§
Source§impl<H: Clone + ExtendedHasher> Clone for StableHasher<H>
impl<H: Clone + ExtendedHasher> Clone for StableHasher<H>
Source§fn clone(&self) -> StableHasher<H>
fn clone(&self) -> StableHasher<H>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<H: ExtendedHasher + Debug> Debug for StableHasher<H>
impl<H: ExtendedHasher + Debug> Debug for StableHasher<H>
Source§impl<H: ExtendedHasher + Default> Default for StableHasher<H>
impl<H: ExtendedHasher + Default> Default for StableHasher<H>
Source§fn default() -> Self
fn default() -> Self
Creates a new StableHasher
.
To be used with the Hasher
implementation and StableHasher::finish
.
Source§impl<H: ExtendedHasher> Hasher for StableHasher<H>
impl<H: ExtendedHasher> Hasher for StableHasher<H>
Source§fn finish(&self) -> u64
fn finish(&self) -> u64
Returns a combined hash.
For greater precision use instead StableHasher::finish
.
Source§fn write_u128(&mut self, i: u128)
fn write_u128(&mut self, i: u128)
u128
into this hasher.Source§fn write_usize(&mut self, i: usize)
fn write_usize(&mut self, i: usize)
usize
into this hasher.Source§fn write_i128(&mut self, i: i128)
fn write_i128(&mut self, i: i128)
i128
into this hasher.Source§fn write_isize(&mut self, i: isize)
fn write_isize(&mut self, i: isize)
isize
into this hasher.Source§fn write_length_prefix(&mut self, len: usize)
fn write_length_prefix(&mut self, len: usize)
hasher_prefixfree_extras
)