Skip to content

Commit 34815f6

Browse files
newAMeldruin
authored andcommitted
Hasher: Add example
This is adapted from the example in core::hash::Hasher.
1 parent 9fa28e7 commit 34815f6

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

src/lib.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,35 @@ pub use crate::murmur3::Hasher as Murmur3Hasher;
5454
mod fnv;
5555
mod murmur3;
5656

57-
/// An extension of [core::hash::Hasher][0] for hashers which use 32 bits.
57+
/// An extension of [`core::hash::Hasher`] for 32-bit hashers.
5858
///
59-
/// For hashers which implement this trait, the standard `finish` method should just return a
60-
/// zero-extended version of the result of `finish32`.
61-
///
62-
/// [0]: https://doc.rust-lang.org/core/hash/trait.Hasher.html
59+
/// For hashers that implement this trait, the [`core::hash::Hasher::finish`] method should return a
60+
/// zero-extended version of the result from [`Hasher::finish32`].
6361
///
6462
/// # Contract
6563
///
66-
/// Implementers of this trait must *not* perform any 64-bit (or 128-bit) operation while computing
64+
/// Implementers of this trait must **not** perform any 64-bit (or 128-bit) operation while computing
6765
/// the hash.
66+
///
67+
/// # Examples
68+
///
69+
/// ```
70+
/// use core::hash::{Hasher as _};
71+
/// use hash32::{FnvHasher, Hasher as _};
72+
///
73+
/// let mut hasher: FnvHasher = Default::default();
74+
///
75+
/// hasher.write_u32(1989);
76+
/// hasher.write_u8(11);
77+
/// hasher.write_u8(9);
78+
/// hasher.write(b"Huh?");
79+
///
80+
/// println!("Hash is {:x}!", hasher.finish32());
81+
/// ```
6882
pub trait Hasher: core::hash::Hasher {
69-
/// The equivalent of [`core::hash::Hasher.finish`][0] for 32-bit hashers.
70-
///
71-
/// This returns the hash directly; `finish` zero-extends it to 64 bits for compatibility.
83+
/// The equivalent of [`core::hash::Hasher::finish`] for 32-bit hashers.
7284
///
73-
/// [0]: https://doc.rust-lang.org/std/hash/trait.Hasher.html#tymethod.finish
85+
/// This returns the hash directly; [`core::hash::Hasher::finish`] zero-extends the `finish32`
86+
/// result to 64-bits for compatibility.
7487
fn finish32(&self) -> u32;
7588
}

0 commit comments

Comments
 (0)