Skip to content

Commit

Permalink
Use const fn (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxpv committed Oct 10, 2020
1 parent 5fbba1b commit 6253953
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 42 deletions.
32 changes: 18 additions & 14 deletions src/crc16.rs
Expand Up @@ -8,50 +8,54 @@ impl Crc<u16> {
Self { algorithm, table }
}

pub fn checksum(&self, bytes: &[u8]) -> u16 {
pub const fn checksum(&self, bytes: &[u8]) -> u16 {
let mut crc = self.init();
crc = self.update(crc, bytes);
self.finalize(crc)
}

fn init(&self) -> u16 {
const fn init(&self) -> u16 {
if self.algorithm.refin {
reflect_16(self.algorithm.init)
} else {
self.algorithm.init
}
}

fn table_entry(&self, index: u16) -> u16 {
const fn table_entry(&self, index: u16) -> u16 {
self.table[(index & 0xFF) as usize]
}

fn update(&self, crc: u16, bytes: &[u8]) -> u16 {
const fn update(&self, mut crc: u16, bytes: &[u8]) -> u16 {
let mut i = 0;
if self.algorithm.refin {
bytes.iter().fold(crc, |crc, &byte| {
self.table_entry(crc ^ byte as u16) ^ (crc >> 8)
})
while i < bytes.len() {
crc = self.table_entry(crc ^ bytes[i] as u16) ^ (crc >> 8);
i += 1;
}
} else {
bytes.iter().fold(crc, |crc, &byte| {
self.table_entry((byte as u16) ^ (crc >> 8)) ^ (crc << 8)
})
while i < bytes.len() {
crc = self.table_entry(bytes[i] as u16 ^ (crc >> 8)) ^ (crc << 8);
i += 1;
}
}
crc
}

fn finalize(&self, mut crc: u16) -> u16 {
const fn finalize(&self, mut crc: u16) -> u16 {
if self.algorithm.refin ^ self.algorithm.refout {
crc = reflect_16(crc);
}
crc ^ self.algorithm.xorout
}

pub fn digest(&self) -> Digest<u16> {
pub const fn digest(&self) -> Digest<u16> {
Digest::new(self)
}
}

impl<'a> Digest<'a, u16> {
fn new(crc: &'a Crc<u16>) -> Self {
const fn new(crc: &'a Crc<u16>) -> Self {
let value = crc.init();
Digest { crc, value }
}
Expand All @@ -60,7 +64,7 @@ impl<'a> Digest<'a, u16> {
self.value = self.crc.update(self.value, bytes);
}

pub fn finalize(self) -> u16 {
pub const fn finalize(self) -> u16 {
self.crc.finalize(self.value)
}
}
32 changes: 18 additions & 14 deletions src/crc32.rs
Expand Up @@ -8,50 +8,54 @@ impl Crc<u32> {
Self { algorithm, table }
}

pub fn checksum(&self, bytes: &[u8]) -> u32 {
pub const fn checksum(&self, bytes: &[u8]) -> u32 {
let mut crc = self.init();
crc = self.update(crc, bytes);
self.finalize(crc)
}

fn init(&self) -> u32 {
const fn init(&self) -> u32 {
if self.algorithm.refin {
reflect_32(self.algorithm.init)
} else {
self.algorithm.init
}
}

fn table_entry(&self, index: u32) -> u32 {
const fn table_entry(&self, index: u32) -> u32 {
self.table[(index & 0xFF) as usize]
}

fn update(&self, crc: u32, bytes: &[u8]) -> u32 {
const fn update(&self, mut crc: u32, bytes: &[u8]) -> u32 {
let mut i = 0;
if self.algorithm.refin {
bytes.iter().fold(crc, |crc, &byte| {
self.table_entry(crc ^ byte as u32) ^ (crc >> 8)
})
while i < bytes.len() {
crc = self.table_entry(crc ^ bytes[i] as u32) ^ (crc >> 8);
i += 1;
}
} else {
bytes.iter().fold(crc, |crc, &byte| {
self.table_entry((byte as u32) ^ (crc >> 24)) ^ (crc << 8)
})
while i < bytes.len() {
crc = self.table_entry(bytes[i] as u32 ^ (crc >> 24)) ^ (crc << 8);
i += 1;
}
}
crc
}

fn finalize(&self, mut crc: u32) -> u32 {
const fn finalize(&self, mut crc: u32) -> u32 {
if self.algorithm.refin ^ self.algorithm.refout {
crc = reflect_32(crc);
}
crc ^ self.algorithm.xorout
}

pub fn digest(&self) -> Digest<u32> {
pub const fn digest(&self) -> Digest<u32> {
Digest::new(self)
}
}

impl<'a> Digest<'a, u32> {
fn new(crc: &'a Crc<u32>) -> Self {
const fn new(crc: &'a Crc<u32>) -> Self {
let value = crc.init();
Digest { crc, value }
}
Expand All @@ -60,7 +64,7 @@ impl<'a> Digest<'a, u32> {
self.value = self.crc.update(self.value, bytes);
}

pub fn finalize(self) -> u32 {
pub const fn finalize(self) -> u32 {
self.crc.finalize(self.value)
}
}
32 changes: 18 additions & 14 deletions src/crc64.rs
Expand Up @@ -8,50 +8,54 @@ impl Crc<u64> {
Self { algorithm, table }
}

pub fn checksum(&self, bytes: &[u8]) -> u64 {
pub const fn checksum(&self, bytes: &[u8]) -> u64 {
let mut crc = self.init();
crc = self.update(crc, bytes);
self.finalize(crc)
}

fn init(&self) -> u64 {
const fn init(&self) -> u64 {
if self.algorithm.refin {
reflect_64(self.algorithm.init)
} else {
self.algorithm.init
}
}

fn table_entry(&self, index: u64) -> u64 {
const fn table_entry(&self, index: u64) -> u64 {
self.table[(index & 0xFF) as usize]
}

fn update(&self, crc: u64, bytes: &[u8]) -> u64 {
const fn update(&self, mut crc: u64, bytes: &[u8]) -> u64 {
let mut i = 0;
if self.algorithm.refin {
bytes.iter().fold(crc, |crc, &byte| {
self.table_entry(crc ^ byte as u64) ^ (crc >> 8)
})
while i < bytes.len() {
crc = self.table_entry(crc ^ bytes[i] as u64) ^ (crc >> 8);
i += 1;
}
} else {
bytes.iter().fold(crc, |crc, &byte| {
self.table_entry((byte as u64) ^ (crc >> 56)) ^ (crc << 8)
})
while i < bytes.len() {
crc = self.table_entry(bytes[i] as u64 ^ (crc >> 56)) ^ (crc << 8);
i += 1;
}
}
crc
}

fn finalize(&self, mut crc: u64) -> u64 {
const fn finalize(&self, mut crc: u64) -> u64 {
if self.algorithm.refin ^ self.algorithm.refout {
crc = reflect_64(crc);
}
crc ^ self.algorithm.xorout
}

pub fn digest(&self) -> Digest<u64> {
pub const fn digest(&self) -> Digest<u64> {
Digest::new(self)
}
}

impl<'a> Digest<'a, u64> {
fn new(crc: &'a Crc<u64>) -> Self {
const fn new(crc: &'a Crc<u64>) -> Self {
let value = crc.init();
Digest { crc, value }
}
Expand All @@ -60,7 +64,7 @@ impl<'a> Digest<'a, u64> {
self.value = self.crc.update(self.value, bytes);
}

pub fn finalize(self) -> u64 {
pub const fn finalize(self) -> u64 {
self.crc.finalize(self.value)
}
}

0 comments on commit 6253953

Please sign in to comment.