slice-codec 0.4.0

Library for encoding and decoding Slice types.
Documentation
  • Coverage
  • 74.29%
    52 out of 70 items documented0 out of 29 items with examples
  • Size
  • Source code size: 111.74 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.45 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 2s Average build duration of successful builds.
  • all releases: 2s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • InsertCreativityHere

The Slice codec (slice-codec)

CI crates.io docs.rs License

slice-codec is a lightweight library for encoding and decoding values using the Slice encoding.

Overview

The codec is built around two pairs of types:

  • Encoder writes values into an OutputTarget, driven by the EncodeInto trait.
  • Decoder reads values out of an InputSource, driven by the DecodeFrom trait.

Implementations of EncodeInto / DecodeFrom are provided for common primitives and collections, but you can implement these traits on your own types to make them usable with Slice.

Example

use slice_codec::encoder::Encoder;
use slice_codec::decoder::Decoder;

fn main() -> slice_codec::Result<()> {
    let mut buffer: Vec<u8> = Vec::new();

    // Encode some values into a byte buffer.
    let mut encoder = Encoder::from(&mut buffer);
    encoder.encode("hello")?;
    encoder.encode::<i32>(42)?;

    // Decode them back out.
    let mut decoder = Decoder::from(&buffer);
    let greeting: String = decoder.decode()?;
    let the_answer = decoder.decode::<i32>()?;

    assert_eq!(greeting, "hello");
    assert_eq!(the_answer, 42);
    Ok(())
}

Using slice-codec in a Cargo Project:

Add slice-codec as a dependency to your project's Cargo.toml file:

[dependencies]

slice-codec = "0.4"

Usage in no_std Projects

The codec is no_std compatible, so it can be used in embedded or other constrained environments.
To use the codec in a no_std project, just disable default features:

[dependencies]

slice-codec = { version = "0.4", default-features = false }

If you have a global allocator and need to decode owned types like String or Vec, also enable the alloc feature.

Feature Flags

Feature Default Description
std Enables implementations to encode/decode standard-library types such as String, Vec, and HashMap. Implies alloc.
alloc Enables implementations to decode dynamically-allocated types like String and Vec. Intended for no_std + global allocator.

Building

This crate is part of the slicec workspace. Commands should be run from the repository root:

# Build with the default features.
cargo build -p slice-codec

# Run the tests with all features enabled.
cargo test -p slice-codec --all-features

License

'slicec-codec' is licensed under the Apache License, Version 2.0.