#[derive(AFastDeserialize)]
{
// Attributes available to this derive:
#[afast]
}
Expand description
为结构体或枚举生成 AFastDeserialize trait 实现。
Generates an AFastDeserialize trait implementation for a struct or enum.
§生成的代码 / Generated Code
§结构体 / Struct
为每个字段依次调用 from_bytes(),并使用偏移量追踪已消耗的字节数,
最后构造结构体实例。
Calls from_bytes() on each field sequentially, using an offset to track
consumed bytes, then constructs the struct instance.
// 以下为生成代码的示意(非实际代码)
// The following is an illustration of generated code (not actual code)
impl AFastDeserialize for MyStruct {
fn from_bytes(data: &[u8]) -> Result<(Self, usize), Error> {
let mut offset: usize = 0;
let (__val, __new_offset) = AFastDeserialize::from_bytes(&data[offset..])?;
let field1 = __val;
offset += __new_offset;
// ... 更多字段 / more fields ...
Ok((MyStruct { field1, ... }, offset))
}
}§枚举 / Enum
先读取 u8 变体索引,根据索引匹配对应变体,再反序列化该变体的字段。
可通过 feature 切换为 u16 或 u32。
First reads the u8 variant index, matches the corresponding variant by index,
then deserializes the variant’s fields.
Switchable to u16 or u32 via features.
// 以下为生成代码的示意(非实际代码)
// The following is an illustration of generated code (not actual code)
impl AFastDeserialize for MyEnum {
fn from_bytes(data: &[u8]) -> Result<(Self, usize), Error> {
let mut offset: usize = 0;
let (__tag, __new_offset) = <u8 as AFastDeserialize>::from_bytes(&data[offset..])?;
offset += __new_offset;
match __tag as usize {
0 => Ok((MyEnum::Variant1, offset)),
1 => {
let (__val, __new_offset) = AFastDeserialize::from_bytes(&data[offset..])?;
offset += __new_offset;
Ok((MyEnum::Variant2(__val), offset))
}
v => Err(format!("Unknown variant tag: {} for MyEnum", v)),
}
}
}§泛型 / Generics
如果目标类型包含泛型参数,生成的 impl 会同时添加 AFastSerialize 和
AFastDeserialize 约束。双重约束确保泛型类型在序列化和反序列化两个方向
上都可用。
If the target type contains generic parameters, the generated impl adds both
AFastSerialize and AFastDeserialize bounds. The dual bounds ensure the generic
type is available in both serialization and deserialization directions.
§Panics
对 union 类型使用此宏会触发编译 panic。
Using this macro on a union type will trigger a compile-time panic.