senax-encoder-0.1.0 has been yanked.
senax-encoder
A fast, compact, and schema-evolution-friendly binary serialization library for Rust.
- Supports struct/enum encoding with field/variant IDs for forward/backward compatibility
- Efficient encoding for primitives, collections, Option, String, bytes, and popular crates (chrono, uuid, ulid, rust_decimal, indexmap)
- Custom derive macros for ergonomic usage
- Feature-gated support for optional dependencies
Features
- Compact, efficient encoding for a wide range of types (primitives, collections, Option, String, bytes, chrono, uuid, ulid, rust_decimal, indexmap)
- Schema evolution and version compatibility via field/variant IDs and tag-based format
- Attribute macros for fine-grained control (custom IDs, default values, skip encode/decode, renaming, compact ID encoding)
- Feature flags for optional support of popular crates
- Suitable for network protocols, storage, and applications requiring forward/backward compatibility
Attribute Macros
You can control encoding/decoding behavior using the following attributes:
#[senax(id = N)]— Assigns a custom field or variant ID (u32 or u8, see below). Ensures stable wire format across versions.#[senax(default)]— If a field is missing during decoding, its value is set toDefault::default()instead of causing an error.#[senax(skip_encode)]— This field is not written during encoding.#[senax(skip_decode)]— This field is ignored during decoding and always set toDefault::default(). It is still encoded if present.#[senax(rename = "name")]— Use the given string as the logical field/variant name for ID calculation. Useful for renaming fields/variants while keeping the same wire format.#[senax(u8)]— On structs/enums, encodes field/variant IDs asu8instead ofu32(for compactness, up to 255 IDs; 0 is reserved for terminator).
Feature Flags
The following optional features enable support for popular crates and types:
all— Enables all optional features below at once:indexmap,chrono,rust_decimal,uuid,ulid.chrono— Enables encoding/decoding ofchrono::DateTime,NaiveDate, andNaiveTimetypes.uuid— Enables encoding/decoding ofuuid::Uuid.ulid— Enables encoding/decoding ofulid::Ulid(shares the same tag as UUID for binary compatibility).rust_decimal— Enables encoding/decoding ofrust_decimal::Decimal.indexmap— Enables encoding/decoding ofIndexMapandIndexSetcollections.
Example
use ;
use BytesMut;
let value = MyStruct ;
let mut buf = new;
value.encode.unwrap;
let decoded = decode.unwrap;
assert_eq!;
Quick Start
Add to your Cargo.toml:
[]
= "0.1"
Basic usage:
use ;
use ;
let user = User ;
let mut buf = new;
user.encode.unwrap;
let mut bytes = buf.freeze;
let decoded = decode.unwrap;
assert_eq!;
Usage
1. Derive macros for automatic implementation
2. Binary encode/decode
let mut buf = new;
value.encode?;
let mut bytes = buf.freeze;
let value2 = decode?;
3. Schema evolution (adding/removing/changing fields)
- Field IDs are automatically generated from field names (CRC32) by default.
- Use
#[senax(id=...)]only if you need to resolve a collision.
- Use
- Because mapping is by field ID (u32):
- Old struct → new struct:
- New fields of type
OptionbecomeNoneif missing. - New required fields without
defaultwill cause a decode error if missing.
- New fields of type
- New struct → old struct: unknown fields are automatically skipped.
- Old struct → new struct:
- No field names are stored, only u32 IDs, so field addition/removal/reordering/type changes are robust.
4. Feature flags
- Enable only the types you need:
indexmap,chrono,rust_decimal,uuid,ulid, etc. - Minimizes dependencies and build time.
Supported Types
- Primitives:
u8~u128,i8~i128,f32,f64,bool,String,Bytes - Option, Vec, arrays, HashMap, BTreeMap, Set, Tuple, Enum, Struct
- chrono:
DateTime<Utc/Local>,NaiveDate,NaiveTime - rust_decimal:
Decimal - uuid:
Uuid - ulid:
Ulid - indexmap:
IndexMap,IndexSet
Binary Format (Overview)
- Each value is tagged binary (
u8tag + data) - Structs/enums use ID-based fields for robustness to order, presence, and type changes
- See
specification.mdfor full details
Contribution & Bug Reports
Pull requests and issues are welcome!
Related Projects
senax-encoder is a Rust encoder designed for robust, safe binary exchange even across versions. Feedback and contributions are highly appreciated!