Skip to content

Commit

Permalink
Rename StableHasherResult to FromStableHash
Browse files Browse the repository at this point in the history
  • Loading branch information
Urgau authored and michaelwoerister committed Jul 9, 2024
1 parent 5185cef commit 3805516
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Unreleased

- Rename `StableHasherResult` to `FromStableHash` (#8)
- Use new-type for returned-hash of `SipHasher128`(`Hash`) (#8)
- Introduce multi hasher support (#8)
- `StableHasher::finish` now returns a small hash instead of being fatal (#6)
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub mod hashers {
pub use stable_hasher::StableHasher;

#[doc(inline)]
pub use stable_hasher::StableHasherResult;
pub use stable_hasher::FromStableHash;

#[doc(inline)]
pub use stable_hasher::ExtendedHasher;
Expand Down
28 changes: 14 additions & 14 deletions src/stable_hasher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ pub trait ExtendedHasher: Hasher {
///
/// ```
/// use rustc_stable_hash::hashers::{StableSipHasher128, SipHasher128Hash};
/// use rustc_stable_hash::{StableHasher, StableHasherResult};
/// use rustc_stable_hash::{StableHasher, FromStableHash};
/// use std::hash::Hasher;
///
/// struct Hash128([u64; 2]);
/// impl StableHasherResult for Hash128 {
/// impl FromStableHash for Hash128 {
/// type Hash = SipHasher128Hash;
///
/// fn finish(SipHasher128Hash(hash): SipHasher128Hash) -> Hash128 {
/// fn from(SipHasher128Hash(hash): SipHasher128Hash) -> Hash128 {
/// Hash128(hash)
/// }
/// }
Expand All @@ -92,32 +92,32 @@ pub struct StableHasher<H: ExtendedHasher> {
state: H,
}

/// Trait for retrieving the result of the stable hashing operation.
/// Trait for processing the result of the stable hashing operation.
///
/// # Example
///
/// ```
/// use rustc_stable_hash::{StableHasher, StableHasherResult};
/// use rustc_stable_hash::{StableHasher, FromStableHash};
///
/// struct Hash128(u128);
///
/// impl StableHasherResult for Hash128 {
/// impl FromStableHash for Hash128 {
/// type Hash = [u64; 2];
///
/// fn finish(hash: [u64; 2]) -> Hash128 {
/// fn from(hash: [u64; 2]) -> Hash128 {
/// let upper: u128 = hash[0] as u128;
/// let lower: u128 = hash[1] as u128;
///
/// Hash128((upper << 64) | lower)
/// }
/// }
/// ```
pub trait StableHasherResult: Sized {
pub trait FromStableHash: Sized {
type Hash;

/// Retrieving the finalized state of the [`StableHasher`] and construct
/// an [`Self`] containing the hash.
fn finish(hash: Self::Hash) -> Self;
/// Convert the finalized state of a [`StableHasher`] and construct
/// an [`Self`] containing the processed hash.
fn from(hash: Self::Hash) -> Self;
}

impl<H: ExtendedHasher + Default> StableHasher<H> {
Expand Down Expand Up @@ -161,13 +161,13 @@ impl<H: ExtendedHasher> StableHasher<H> {
/// Returns the typed-hash value for the values written.
///
/// The resulting typed-hash value is constructed from an
/// [`StableHasherResult`] implemenation.
/// [`FromStableHash`] implemenation.
///
/// To be used in-place of [`Hasher::finish`].
#[inline]
#[must_use]
pub fn finish<W: StableHasherResult<Hash = H::Hash>>(self) -> W {
W::finish(self.state.finish())
pub fn finish<W: FromStableHash<Hash = H::Hash>>(self) -> W {
W::from(self.state.finish())
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/stable_hasher/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ use crate::{SipHasher128Hash, StableSipHasher128};
#[derive(Debug, PartialEq)]
struct TestHash([u64; 2]);

impl StableHasherResult for TestHash {
impl FromStableHash for TestHash {
type Hash = SipHasher128Hash;

fn finish(SipHasher128Hash(hash): Self::Hash) -> TestHash {
fn from(SipHasher128Hash(hash): Self::Hash) -> TestHash {
TestHash(hash)
}
}
Expand Down

0 comments on commit 3805516

Please sign in to comment.