Skip to content

Add examples for all remaining docstrings. #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/fnv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ const PRIME: u32 = 0x1000193;
/// Specifically this implements the [FNV-1a hash].
///
/// [FNV-1a hash]: https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#FNV-1a_hash
///
/// # Examples
///
/// ```
/// use core::hash::Hasher as _;
/// use hash32::{FnvHasher, Hasher as _};
///
/// let mut hasher: FnvHasher = Default::default();
/// hasher.write(b"Hello, World!");
///
/// println!("Hash is {:x}!", hasher.finish32());
/// ```
#[derive(Debug, Clone)]
pub struct FnvHasher {
state: u32,
Expand Down
34 changes: 33 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,38 @@
//!
//! This crate is guaranteed to compile on latest stable Rust. It *might* compile on older
//! versions but that may change in any new patch release.
//!
//! # Examples
//!
//! ```
//! use hash32::{FnvHasher, Hasher as _};
//!
//! #[derive(Hash)]
//! struct Person {
//! id: u32,
//! name: &'static str,
//! phone: u64,
//! }
//!
//! let person1 = Person {
//! id: 5,
//! name: "Janet",
//! phone: 555_666_7777,
//! };
//! let person2 = Person {
//! id: 5,
//! name: "Bob",
//! phone: 555_666_7777,
//! };
//!
//! assert!(calculate_hash(&person1) != calculate_hash(&person2));
//!
//! fn calculate_hash<T: core::hash::Hash>(t: &T) -> u32 {
//! let mut fnv: FnvHasher = Default::default();
//! t.hash(&mut fnv);
//! fnv.finish32()
//! }
//! ```

#![warn(
missing_docs,
Expand Down Expand Up @@ -86,7 +118,7 @@ mod murmur3;
/// # Examples
///
/// ```
/// use core::hash::{Hasher as _};
/// use core::hash::Hasher as _;
/// use hash32::{FnvHasher, Hasher as _};
///
/// let mut hasher: FnvHasher = Default::default();
Expand Down
12 changes: 12 additions & 0 deletions src/murmur3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@ use core::slice;
use crate::Hasher as _;

/// 32-bit `MurmurHash3` hasher
///
/// # Examples
///
/// ```
/// use core::hash::Hasher as _;
/// use hash32::{Hasher as _, Murmur3Hasher};
///
/// let mut hasher: Murmur3Hasher = Default::default();
/// hasher.write(b"Hello, World!");
///
/// println!("Hash is {:x}!", hasher.finish32());
/// ```
#[derive(Debug, Clone)]
pub struct Murmur3Hasher {
buf: Buffer,
Expand Down