Skip to content

Commit

Permalink
add BuildHasher and Hasher impls for the Crc structs
Browse files Browse the repository at this point in the history
  • Loading branch information
Moritz Borcherding committed Oct 5, 2023
1 parent c6d99b5 commit 734bcdd
Show file tree
Hide file tree
Showing 21 changed files with 383 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/crc128/bytewise.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::table::crc128_table;
use crate::{Algorithm, Bytewise, Crc, Digest};
use core::hash::{BuildHasher, Hasher};

use super::{finalize, init, update_bytewise};

Expand Down Expand Up @@ -47,3 +48,21 @@ impl<'a> Digest<'a, Bytewise<u128>> {
finalize(self.crc.algorithm, self.value)
}
}

impl<'a> Hasher for Digest<'a, Bytewise<u128>> {
fn finish(&self) -> u64 {
self.clone().finalize() as u64
}

fn write(&mut self, bytes: &[u8]) {
self.update(bytes);
}
}

impl<'a> BuildHasher for &'a Crc<Bytewise<u128>> {
type Hasher = Digest<'a, Bytewise<u128>>;

fn build_hasher(&self) -> Self::Hasher {
self.digest()
}
}
19 changes: 19 additions & 0 deletions src/crc128/default.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::crc128::{finalize, init};
use crate::{Algorithm, Crc, Digest, Implementation};
use core::hash::{BuildHasher, Hasher};

#[cfg(feature = "no-table-mem-limit")]
impl Implementation for u128 {
Expand Down Expand Up @@ -124,3 +125,21 @@ impl<'a> Digest<'a, u128> {
finalize(self.crc.algorithm, self.value)
}
}

impl<'a> Hasher for Digest<'a, u128> {
fn finish(&self) -> u64 {
self.clone().finalize() as u64
}

fn write(&mut self, bytes: &[u8]) {
self.update(bytes);
}
}

impl<'a> BuildHasher for &'a Crc<u128> {
type Hasher = Digest<'a, u128>;

fn build_hasher(&self) -> Self::Hasher {
self.digest()
}
}
19 changes: 19 additions & 0 deletions src/crc128/nolookup.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{Algorithm, Crc, Digest, NoTable};
use core::hash::{BuildHasher, Hasher};

use super::{finalize, init, update_nolookup};

Expand Down Expand Up @@ -48,3 +49,21 @@ impl<'a> Digest<'a, NoTable<u128>> {
finalize(self.crc.algorithm, self.value)
}
}

impl<'a> Hasher for Digest<'a, NoTable<u128>> {
fn finish(&self) -> u64 {
self.clone().finalize() as u64
}

fn write(&mut self, bytes: &[u8]) {
self.update(bytes);
}
}

impl<'a> BuildHasher for &'a Crc<NoTable<u128>> {
type Hasher = Digest<'a, NoTable<u128>>;

fn build_hasher(&self) -> Self::Hasher {
self.digest()
}
}
19 changes: 19 additions & 0 deletions src/crc128/slice16.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::table::crc128_table_slice_16;
use crate::{Algorithm, Crc, Digest, Slice16};
use core::hash::{BuildHasher, Hasher};

use super::{finalize, init, update_slice16};

Expand Down Expand Up @@ -47,3 +48,21 @@ impl<'a> Digest<'a, Slice16<u128>> {
finalize(self.crc.algorithm, self.value)
}
}

impl<'a> Hasher for Digest<'a, Slice16<u128>> {
fn finish(&self) -> u64 {
self.clone().finalize() as u64
}

fn write(&mut self, bytes: &[u8]) {
self.update(bytes);
}
}

impl<'a> BuildHasher for &'a Crc<Slice16<u128>> {
type Hasher = Digest<'a, Slice16<u128>>;

fn build_hasher(&self) -> Self::Hasher {
self.digest()
}
}
19 changes: 19 additions & 0 deletions src/crc16/bytewise.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::crc16::{finalize, init, update_bytewise};
use crate::table::crc16_table;
use crate::{Algorithm, Bytewise, Crc, Digest};
use core::hash::{BuildHasher, Hasher};

impl Crc<Bytewise<u16>> {
pub const fn new(algorithm: &'static Algorithm<u16>) -> Self {
Expand Down Expand Up @@ -46,3 +47,21 @@ impl<'a> Digest<'a, Bytewise<u16>> {
finalize(self.crc.algorithm, self.value)
}
}

impl<'a> Hasher for Digest<'a, Bytewise<u16>> {
fn finish(&self) -> u64 {
self.clone().finalize() as u64
}

fn write(&mut self, bytes: &[u8]) {
self.update(bytes);
}
}

impl<'a> BuildHasher for &'a Crc<Bytewise<u16>> {
type Hasher = Digest<'a, Bytewise<u16>>;

fn build_hasher(&self) -> Self::Hasher {
self.digest()
}
}
19 changes: 19 additions & 0 deletions src/crc16/default.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::crc16::{finalize, init};
use crate::{Algorithm, Crc, Digest, Implementation};
use core::hash::{BuildHasher, Hasher};

#[cfg(feature = "no-table-mem-limit")]
impl Implementation for u16 {
Expand Down Expand Up @@ -124,3 +125,21 @@ impl<'a> Digest<'a, u16> {
finalize(self.crc.algorithm, self.value)
}
}

impl<'a> Hasher for Digest<'a, u16> {
fn finish(&self) -> u64 {
self.clone().finalize() as u64
}

fn write(&mut self, bytes: &[u8]) {
self.update(bytes);
}
}

impl<'a> BuildHasher for &'a Crc<u16> {
type Hasher = Digest<'a, u16>;

fn build_hasher(&self) -> Self::Hasher {
self.digest()
}
}
19 changes: 19 additions & 0 deletions src/crc16/nolookup.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::crc16::{finalize, init, update_nolookup};
use crate::{Algorithm, Crc, Digest, NoTable};
use core::hash::{BuildHasher, Hasher};

impl Crc<NoTable<u16>> {
pub const fn new(algorithm: &'static Algorithm<u16>) -> Self {
Expand Down Expand Up @@ -47,3 +48,21 @@ impl<'a> Digest<'a, NoTable<u16>> {
finalize(self.crc.algorithm, self.value)
}
}

impl<'a> Hasher for Digest<'a, NoTable<u16>> {
fn finish(&self) -> u64 {
self.clone().finalize() as u64
}

fn write(&mut self, bytes: &[u8]) {
self.update(bytes);
}
}

impl<'a> BuildHasher for &'a Crc<NoTable<u16>> {
type Hasher = Digest<'a, NoTable<u16>>;

fn build_hasher(&self) -> Self::Hasher {
self.digest()
}
}
19 changes: 19 additions & 0 deletions src/crc16/slice16.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::crc16::{finalize, init, update_slice16};
use crate::table::crc16_table_slice_16;
use crate::{Algorithm, Crc, Digest, Slice16};
use core::hash::{BuildHasher, Hasher};

impl Crc<Slice16<u16>> {
pub const fn new(algorithm: &'static Algorithm<u16>) -> Self {
Expand Down Expand Up @@ -46,3 +47,21 @@ impl<'a> Digest<'a, Slice16<u16>> {
finalize(self.crc.algorithm, self.value)
}
}

impl<'a> Hasher for Digest<'a, Slice16<u16>> {
fn finish(&self) -> u64 {
self.clone().finalize() as u64
}

fn write(&mut self, bytes: &[u8]) {
self.update(bytes);
}
}

impl<'a> BuildHasher for &'a Crc<Slice16<u16>> {
type Hasher = Digest<'a, Slice16<u16>>;

fn build_hasher(&self) -> Self::Hasher {
self.digest()
}
}
19 changes: 19 additions & 0 deletions src/crc32/bytewise.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::table::crc32_table;
use crate::{Algorithm, Bytewise, Crc, Digest};
use core::hash::{BuildHasher, Hasher};

use crate::crc32::{finalize, init, update_bytewise};

Expand Down Expand Up @@ -47,3 +48,21 @@ impl<'a> Digest<'a, Bytewise<u32>> {
finalize(self.crc.algorithm, self.value)
}
}

impl<'a> Hasher for Digest<'a, Bytewise<u32>> {
fn finish(&self) -> u64 {
self.clone().finalize() as u64
}

fn write(&mut self, bytes: &[u8]) {
self.update(bytes);
}
}

impl<'a> BuildHasher for &'a Crc<Bytewise<u32>> {
type Hasher = Digest<'a, Bytewise<u32>>;

fn build_hasher(&self) -> Self::Hasher {
self.digest()
}
}
19 changes: 19 additions & 0 deletions src/crc32/default.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::crc32::{finalize, init};
use crate::{Algorithm, Crc, Digest, Implementation};
use core::hash::{BuildHasher, Hasher};

#[cfg(feature = "no-table-mem-limit")]
impl Implementation for u32 {
Expand Down Expand Up @@ -124,3 +125,21 @@ impl<'a> Digest<'a, u32> {
finalize(self.crc.algorithm, self.value)
}
}

impl<'a> Hasher for Digest<'a, u32> {
fn finish(&self) -> u64 {
self.clone().finalize() as u64
}

fn write(&mut self, bytes: &[u8]) {
self.update(bytes);
}
}

impl<'a> BuildHasher for &'a Crc<u32> {
type Hasher = Digest<'a, u32>;

fn build_hasher(&self) -> Self::Hasher {
self.digest()
}
}
19 changes: 19 additions & 0 deletions src/crc32/nolookup.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{Algorithm, Crc, Digest, NoTable};
use core::hash::{BuildHasher, Hasher};

use super::{finalize, init, update_nolookup};

Expand Down Expand Up @@ -48,3 +49,21 @@ impl<'a> Digest<'a, NoTable<u32>> {
finalize(self.crc.algorithm, self.value)
}
}

impl<'a> Hasher for Digest<'a, NoTable<u32>> {
fn finish(&self) -> u64 {
self.clone().finalize() as u64
}

fn write(&mut self, bytes: &[u8]) {
self.update(bytes);
}
}

impl<'a> BuildHasher for &'a Crc<NoTable<u32>> {
type Hasher = Digest<'a, NoTable<u32>>;

fn build_hasher(&self) -> Self::Hasher {
self.digest()
}
}
19 changes: 19 additions & 0 deletions src/crc32/slice16.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::table::crc32_table_slice_16;
use crate::{Algorithm, Crc, Digest, Slice16};
use core::hash::{BuildHasher, Hasher};

use super::{finalize, init, update_slice16};

Expand Down Expand Up @@ -47,3 +48,21 @@ impl<'a> Digest<'a, Slice16<u32>> {
finalize(self.crc.algorithm, self.value)
}
}

impl<'a> Hasher for Digest<'a, Slice16<u32>> {
fn finish(&self) -> u64 {
self.clone().finalize() as u64
}

fn write(&mut self, bytes: &[u8]) {
self.update(bytes);
}
}

impl<'a> BuildHasher for &'a Crc<Slice16<u32>> {
type Hasher = Digest<'a, Slice16<u32>>;

fn build_hasher(&self) -> Self::Hasher {
self.digest()
}
}
19 changes: 19 additions & 0 deletions src/crc64/bytewise.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::table::crc64_table;
use crate::{Algorithm, Bytewise, Crc, Digest};
use core::hash::{BuildHasher, Hasher};

use super::{finalize, init, update_bytewise};

Expand Down Expand Up @@ -47,3 +48,21 @@ impl<'a> Digest<'a, Bytewise<u64>> {
finalize(self.crc.algorithm, self.value)
}
}

impl<'a> Hasher for Digest<'a, Bytewise<u64>> {
fn finish(&self) -> u64 {
self.clone().finalize() as u64
}

fn write(&mut self, bytes: &[u8]) {
self.update(bytes);
}
}

impl<'a> BuildHasher for &'a Crc<Bytewise<u64>> {
type Hasher = Digest<'a, Bytewise<u64>>;

fn build_hasher(&self) -> Self::Hasher {
self.digest()
}
}
Loading

0 comments on commit 734bcdd

Please sign in to comment.