Skip to content

Commit

Permalink
impl Clone for Digest
Browse files Browse the repository at this point in the history
I needed a way to feed a Digest a stream of bytes while checking the CRC
for possible matches after each byte. Using CRC::crc directly would make
this O(n^2), so I looked to Digest. Because Digest::finalize consumes
self, it must be thrown out after each finalize operation. This is
probably what you want in the general case, but not in my slightly weird
case.

This problem can be solved by allowing Digest to be duplicated, thereby
letting the user "fork" the CRC state as desired. (This also enables
certain niche CRC-search algorithms.)

I went with Clone rather than Copy because making Digest Copy seemed
potentially error-prone; Clone has the advantage of requiring explicit
intent.
  • Loading branch information
cbiffle authored and akhilles committed Jan 19, 2022
1 parent f0e62a3 commit 4aeef11
Showing 1 changed file with 1 addition and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub struct Crc<W: Width> {
table: [W; 256],
}

#[derive(Clone)]
pub struct Digest<'a, W: Width> {
crc: &'a Crc<W>,
value: W,
Expand Down

0 comments on commit 4aeef11

Please sign in to comment.