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 for self-describing protocol and encoding identifiers.
Overview
Multicodec is a self-describing multiformat that provides a way to uniquely identify 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 representing all
standardized multicodec identifiers from the official specification. The enum
and its conversions are generated at build time from table.csv, which is kept
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:
TryFrom/Intofor all numeric types and strings - Serde Support: JSON and binary serialization (feature-gated)
no_stdSupport: Works inno_stdenvironments withalloc- Zero Unsafe Code:
#![deny(unsafe_code)]enforced at compile time - Thread-Safe: All types are
Send + Sync - Type-Safe Newtypes:
CodecCodeandCodecNamewrappers - Varint Encoding: Encodes codecs as unsigned varints via
multi-trait
Install
Add this to your Cargo.toml:
[]
= "1.0"
For no_std environments (disable std and serde):
[]
= { = "1.0", = false }
To use serde under no_std, enable only the serde feature:
[]
= { = "1.0", = 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
Codecs encode 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 (enabled by default), Codec serializes as a string in
human-readable formats (JSON, TOML) and 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
Multiple codecs can be encoded into a single buffer and decoded 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
The codec enum is generated at build time from table.csv at the crate root.
The build script (build.rs) parses the CSV, validates that all codes and
names are unique, and emits the src/table_gen.rs file that is included into
src/codec.rs.
To update the table:
- Replace
table.csvwith the latest version from the official multicodec repository (or apply your local additions). - Run
cargo build— the build script will regeneratesrc/table_gen.rs. - Run
cargo testto verify the new codecs work correctly.
If the CSV contains duplicate codes or names, the build will fail with an error identifying the offending row.
Feature Flags
serde(default): Enables serde serialization/deserialization. When enabled,CodecimplementsSerializeandDeserialize— strings in human-readable formats, varint bytes in binary formats.
Disabling Default Features
[]
= { = "1.0", = false }
Security
#![deny(unsafe_code)]enforced at compile time- All conversions validate input ranges; negative signed integers are rejected
- Deserialization has a 19-byte size limit on varint input (DoS protection)
- All errors return
Resulttypes — no panics on invalid input - All types are
Send + Syncwith no shared mutable state
Maintainers
This repo: @dhuseby.
Original author: @gnunicorn.
Contribute
Contributions welcome! Please check out the issues.
Development Guidelines
- Run
cargo fmtbefore committing - 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 © Cryptid Technologies