feat(crc): add checksum associated function to Crc trait for convenience
This commit is contained in:
parent
84698796d1
commit
e0b79b5a22
2 changed files with 9 additions and 7 deletions
|
@ -44,7 +44,6 @@ pub const CRC32_INIT: u32 = 0xffffffff;
|
||||||
|
|
||||||
/// Implements CRC-32/ISO-HDLC with look up table
|
/// Implements CRC-32/ISO-HDLC with look up table
|
||||||
pub struct Crc32(<Crc32 as Crc>::Checksum);
|
pub struct Crc32(<Crc32 as Crc>::Checksum);
|
||||||
impl Crc32 {}
|
|
||||||
impl Crc for Crc32 {
|
impl Crc for Crc32 {
|
||||||
type Input = [u8];
|
type Input = [u8];
|
||||||
type Checksum = u32;
|
type Checksum = u32;
|
||||||
|
@ -74,10 +73,8 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_check() {
|
fn test_check() {
|
||||||
let mut c = Crc32::new();
|
|
||||||
c.process(&CHECK_DATA);
|
|
||||||
// see https://reveng.sourceforge.io/crc-catalogue/17plus.htm#crc.cat-bits.32 for the check
|
// see https://reveng.sourceforge.io/crc-catalogue/17plus.htm#crc.cat-bits.32 for the check
|
||||||
// value
|
// value
|
||||||
assert_eq!(*c.shift_reg(), 0xCBF43926);
|
assert_eq!(Crc32::checksum(&CHECK_DATA), 0xCBF43926);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
//! ## Acknowledgements
|
//! ## Acknowledgements
|
||||||
//!
|
//!
|
||||||
//! Many thanks to the [crc-catalogue](https://reveng.sourceforge.io/crc-catalogue/) for providing
|
//! Many thanks to the [crc-catalogue](https://reveng.sourceforge.io/crc-catalogue/) for providing
|
||||||
//! some test vectors.
|
//! an overview over the various algorithms with test vectors and examples.
|
||||||
|
|
||||||
#![cfg_attr(not(test), no_std)]
|
#![cfg_attr(not(test), no_std)]
|
||||||
|
|
||||||
|
@ -15,11 +15,16 @@ pub const CHECK_DATA: [u8; 9] = *b"123456789";
|
||||||
|
|
||||||
pub use crc_32::*;
|
pub use crc_32::*;
|
||||||
|
|
||||||
pub trait Crc {
|
pub trait Crc: Sized {
|
||||||
type Input: ?Sized;
|
type Input: ?Sized;
|
||||||
type Checksum: Eq + Default;
|
type Checksum: Eq + Default + Copy;
|
||||||
|
|
||||||
fn new() -> Self;
|
fn new() -> Self;
|
||||||
fn process(&mut self, data: &Self::Input);
|
fn process(&mut self, data: &Self::Input);
|
||||||
fn shift_reg(&mut self) -> &mut Self::Checksum;
|
fn shift_reg(&mut self) -> &mut Self::Checksum;
|
||||||
|
fn checksum(data: &Self::Input) -> Self::Checksum {
|
||||||
|
let mut c = Self::new();
|
||||||
|
c.process(data);
|
||||||
|
*c.shift_reg()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue