Skip to content

Implement DigestOps trait for Digest across CRC types. #125

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
15 changes: 13 additions & 2 deletions src/crc128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,22 @@ where
Digest { crc, value }
}

pub fn update(&mut self, bytes: &[u8]) {
pub const fn finalize(self) -> u128 {
finalize(self.crc.algorithm, self.value)
}
}

impl<'a, const L: usize> DigestOps for Digest<'a, u128, Table<L>>
where
Table<L>: private::Sealed,
{
type Width = u128;

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

pub const fn finalize(self) -> u128 {
fn finalize(self) -> Self::Width {
finalize(self.crc.algorithm, self.value)
}
}
Expand Down
15 changes: 13 additions & 2 deletions src/crc16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,22 @@ where
Digest { crc, value }
}

pub fn update(&mut self, bytes: &[u8]) {
pub const fn finalize(self) -> u16 {
finalize(self.crc.algorithm, self.value)
}
}

impl<'a, const L: usize> DigestOps for Digest<'a, u16, Table<L>>
where
Table<L>: private::Sealed,
{
type Width = u16;

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

pub const fn finalize(self) -> u16 {
fn finalize(self) -> Self::Width {
finalize(self.crc.algorithm, self.value)
}
}
Expand Down
15 changes: 13 additions & 2 deletions src/crc32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,22 @@ where
Digest { crc, value }
}

pub fn update(&mut self, bytes: &[u8]) {
pub const fn finalize(self) -> u32 {
finalize(self.crc.algorithm, self.value)
}
}

impl<'a, const L: usize> DigestOps for Digest<'a, u32, Table<L>>
where
Table<L>: private::Sealed,
{
type Width = u32;

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

pub const fn finalize(self) -> u32 {
fn finalize(self) -> Self::Width {
finalize(self.crc.algorithm, self.value)
}
}
Expand Down
15 changes: 13 additions & 2 deletions src/crc64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,22 @@ where
Digest { crc, value }
}

pub fn update(&mut self, bytes: &[u8]) {
pub const fn finalize(self) -> u64 {
finalize(self.crc.algorithm, self.value)
}
}

impl<'a, const L: usize> DigestOps for Digest<'a, u64, Table<L>>
where
Table<L>: private::Sealed,
{
type Width = u64;

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

pub const fn finalize(self) -> u64 {
fn finalize(self) -> Self::Width {
finalize(self.crc.algorithm, self.value)
}
}
Expand Down
15 changes: 13 additions & 2 deletions src/crc8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,22 @@ where
Digest { crc, value }
}

pub fn update(&mut self, bytes: &[u8]) {
pub const fn finalize(self) -> u8 {
finalize(self.crc.algorithm, self.value)
}
}

impl<'a, const L: usize> DigestOps for Digest<'a, u8, Table<L>>
where
Table<L>: private::Sealed,
{
type Width = u8;

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

pub const fn finalize(self) -> u8 {
fn finalize(self) -> Self::Width {
finalize(self.crc.algorithm, self.value)
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
//!
//! Using a custom algorithm:
//! ```rust
//! use crc::DigestOps;
//!
//! const CUSTOM_ALG: crc::Algorithm<u16> = crc::Algorithm {
//! width: 16,
//! poly: 0x8005,
Expand Down Expand Up @@ -79,6 +81,13 @@ pub struct Digest<'a, W: Width, I: Implementation = DefaultImpl> {
value: W,
}

pub trait DigestOps {
type Width: Width;

fn update(&mut self, bytes: &[u8]);
fn finalize(self) -> Self::Width;
}

#[cfg(test)]
mod test {
use super::{Crc, CRC_32_ISCSI};
Expand Down