Skip to main content

Checksum

Trait Checksum 

Source
pub trait Checksum {
    const LEN: usize;

    // Required method
    fn compute(&self, data: &[u8]) -> u64;
}
Expand description

An integrity check over a byte slice that yields Checksum::LEN bytes.

§Example

A one-byte XOR checksum (weak, but a short example):

use wire_codec::checksum::Checksum;

struct Xor8;

impl Checksum for Xor8 {
    const LEN: usize = 1;
    fn compute(&self, data: &[u8]) -> u64 {
        u64::from(data.iter().fold(0u8, |acc, b| acc ^ b))
    }
}

assert_eq!(Xor8.compute(&[0x0F, 0xF0]), 0xFF);

Required Associated Constants§

Source

const LEN: usize

Number of bytes the checksum occupies on the wire. Must be in 1..=8.

Required Methods§

Source

fn compute(&self, data: &[u8]) -> u64

Compute the checksum of data.

Only the low LEN * 8 bits of the result are transmitted.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§