Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
multi-codec
Rust implementation of the multicodec specification. The crate provides self-describing protocol and encoding identifiers.
Overview
Multicodec is a self-describing multiformat. It identifies protocols, encodings, cryptographic algorithms, and other systems. Each codec has a unique numeric identifier (code), a human-readable name, and a canonical string representation.
This crate provides the Codec enum with 570+ variants. The variants represent all standardized multicodec identifiers from the official specification. A build script generates the enum and its conversions from table.csv. The crate keeps table.csv in sync with the official multicodec table.
Table of Contents
- Features
- Install
- Usage
- CLI Example
- Testing
- Updating the Codec Table
- Feature Flags
- Security
- Maintainers
- Contribute
- License
Features
- 570+ codec variants. All standardized multicodec identifiers.
- Type-safe conversions.
TryFromandIntofor all numeric types and strings. - Serde support. JSON and binary serialization. The
serdefeature gates it. no_stdsupport. The crate works inno_stdenvironments withalloc.- Zero unsafe code.
#![deny(unsafe_code)]is set at the crate root. - Thread-safe. All types are
Send + Sync. - Type-safe newtypes.
CodecCodeandCodecNamewrappers. - Varint encoding. The crate encodes codecs as unsigned varints via
multi-trait.
Install
Add this to your Cargo.toml:
[]
= "1.1"
For no_std environments, disable std and serde:
[]
= { = "1.1", = false }
To use serde under no_std, enable only the serde feature:
[]
= { = "1.1", = false, = ["serde"] }
MSRV: Rust 1.85 (Edition 2024).
Usage
Basic Usage
use Codec;
// Create codec from name
let codec = try_from?;
assert_eq!;
// Get codec properties
assert_eq!;
assert_eq!;
assert_eq!;
# Ok::
Encoding and Decoding
The crate encodes codecs as unsigned varints via the multi-trait crate:
use Codec;
use TryDecodeFrom;
let codec = Sha2256;
// Encode to varint bytes
let bytes: = codec.into;
assert_eq!; // Sha2-256 encodes as a single byte
// Decode from bytes (returns remaining slice for streaming)
let = try_decode_from?;
assert_eq!;
assert!;
# Ok::
Working with Codes and Names
use Codec;
// From numeric code
let codec = try_from?;
assert_eq!;
// From string name
let codec = try_from?;
assert_eq!;
// Get code and name
assert_eq!;
assert_eq!;
# Ok::
Signed integer conversions reject negative values:
use ;
match try_from
Serde Integration
With the serde feature on by default, Codec serializes as a string in human-readable formats (JSON, TOML). It serializes as varint bytes in binary formats (CBOR, bincode):
use Codec;
use ;
let info = SignatureInfo ;
// Serialize to JSON (human-readable - codec name string)
let json = to_string?;
assert!;
// Deserialize from JSON
let deserialized: SignatureInfo = from_str?;
assert_eq!;
# Ok::
Error Handling
All conversion errors return Result with a structured Error enum:
use ;
// Handle invalid names
match try_from
// Handle invalid codes
match try_from
Type-Safe Newtypes
For additional type safety, use the newtype wrappers:
use ;
// Type-safe code value
let code = new;
assert_eq!;
assert_eq!;
// Type-safe name
let name = new;
assert_eq!;
assert!;
Sequential Codec Handling
You can encode multiple codecs into one buffer and decode them back in order:
use Codec;
use ;
let codecs = vec!;
// Encode all codecs into one buffer
let mut buffer = Vecnew;
for codec in &codecs
// Decode them back sequentially
let mut remaining = buffer.as_slice;
for expected in &codecs
# Ok::
CLI Example
The crate includes a varuint encode/decode example in examples/uvi.rs:
# Encode a hex number as varuint
# Decode a hex-encoded varuint
Testing
The crate has 160 tests across unit, integration, property-based, security, and doc-test suites:
# Run all tests
# Run specific test suites
# Run benchmarks
# Run the example
Linting and formatting:
Updating the Codec Table
A build script generates the codec enum from table.csv at the crate root. The build script build.rs parses the CSV. It checks that all codes and names are unique. It emits src/table_gen.rs, which the build includes into src/codec.rs.
To update the table:
- Replace
table.csvwith the latest version from the official multicodec repository. You can also apply your local additions. - Run
cargo build. The build script regeneratessrc/table_gen.rs. - Run
cargo testto make sure the new codecs work.
If the CSV has duplicate codes or names, the build fails. The error names the offending row.
Feature Flags
serde(default). Enables serde serialization and deserialization. When on,CodecimplementsSerializeandDeserialize. Strings are used in human-readable formats. Varint bytes are used in binary formats.
Disabling Default Features
[]
= { = "1.1", = false }
Security
#![deny(unsafe_code)]is set at the crate root.- All conversions check input ranges. Negative signed integers are rejected.
- Deserialization rejects varint input longer than 19 bytes. This protects against DoS.
TryFrom<&[u8]>rejects trailing bytes after the codec varint. It returnsError::TrailingData. This prevents silent data loss. UseCodec::try_decode_fromto parse from a stream with trailing data.- All errors return
Result. No path panics on invalid input. - All types are
Send + Syncwith no shared mutable state.
Maintainers
This repo: @dhuseby.
Original author: @gnunicorn.
Contribute
Contributions are welcome. Please check out the issues.
Development Guidelines
- Run
cargo fmtbefore you commit. - Run
cargo clippy -- -D warningsto check for issues. - Add tests for new features.
- Update documentation for API changes.
- Run the full test suite:
cargo test --all-features.
License
MIT OR Apache-2.0 (c) Cryptid Technologies