Struct adler2::Adler32

source ·
pub struct Adler32 { /* private fields */ }
Expand description

Adler-32 checksum calculator.

An instance of this type is equivalent to an Adler-32 checksum: It can be created in the default state via new (or the provided Default impl), or from a precalculated checksum via from_checksum, and the currently stored checksum can be fetched via checksum.

This type also implements Hasher, which makes it easy to calculate Adler-32 checksums of any type that implements or derives Hash. This also allows using Adler-32 in a HashMap, although that is not recommended (while every checksum is a hash function, they are not necessarily a good one).

§Examples

Basic, piecewise checksum calculation:

use adler2::Adler32;

let mut adler = Adler32::new();

adler.write_slice(&[0, 1, 2]);
adler.write_slice(&[3, 4, 5]);

assert_eq!(adler.checksum(), 0x00290010);

Using Hash to process structures:

use std::hash::Hash;
use adler2::Adler32;

#[derive(Hash)]
struct Data {
    byte: u8,
    word: u16,
    big: u64,
}

let mut adler = Adler32::new();

let data = Data { byte: 0x1F, word: 0xABCD, big: !0 };
data.hash(&mut adler);

// hash value depends on architecture endianness
if cfg!(target_endian = "little") {
    assert_eq!(adler.checksum(), 0x33410990);
}
if cfg!(target_endian = "big") {
    assert_eq!(adler.checksum(), 0x331F0990);
}

Implementations§

source§

impl Adler32

source

pub fn new() -> Self

Creates a new Adler-32 instance with default state.

source

pub const fn from_checksum(sum: u32) -> Self

Creates an Adler32 instance from a precomputed Adler-32 checksum.

This allows resuming checksum calculation without having to keep the Adler32 instance around.

§Example
let parts = [
    "rust",
    "acean",
];
let whole = adler2::adler32_slice(b"rustacean");

let mut sum = Adler32::new();
sum.write_slice(parts[0].as_bytes());
let partial = sum.checksum();

// ...later

let mut sum = Adler32::from_checksum(partial);
sum.write_slice(parts[1].as_bytes());
assert_eq!(sum.checksum(), whole);
source

pub fn checksum(&self) -> u32

Returns the calculated checksum at this point in time.

source

pub fn write_slice(&mut self, bytes: &[u8])

Adds bytes to the checksum calculation.

If efficiency matters, this should be called with Byte slices that contain at least a few thousand Bytes.

Trait Implementations§

source§

impl Clone for Adler32

source§

fn clone(&self) -> Adler32

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Adler32

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Adler32

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl Hasher for Adler32

source§

fn finish(&self) -> u64

Returns the hash value for the values written so far. Read more
source§

fn write(&mut self, bytes: &[u8])

Writes some data into this Hasher. Read more
1.3.0 · source§

fn write_u8(&mut self, i: u8)

Writes a single u8 into this hasher.
1.3.0 · source§

fn write_u16(&mut self, i: u16)

Writes a single u16 into this hasher.
1.3.0 · source§

fn write_u32(&mut self, i: u32)

Writes a single u32 into this hasher.
1.3.0 · source§

fn write_u64(&mut self, i: u64)

Writes a single u64 into this hasher.
1.26.0 · source§

fn write_u128(&mut self, i: u128)

Writes a single u128 into this hasher.
1.3.0 · source§

fn write_usize(&mut self, i: usize)

Writes a single usize into this hasher.
1.3.0 · source§

fn write_i8(&mut self, i: i8)

Writes a single i8 into this hasher.
1.3.0 · source§

fn write_i16(&mut self, i: i16)

Writes a single i16 into this hasher.
1.3.0 · source§

fn write_i32(&mut self, i: i32)

Writes a single i32 into this hasher.
1.3.0 · source§

fn write_i64(&mut self, i: i64)

Writes a single i64 into this hasher.
1.26.0 · source§

fn write_i128(&mut self, i: i128)

Writes a single i128 into this hasher.
1.3.0 · source§

fn write_isize(&mut self, i: isize)

Writes a single isize into this hasher.
source§

fn write_length_prefix(&mut self, len: usize)

🔬This is a nightly-only experimental API. (hasher_prefixfree_extras)
Writes a length prefix into this hasher, as part of being prefix-free. Read more
source§

fn write_str(&mut self, s: &str)

🔬This is a nightly-only experimental API. (hasher_prefixfree_extras)
Writes a single str into this hasher. Read more
source§

impl Copy for Adler32

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Copy,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.