Expand description
§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
§Attribute Macros
You can control encoding/decoding behavior using the following attributes:
#[senax(id = N)]— Assigns a custom field or variant ID (u64). 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. ForOption<T>, this meansNone.#[senax(skip_encode)]— This field is not written during encoding. On decode, it is set toDefault::default().#[senax(skip_decode)]— This field is ignored during decoding and always set toDefault::default(). It is still encoded if present.#[senax(skip_default)]— This field is not written during encoding if its value equals the default value. On decode, missing fields are set toDefault::default().#[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.
§Feature Flags
The following optional features enable support for popular crates and types:
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.serde_json— Enables encoding/decoding ofserde_json::Value(JSON values as dynamic type).
§Example
use senax_encoder::{Encoder, Decoder, Encode, Decode};
use bytes::BytesMut;
#[derive(Encode, Decode, PartialEq, Debug)]
struct MyStruct {
id: u32,
name: String,
}
let value = MyStruct { id: 42, name: "hello".to_string() };
let mut buf = BytesMut::new();
value.encode(&mut buf).unwrap();
let decoded = MyStruct::decode(&mut buf.freeze()).unwrap();
assert_eq!(value, decoded);Enums§
- Encoder
Error - Error type for all encoding and decoding operations in this crate.
Constants§
- TAG_
ARRAY_ VEC_ SET_ BASE - < Enum with tuple fields
- TAG_
ARRAY_ VEC_ SET_ LONG - < Short array/vec/set (length in tag)
- TAG_
BINARY - < Long string (length encoded)
- TAG_
CHRONO_ DATETIME - < Map (HashMap, BTreeMap, IndexMap)
- TAG_
CHRONO_ NAIVE_ DATE - < chrono::DateTime
- TAG_
CHRONO_ NAIVE_ TIME - < chrono::NaiveDate
- TAG_
DECIMAL - < chrono::NaiveTime
- TAG_
ENUM - < Tuple struct
- TAG_
ENUM_ NAMED - < C-like enum (unit variants)
- TAG_
ENUM_ UNNAMED - < Enum with named fields
- TAG_F32
- < Negative signed integer (bit-inverted encoding)
- TAG_F64
- < f32
- TAG_
JSON_ ARRAY - TAG_
JSON_ BOOL - TAG_
JSON_ NULL - < uuid::Uuid, ulid::Ulid
- TAG_
JSON_ NUMBER - TAG_
JSON_ OBJECT - TAG_
JSON_ STRING - TAG_MAP
- < Tuple
- TAG_
NEGATIVE - < u128
- TAG_
NONE - Type tags used in the senax binary format.
- TAG_ONE
- < 0 for numbers, false for bool
- TAG_
SOME - < Option::None
- TAG_
STRING_ BASE - < f64
- TAG_
STRING_ LONG - < Short string (length in tag)
- TAG_
STRUCT_ NAMED - < Unit struct
- TAG_
STRUCT_ UNIT - < Vec
or Bytes - TAG_
STRUCT_ UNNAMED - < Named struct
- TAG_
TUPLE - < Long array/vec/set (length encoded)
- TAG_U8
- TAG_
U8_ 2_ BASE - < 1 for numbers, true for bool
- TAG_
U8_ 127 - TAG_U16
- < u8 (full range)
- TAG_U32
- < u16
- TAG_U64
- < u32
- TAG_
U128 - < u64
- TAG_
UUID - < rust_decimal::Decimal
- TAG_
ZERO - < Option::Some
Traits§
- Decoder
- Trait for types that can be decoded from the senax binary format.
- Encoder
- Trait for types that can be encoded into the senax binary format.
Functions§
- read_
field_ id_ optimized - Reads a field ID using optimized encoding.
- read_
u32_ le - Reads a
u32in little-endian format without a tag. - read_
u64_ le - Reads a
u64in little-endian format without a tag. - skip_
value - Skips a value of any type in the senax binary format.
- write_
field_ id_ optimized - Writes a
u64in little-endian format without a tag. - write_
u32_ le - Writes a
u32in little-endian format without a tag. - write_
u64_ le - Writes a
u64in little-endian format without a tag.
Type Aliases§
- Result
- The result type used throughout this crate for encode/decode operations.