diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:19:13 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:19:13 +0000 |
commit | 218caa410aa38c29984be31a5229b9fa717560ee (patch) | |
tree | c54bd55eeb6e4c508940a30e94c0032fbd45d677 /vendor/twox-hash/src/digest_support.rs | |
parent | Releasing progress-linux version 1.67.1+dfsg1-1~progress7.99u1. (diff) | |
download | rustc-218caa410aa38c29984be31a5229b9fa717560ee.tar.xz rustc-218caa410aa38c29984be31a5229b9fa717560ee.zip |
Merging upstream version 1.68.2+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/twox-hash/src/digest_support.rs')
-rw-r--r-- | vendor/twox-hash/src/digest_support.rs | 179 |
1 files changed, 179 insertions, 0 deletions
diff --git a/vendor/twox-hash/src/digest_support.rs b/vendor/twox-hash/src/digest_support.rs new file mode 100644 index 000000000..7b00b9d80 --- /dev/null +++ b/vendor/twox-hash/src/digest_support.rs @@ -0,0 +1,179 @@ +use core::hash::Hasher; + +use digest::{ + generic_array::{ + typenum::consts::{U16, U4, U8}, + GenericArray, + }, + Digest, +}; + +use crate::{xxh3, XxHash32, XxHash64}; + +impl Digest for XxHash32 { + type OutputSize = U4; + + fn new() -> Self { + Self::default() + } + + fn input<B: AsRef<[u8]>>(&mut self, data: B) { + self.write(data.as_ref()); + } + + fn chain<B: AsRef<[u8]>>(mut self, data: B) -> Self + where + Self: Sized, + { + self.input(data); + self + } + + fn result(self) -> GenericArray<u8, Self::OutputSize> { + self.finish().to_be_bytes().into() + } + + fn result_reset(&mut self) -> GenericArray<u8, Self::OutputSize> { + let result = self.result(); + self.reset(); + result + } + + fn reset(&mut self) { + *self = Self::default(); + } + + fn output_size() -> usize { + 4 + } + + fn digest(data: &[u8]) -> GenericArray<u8, Self::OutputSize> { + Self::new().chain(data).result() + } +} + +impl Digest for XxHash64 { + type OutputSize = U8; + + fn new() -> Self { + Self::default() + } + + fn input<B: AsRef<[u8]>>(&mut self, data: B) { + self.write(data.as_ref()); + } + + fn chain<B: AsRef<[u8]>>(mut self, data: B) -> Self + where + Self: Sized, + { + self.input(data); + self + } + + fn result(self) -> GenericArray<u8, Self::OutputSize> { + self.finish().to_be_bytes().into() + } + + fn result_reset(&mut self) -> GenericArray<u8, Self::OutputSize> { + let result = self.result(); + self.reset(); + result + } + + fn reset(&mut self) { + *self = Self::default(); + } + + fn output_size() -> usize { + 8 + } + + fn digest(data: &[u8]) -> GenericArray<u8, Self::OutputSize> { + Self::new().chain(data).result() + } +} + +impl Digest for xxh3::Hash64 { + type OutputSize = U8; + + fn new() -> Self { + Self::default() + } + + fn input<B: AsRef<[u8]>>(&mut self, data: B) { + self.write(data.as_ref()); + } + + fn chain<B: AsRef<[u8]>>(mut self, data: B) -> Self + where + Self: Sized, + { + self.input(data); + self + } + + fn result(self) -> GenericArray<u8, Self::OutputSize> { + self.finish().to_be_bytes().into() + } + + fn result_reset(&mut self) -> GenericArray<u8, Self::OutputSize> { + let result = self.clone().result(); + self.reset(); + result + } + + fn reset(&mut self) { + *self = Self::default(); + } + + fn output_size() -> usize { + 8 + } + + fn digest(data: &[u8]) -> GenericArray<u8, Self::OutputSize> { + Self::new().chain(data).result() + } +} + +impl Digest for xxh3::Hash128 { + type OutputSize = U16; + + fn new() -> Self { + Self::default() + } + + fn input<B: AsRef<[u8]>>(&mut self, data: B) { + self.write(data.as_ref()); + } + + fn chain<B: AsRef<[u8]>>(mut self, data: B) -> Self + where + Self: Sized, + { + self.input(data); + self + } + + fn result(self) -> GenericArray<u8, Self::OutputSize> { + xxh3::HasherExt::finish_ext(&self).to_be_bytes().into() + } + + fn result_reset(&mut self) -> GenericArray<u8, Self::OutputSize> { + let result = self.clone().result(); + self.reset(); + result + } + + fn reset(&mut self) { + *self = Self::default(); + } + + fn output_size() -> usize { + 8 + } + + fn digest(data: &[u8]) -> GenericArray<u8, Self::OutputSize> { + Self::new().chain(data).result() + } +} |