Skip to main content

AFastSerialize

Trait AFastSerialize 

Source
pub trait AFastSerialize {
    // Required method
    fn to_bytes(&self) -> Vec<u8> ;
}
Expand description

序列化 trait,为类型提供转换为字节数组的能力。

Serialization trait that provides the ability to convert a type into a byte array.

§编码规则 / Encoding Rules

实现者应确保 to_bytes() 输出的字节流能够被对应的 AFastDeserialize::from_bytes 完整还原。字节序统一使用 小端序 (little-endian)

Implementors must ensure that the byte output of to_bytes() can be fully restored by the corresponding AFastDeserialize::from_bytes. All multi-byte values use little-endian byte order.

§示例 / Example

use afastdata::AFastSerialize;

let value: i32 = 42;
let bytes = value.to_bytes();
assert_eq!(bytes, vec![42, 0, 0, 0]);

Required Methods§

Source

fn to_bytes(&self) -> Vec<u8>

将值序列化为字节数组。

Serialize the value into a byte array.

§返回值 / Returns

返回一个 Vec<u8>,包含该值的完整二进制表示。

Returns a Vec<u8> containing the complete binary representation of the value.

Implementations on Foreign Types§

Source§

impl AFastSerialize for &str

Source§

fn to_bytes(&self) -> Vec<u8>

将字符串切片序列化为:LenInt 长度前缀 + UTF-8 字节

Serializes the string slice as: LenInt length prefix + UTF-8 bytes.

String 的序列化格式完全一致,方便在不拥有所有权的情况下进行序列化。

Identical to String serialization format, allowing serialization without taking ownership.

Source§

impl AFastSerialize for bool

Source§

fn to_bytes(&self) -> Vec<u8>

将布尔值序列化为 1 个字节:true0x01false0x00

Serializes the boolean to 1 byte: true as 0x01, false as 0x00.

Source§

impl AFastSerialize for f32

Source§

fn to_bytes(&self) -> Vec<u8>

将数值转为 $size 字节的 little-endian 字节数组。

Converts the value to a $size-byte little-endian byte array.

Source§

impl AFastSerialize for f64

Source§

fn to_bytes(&self) -> Vec<u8>

将数值转为 $size 字节的 little-endian 字节数组。

Converts the value to a $size-byte little-endian byte array.

Source§

impl AFastSerialize for i8

Source§

fn to_bytes(&self) -> Vec<u8>

将数值转为 $size 字节的 little-endian 字节数组。

Converts the value to a $size-byte little-endian byte array.

Source§

impl AFastSerialize for i16

Source§

fn to_bytes(&self) -> Vec<u8>

将数值转为 $size 字节的 little-endian 字节数组。

Converts the value to a $size-byte little-endian byte array.

Source§

impl AFastSerialize for i32

Source§

fn to_bytes(&self) -> Vec<u8>

将数值转为 $size 字节的 little-endian 字节数组。

Converts the value to a $size-byte little-endian byte array.

Source§

impl AFastSerialize for i64

Source§

fn to_bytes(&self) -> Vec<u8>

将数值转为 $size 字节的 little-endian 字节数组。

Converts the value to a $size-byte little-endian byte array.

Source§

impl AFastSerialize for i128

Source§

fn to_bytes(&self) -> Vec<u8>

将数值转为 $size 字节的 little-endian 字节数组。

Converts the value to a $size-byte little-endian byte array.

Source§

impl AFastSerialize for u8

Source§

fn to_bytes(&self) -> Vec<u8>

将数值转为 $size 字节的 little-endian 字节数组。

Converts the value to a $size-byte little-endian byte array.

Source§

impl AFastSerialize for u16

Source§

fn to_bytes(&self) -> Vec<u8>

将数值转为 $size 字节的 little-endian 字节数组。

Converts the value to a $size-byte little-endian byte array.

Source§

impl AFastSerialize for u32

Source§

fn to_bytes(&self) -> Vec<u8>

将数值转为 $size 字节的 little-endian 字节数组。

Converts the value to a $size-byte little-endian byte array.

Source§

impl AFastSerialize for u64

Source§

fn to_bytes(&self) -> Vec<u8>

将数值转为 $size 字节的 little-endian 字节数组。

Converts the value to a $size-byte little-endian byte array.

Source§

impl AFastSerialize for u128

Source§

fn to_bytes(&self) -> Vec<u8>

将数值转为 $size 字节的 little-endian 字节数组。

Converts the value to a $size-byte little-endian byte array.

Source§

impl AFastSerialize for String

Source§

fn to_bytes(&self) -> Vec<u8>

将字符串序列化为:LenInt 长度前缀 + UTF-8 字节

Serializes the string as: LenInt length prefix + UTF-8 bytes.

长度前缀表示 UTF-8 字节的长度(不是字符数)。

The length prefix represents the number of UTF-8 bytes (not the character count).

Source§

impl<T: AFastSerialize> AFastSerialize for Option<T>

Source§

fn to_bytes(&self) -> Vec<u8>

Option<T> 序列化。

Serializes an Option<T>.

  • None:写入 0x00(1 字节)

  • Some(value):写入 0x01(1 字节标记)+ 值的序列化数据

  • None: Writes 0x00 (1 byte)

  • Some(value): Writes 0x01 (1 byte tag) + the serialized value data

Source§

impl<T: AFastSerialize> AFastSerialize for Vec<T>

Source§

fn to_bytes(&self) -> Vec<u8>

将向量序列化为:LenInt 元素个数前缀 + 逐个元素的序列化数据

Serializes the vector as: LenInt element count prefix + serialized data for each element.

每个元素调用其自身的 to_bytes() 方法,所有元素的序列化结果依次拼接。

Each element’s to_bytes() method is called, and all serialized results are concatenated sequentially.

Source§

impl<T: AFastSerialize, const N: usize> AFastSerialize for [T; N]

Source§

fn to_bytes(&self) -> Vec<u8>

将固定大小数组序列化为:逐个元素的序列化数据,无长度前缀。

Serializes a fixed-size array as: element-wise serialized data with no length prefix.

因为数组大小在编译时已知,所以不需要长度前缀。

Since the array size is known at compile time, no length prefix is needed.

Implementors§