-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add xxhash's xxh32 and xxh64, common trait
- Loading branch information
Showing
10 changed files
with
197 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
pub trait HashWrapper { | ||
type Hasher; | ||
|
||
fn from_hasher(hasher: Self::Hasher) -> Self; | ||
|
||
fn get_hasher(&self) -> &Self::Hasher; | ||
|
||
fn get_hasher_mut(&mut self) -> &mut Self::Hasher; | ||
} | ||
|
||
macro_rules! impl_hash_wrapper { | ||
($hash_wrapper:ident, $hasher:ty, $output_size:ident) => { | ||
impl HashWrapper for $hash_wrapper { | ||
type Hasher = $hasher; | ||
|
||
fn from_hasher(hasher: Self::Hasher) -> Self { | ||
Self(hasher) | ||
} | ||
|
||
fn get_hasher(&self) -> &Self::Hasher { | ||
&self.0 | ||
} | ||
|
||
fn get_hasher_mut(&mut self) -> &mut Self::Hasher { | ||
&mut self.0 | ||
} | ||
} | ||
|
||
impl OutputSizeUser for $hash_wrapper { | ||
type OutputSize = $output_size; | ||
} | ||
|
||
impl HashMarker for $hash_wrapper {} | ||
}; | ||
} | ||
|
||
pub(crate) use impl_hash_wrapper; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
pub use ::xxhash_rust::xxh32::Xxh32 as Xxh32Hasher; | ||
use digest::typenum::U4; | ||
use digest::{FixedOutput, HashMarker, Output, OutputSizeUser, Update}; | ||
|
||
use crate::common::{impl_hash_wrapper, HashWrapper}; | ||
|
||
#[derive(Clone)] | ||
pub struct Xxh32(Xxh32Hasher); | ||
|
||
impl_hash_wrapper!(Xxh32, Xxh32Hasher, U4); | ||
|
||
impl Default for Xxh32 { | ||
fn default() -> Self { | ||
Self(Xxh32Hasher::new(0)) | ||
} | ||
} | ||
|
||
impl Update for Xxh32 { | ||
fn update(&mut self, data: &[u8]) { | ||
self.0.update(data); | ||
} | ||
} | ||
|
||
impl FixedOutput for Xxh32 { | ||
fn finalize_into(self, out: &mut Output<Self>) { | ||
let result = self.0.digest(); | ||
let bytes = result.to_be_bytes(); | ||
out.copy_from_slice(&bytes); | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use insta::assert_snapshot; | ||
use xxhash_rust::xxh32::xxh32; | ||
|
||
use super::*; | ||
use crate::tests::hash; | ||
|
||
#[test] | ||
fn test_xxh32() { | ||
let default = xxh32(&[], 0); | ||
assert_eq!(hash::<Xxh32>(""), format!("{default:0>8X}")); | ||
assert_snapshot!(hash::<Xxh32>(""), @"02CC5D05"); | ||
assert_snapshot!(hash::<Xxh32>("hello"), @"FB0077F9"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
pub use ::xxhash_rust::xxh64::Xxh64 as Xxh64Hasher; | ||
use digest::typenum::U8; | ||
use digest::{FixedOutput, HashMarker, Output, OutputSizeUser, Update}; | ||
|
||
use crate::common::{impl_hash_wrapper, HashWrapper}; | ||
|
||
#[derive(Clone)] | ||
pub struct Xxh64(Xxh64Hasher); | ||
|
||
impl_hash_wrapper!(Xxh64, Xxh64Hasher, U8); | ||
|
||
impl Default for Xxh64 { | ||
fn default() -> Self { | ||
Self(Xxh64Hasher::new(0)) | ||
} | ||
} | ||
|
||
impl Update for Xxh64 { | ||
fn update(&mut self, data: &[u8]) { | ||
self.0.update(data); | ||
} | ||
} | ||
|
||
impl FixedOutput for Xxh64 { | ||
fn finalize_into(self, out: &mut Output<Self>) { | ||
let result = self.0.digest(); | ||
let bytes = result.to_be_bytes(); | ||
out.copy_from_slice(&bytes); | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use insta::assert_snapshot; | ||
use xxhash_rust::xxh64::xxh64; | ||
|
||
use super::*; | ||
use crate::tests::hash; | ||
|
||
#[test] | ||
fn test_xxh64() { | ||
let default = xxh64(&[], 0); | ||
assert_eq!(hash::<Xxh64>(""), format!("{default:0>8X}")); | ||
assert_snapshot!(hash::<Xxh64>(""), @"EF46DB3751D8E999"); | ||
assert_snapshot!(hash::<Xxh64>("hello"), @"26C7827D889F6DA3"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters