Skip to content

Commit

Permalink
Add some documentation as code
Browse files Browse the repository at this point in the history
  • Loading branch information
ogxd committed Nov 9, 2023
1 parent 30e753f commit 8562065
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
18 changes: 18 additions & 0 deletions src/gxhash/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ pub(crate) mod platform;

use platform::*;

/// Hashes an arbitrary stream of bytes to an u32.
///
/// # Example
///
/// ```
/// let bytes = [42u8; 1000];
/// let seed = 1234;
/// println!("Hash is {:x}!", gxhash::gxhash32(bytes, seed));
/// ```
#[inline(always)]
pub fn gxhash32(input: &[u8], seed: i32) -> u32 {
unsafe {
Expand All @@ -10,6 +19,15 @@ pub fn gxhash32(input: &[u8], seed: i32) -> u32 {
}
}

/// Hashes an arbitrary stream of bytes to an u64.
///
/// # Example
///
/// ```
/// let bytes = [42u8; 1000];
/// let seed = 1234;
/// println!("Hash is {:x}!", gxhash::gxhash32(bytes, seed));
/// ```
#[inline(always)]
pub fn gxhash64(input: &[u8], seed: i32) -> u64 {
unsafe {
Expand Down
19 changes: 17 additions & 2 deletions src/hasher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::collections::{HashMap, HashSet};
use crate::gxhash::*;
use crate::gxhash::platform::*;

/// A `Hasher` for hashing an arbitrary stream of bytes.
pub struct GxHasher(State);

impl Default for GxHasher {
Expand All @@ -13,6 +14,22 @@ impl Default for GxHasher {
}

impl GxHasher {
/// Creates a new hasher using the provided seed.
///
/// # Example
///
/// ```
/// use std::hash::Hasher;
/// use gxhash::GxHasher;
///
/// let mut hasher = GxHasher::with_seed(1234);
///
/// hasher.write(b"Hello");
/// hasher.write_u32(123);
/// hasher.write_u8(42);
///
/// println!("Hash is {:x}!", hasher.finish());
/// ```
#[inline]
pub fn with_seed(seed: i32) -> GxHasher {
// Use gxhash64 to generate an initial state from a seed
Expand Down Expand Up @@ -40,11 +57,9 @@ impl Hasher for GxHasher {
pub type GxBuildHasher = BuildHasherDefault<GxHasher>;

/// A `HashMap` using a default GxHash hasher.
//#[cfg(feature = "std")]
pub type GxHashMap<K, V> = HashMap<K, V, GxBuildHasher>;

/// A `HashSet` using a default GxHash hasher.
//#[cfg(feature = "std")]
pub type GxHashSet<T> = HashSet<T, GxBuildHasher>;

#[cfg(test)]
Expand Down

0 comments on commit 8562065

Please sign in to comment.