pub trait AFastDeserialize: Sized {
// Required method
fn from_bytes(data: &[u8]) -> Result<(Self, usize), Error>;
}Expand description
反序列化 trait,为类型提供从字节数组还原的能力。
Deserialization trait that provides the ability to restore a type from a byte array.
§返回值说明 / Return Value Notes
from_bytes 返回 Result<(Self, usize), Error>:
Ok((value, bytes_consumed)):成功时返回还原的值和实际消耗的字节数Err(message):失败时返回错误描述
from_bytes returns Result<(Self, usize), Error>:
Ok((value, bytes_consumed)): On success, returns the restored value and the number of bytes actually consumedErr(message): On failure, returns an error description
§示例 / Example
use afastdata::AFastDeserialize;
let bytes: Vec<u8> = vec![42, 0, 0, 0];
let (value, consumed) = i32::from_bytes(&bytes).unwrap();
assert_eq!(value, 42);
assert_eq!(consumed, 4);Required Methods§
Sourcefn from_bytes(data: &[u8]) -> Result<(Self, usize), Error>
fn from_bytes(data: &[u8]) -> Result<(Self, usize), Error>
从字节数组中反序列化一个值。
Deserialize a value from a byte array.
§参数 / Parameters
-
data:待反序列化的字节切片,可以是完整数据的子集(从指定偏移量开始) -
data: The byte slice to deserialize from. May be a subset of the complete data (starting from a specific offset).
§错误 / Errors
当字节数据不足或格式无效时返回 Err。
Returns Err when there are insufficient bytes or the format is invalid.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementations on Foreign Types§
Source§impl AFastDeserialize for bool
impl AFastDeserialize for bool
Source§impl AFastDeserialize for f32
impl AFastDeserialize for f32
Source§impl AFastDeserialize for f64
impl AFastDeserialize for f64
Source§impl AFastDeserialize for i8
impl AFastDeserialize for i8
Source§impl AFastDeserialize for i16
impl AFastDeserialize for i16
Source§impl AFastDeserialize for i32
impl AFastDeserialize for i32
Source§impl AFastDeserialize for i64
impl AFastDeserialize for i64
Source§impl AFastDeserialize for i128
impl AFastDeserialize for i128
Source§impl AFastDeserialize for u8
impl AFastDeserialize for u8
Source§impl AFastDeserialize for u16
impl AFastDeserialize for u16
Source§impl AFastDeserialize for u32
impl AFastDeserialize for u32
Source§impl AFastDeserialize for u64
impl AFastDeserialize for u64
Source§impl AFastDeserialize for u128
impl AFastDeserialize for u128
Source§impl AFastDeserialize for String
impl AFastDeserialize for String
Source§fn from_bytes(data: &[u8]) -> Result<(Self, usize), Error>
fn from_bytes(data: &[u8]) -> Result<(Self, usize), Error>
从字节数据中反序列化字符串。
Deserializes a string from byte data.
先读取 LenInt 长度前缀,再读取对应数量的 UTF-8 字节。
First reads the LenInt length prefix, then reads the corresponding number
of UTF-8 bytes.
§错误 / Errors
当字节数据不足或包含非法 UTF-8 序列时返回错误。
Returns an error when there are insufficient bytes or the data contains invalid UTF-8 sequences.
Source§impl<T: AFastDeserialize + Default + Copy, const N: usize> AFastDeserialize for [T; N]
impl<T: AFastDeserialize + Default + Copy, const N: usize> AFastDeserialize for [T; N]
Source§impl<T: AFastDeserialize> AFastDeserialize for Option<T>
impl<T: AFastDeserialize> AFastDeserialize for Option<T>
Source§fn from_bytes(data: &[u8]) -> Result<(Self, usize), Error>
fn from_bytes(data: &[u8]) -> Result<(Self, usize), Error>
从字节数据中反序列化 Option<T>。
Deserializes an Option<T> from byte data.
先读取 1 字节标记:0x00 表示 None,0x01 表示 Some 并继续读取值。
First reads a 1-byte tag: 0x00 means None, 0x01 means Some and
continues to deserialize the value.
§错误 / Errors
当标记字节不是 0x00 或 0x01 时返回错误。
Returns an error when the tag byte is neither 0x00 nor 0x01.
Source§impl<T: AFastDeserialize> AFastDeserialize for Vec<T>
impl<T: AFastDeserialize> AFastDeserialize for Vec<T>
Source§fn from_bytes(data: &[u8]) -> Result<(Self, usize), Error>
fn from_bytes(data: &[u8]) -> Result<(Self, usize), Error>
从字节数据中反序列化向量。
Deserializes a vector from byte data.
先读取 LenInt 元素个数,再逐个反序列化元素。
First reads the LenInt element count, then deserializes each element in order.
§错误 / Errors
当字节数据不足或任何元素反序列化失败时返回错误。
Returns an error when there are insufficient bytes or any element fails to deserialize.