Skip to main content

Crate afastdata_macro

Crate afastdata_macro 

Source
Expand description

§afastdata-macro

afastdata 序列化框架的 derive 宏,为结构体和枚举自动生成 AFastSerializeAFastDeserialize trait 的实现。

Derive macros for the afastdata serialization framework, automatically generating implementations of AFastSerialize and [AFastDeserialize`] traits for structs and enums.

§支持的类型 / Supported Types

  • 命名字段结构体 (Named-field struct):逐字段序列化/反序列化 / Field-by-field serialization/deserialization
  • 元组结构体 (Tuple struct):按索引逐字段序列化/反序列化 / Index-based field serialization/deserialization
  • 单元结构体 (Unit struct):生成空实现(不产生任何字节) / Generates an empty implementation (no bytes produced)
  • 枚举 (Enum):写入 u32 变体索引 + 变体字段数据 / Writes a u32 variant index + variant field data

§编码格式 / Encoding Format

§结构体 / Struct

所有字段按声明顺序依次调用 to_bytes() / from_bytes(),无额外前缀。

All fields call to_bytes() / from_bytes() in declaration order, with no additional prefix.

§枚举 / Enum

编码内容 / Content类型 / Type说明 / Description
变体索引 / Variant indexu32 little-endian从 0 开始递增 / Starts from 0, incrementing
变体字段 / Variant fields逐字段编码 / Field-wise encoding仅非 unit 变体 / Only for non-unit variants

§泛型支持 / Generic Support

泛型参数会自动添加 AFastSerialize 和(对于反序列化)AFastDeserialize trait 约束。 如果泛型参数仅用于某些字段,生成的约束可能过于严格,但这保证了实现的正确性。

Generic parameters automatically receive AFastSerialize and (for deserialization) AFastDeserialize trait bounds. If a generic parameter is only used in certain fields, the generated bounds may be overly strict, but this ensures correctness.

§示例 / Example

extern crate afastdata;
use afastdata::{AFastSerialize, AFastDeserialize};

#[derive(AFastSerialize, AFastDeserialize, Debug, PartialEq)]
struct Point {
    x: i32,
    y: i32,
}

#[derive(AFastSerialize, AFastDeserialize, Debug, PartialEq)]
enum Shape {
    Circle(f64),
    Rectangle { width: f64, height: f64 },
    Empty,
}

// 序列化 / Serialize
let point = Point { x: 10, y: 20 };
let bytes = point.to_bytes();

// 反序列化 / Deserialize
let (decoded, _) = Point::from_bytes(&bytes).unwrap();
assert_eq!(point, decoded);

Derive Macros§

AFastDeserialize
为结构体或枚举生成 AFastDeserialize trait 实现。
AFastSerialize
为结构体或枚举生成 AFastSerialize trait 实现。