Skip to main content

Crate neopack_derive

Crate neopack_derive 

Source
Expand description

Derive macros for neopack’s Pack and Unpack traits.

Instead of writing serialization code by hand, add #[derive(Pack, Unpack)] to your struct or enum.

Structs encode as a list of their fields:

#[derive(Pack, Unpack)]
struct Point { x: f64, y: f64 }

Newtypes (single-field tuple structs) encode as their inner value directly:

#[derive(Pack, Unpack)]
struct UserId(u64);

Enums are tagged by variant name. Each variant can be unit, tuple, or struct:

#[derive(Pack, Unpack)]
enum Shape {
    Empty,
    Circle(f64),
    Rect { w: f64, h: f64 },
}

Use #[pack(bytes)] on a Vec<u8> field to encode it as a byte blob rather than a list of individual bytes:

#[derive(Pack, Unpack)]
struct Message {
    topic: String,
    #[pack(bytes)]
    payload: Vec<u8>,
}

Derive Macros§

Pack
Unpack