Skip to main content

Crate reliakit_codec

Crate reliakit_codec 

Source
Expand description

Deterministic canonical binary encoding and decoding.

reliakit-codec provides small traits and strict primitive implementations for one canonical binary representation per supported type. It is intended for simple protocols, fixtures, cache keys, and reliability-oriented library boundaries where handwritten implementations are preferable to schema or derive machinery.

§Example

use reliakit_codec::{decode_from_slice_exact, encode_to_vec, CanonicalDecode, CanonicalEncode};

#[derive(Debug, PartialEq)]
struct Point {
    x: u16,
    y: u16,
}

impl CanonicalEncode for Point {
    fn encode<W: reliakit_codec::EncodeSink + ?Sized>(
        &self,
        writer: &mut W,
    ) -> Result<(), reliakit_codec::CodecError> {
        self.x.encode(writer)?;
        self.y.encode(writer)
    }
}

impl CanonicalDecode for Point {
    fn decode<R: reliakit_codec::DecodeSource + ?Sized>(
        reader: &mut R,
    ) -> Result<Self, reliakit_codec::CodecError> {
        Ok(Self {
            x: u16::decode(reader)?,
            y: u16::decode(reader)?,
        })
    }
}

let encoded = encode_to_vec(&Point { x: 10, y: 20 })?;
assert_eq!(encoded, [10, 0, 20, 0]);
assert_eq!(decode_from_slice_exact::<Point>(&encoded)?, Point { x: 10, y: 20 });

Re-exports§

pub use decode::CanonicalDecode;
pub use decode::DecodeSource;
pub use decode::SliceReader;
pub use encode::CanonicalEncode;
pub use encode::EncodeSink;
pub use error::CodecError;
pub use error::CodecErrorKind;
pub use helpers::decode_from_slice;
pub use helpers::decode_from_slice_exact;
pub use helpers::encode_to_vec;

Modules§

decode
Decoding traits and byte-slice readers. Decoding traits and sources.
encode
Encoding traits and byte sinks. Encoding traits and sinks.
error
Error types. Error types returned by canonical encoding and decoding.
format
Wire format constants and documentation. Canonical binary format rules.
helpers
Convenience helpers. Convenience helpers for common encode and decode operations.
primitives
Optional reliakit-primitives integrations. Optional integrations for reliakit-primitives.