Skip to main content

Checksummed

Struct Checksummed 

Source
pub struct Checksummed<F, C> { /* private fields */ }
Expand description

A framer decorator that appends and verifies a checksum trailer.

§Example

use wire_codec::checksum::Crc32;
use wire_codec::framing::{Checksummed, Endian, Framer, LengthPrefixed, LengthWidth};
use wire_codec::{Error, WriteBuf};

let inner = LengthPrefixed::bounded(LengthWidth::U16, Endian::Big);
let framer = Checksummed::new(inner, Crc32, Endian::Little);

let mut out = [0u8; 32];
let mut buf = WriteBuf::new(&mut out);
framer.write_frame(b"ping", &mut buf).unwrap();
let n = buf.position();
assert_eq!(n, 2 + 4 + 4); // prefix + payload + CRC-32

let frame = framer.next_frame(&out[..n]).unwrap().unwrap();
assert_eq!(frame.payload(), b"ping");
assert_eq!(frame.consumed(), n);

out[3] ^= 0x01; // flip one payload bit in transit
assert_eq!(framer.next_frame(&out[..n]), Err(Error::ChecksumMismatch));

Implementations§

Source§

impl<F: Framer, C: Checksum> Checksummed<F, C>

Source

pub const fn new(inner: F, checksum: C, endian: Endian) -> Self

Wrap inner, appending checksum in endian byte order. The checksum covers the whole inner frame (ChecksumScope::Frame).

Source

pub const fn with_scope(self, scope: ChecksumScope) -> Self

Choose which bytes the checksum covers.

Source

pub const fn inner(&self) -> &F

The wrapped framer.

Source

pub const fn checksum(&self) -> &C

The configured checksum.

Source

pub const fn endian(&self) -> Endian

Byte order of the trailer.

Source

pub const fn scope(&self) -> ChecksumScope

Which bytes the checksum covers.

Trait Implementations§

Source§

impl<F: Clone, C: Clone> Clone for Checksummed<F, C>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<F: Copy, C: Copy> Copy for Checksummed<F, C>

Source§

impl<F: Debug, C: Debug> Debug for Checksummed<F, C>

Source§

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

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

impl<F: Eq, C: Eq> Eq for Checksummed<F, C>

Source§

impl<F: Framer, C: Checksum> Framer for Checksummed<F, C>

Source§

fn next_frame<'a>(&self, input: &'a [u8]) -> Result<Option<Frame<'a>>>

Extract an inner frame and verify the trailer that follows it.

§Errors
Source§

fn write_frame(&self, payload: &[u8], out: &mut WriteBuf<'_>) -> Result<()>

Write the inner frame followed by the checksum trailer.

§Errors
  • Error::BufferFull if out cannot hold the frame and trailer. When the inner framer reports Framer::max_frame_len (every built-in framer does, exactly), that bound plus the trailer is checked before anything is written.
  • Any error from the inner framer.

Because the room is checked first, a short buffer is reported as Error::BufferFull even when the inner framer would also reject the payload (with Error::FrameTooLarge or Error::DelimiterInPayload, say); given enough room, the inner framer’s error is returned instead.

The cursor is unchanged on error. With a built-in inner framer nothing is written either. With a custom inner framer that reports no size bound, a trailer that does not fit can leave the inner frame’s bytes behind the (restored) cursor.

Source§

fn next_frame_from<'a>( &self, input: &'a [u8], scanned: &mut usize, ) -> Result<Option<Frame<'a>>>

Resumable variant: forwards the progress hint to the inner framer.

§Errors

Same contract as Framer::next_frame for this type.

Source§

fn max_frame_len(&self, payload_len: usize) -> Option<usize>

Upper bound on the number of bytes Framer::write_frame writes for a payload of payload_len bytes. Read more
Source§

impl<F: Hash, C: Hash> Hash for Checksummed<F, C>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<F: PartialEq, C: PartialEq> PartialEq for Checksummed<F, C>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<F: PartialEq, C: PartialEq> StructuralPartialEq for Checksummed<F, C>

Auto Trait Implementations§

§

impl<F, C> Forget for Checksummed<F, C>

§

impl<F, C> Freeze for Checksummed<F, C>
where F: Freeze, C: Freeze,

§

impl<F, C> Move for Checksummed<F, C>

§

impl<F, C> RefUnwindSafe for Checksummed<F, C>

§

impl<F, C> Send for Checksummed<F, C>
where F: Send, C: Send,

§

impl<F, C> Sync for Checksummed<F, C>
where F: Sync, C: Sync,

§

impl<F, C> Unpin for Checksummed<F, C>
where F: Unpin, C: Unpin,

§

impl<F, C> UnsafeUnpin for Checksummed<F, C>
where F: UnsafeUnpin, C: UnsafeUnpin,

§

impl<F, C> UnwindSafe for Checksummed<F, C>
where F: UnwindSafe, C: UnwindSafe,

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: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

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

Source§

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.