Skip to main content

AFastDeserialize

Trait AFastDeserialize 

Source
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 consumed
  • Err(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§

Source

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

Source§

fn from_bytes(data: &[u8]) -> Result<(Self, usize), Error>

从 1 个字节反序列化布尔值。仅接受 0x00(false)和 0x01(true)。

Deserializes a boolean from 1 byte. Only accepts 0x00 (false) and 0x01 (true).

Source§

impl AFastDeserialize for f32

Source§

fn from_bytes(data: &[u8]) -> Result<(Self, usize), Error>

从字节数组中读取 $size 字节并还原为数值。

Reads $size bytes from the byte array and restores the value.

Source§

impl AFastDeserialize for f64

Source§

fn from_bytes(data: &[u8]) -> Result<(Self, usize), Error>

从字节数组中读取 $size 字节并还原为数值。

Reads $size bytes from the byte array and restores the value.

Source§

impl AFastDeserialize for i8

Source§

fn from_bytes(data: &[u8]) -> Result<(Self, usize), Error>

从字节数组中读取 $size 字节并还原为数值。

Reads $size bytes from the byte array and restores the value.

Source§

impl AFastDeserialize for i16

Source§

fn from_bytes(data: &[u8]) -> Result<(Self, usize), Error>

从字节数组中读取 $size 字节并还原为数值。

Reads $size bytes from the byte array and restores the value.

Source§

impl AFastDeserialize for i32

Source§

fn from_bytes(data: &[u8]) -> Result<(Self, usize), Error>

从字节数组中读取 $size 字节并还原为数值。

Reads $size bytes from the byte array and restores the value.

Source§

impl AFastDeserialize for i64

Source§

fn from_bytes(data: &[u8]) -> Result<(Self, usize), Error>

从字节数组中读取 $size 字节并还原为数值。

Reads $size bytes from the byte array and restores the value.

Source§

impl AFastDeserialize for i128

Source§

fn from_bytes(data: &[u8]) -> Result<(Self, usize), Error>

从字节数组中读取 $size 字节并还原为数值。

Reads $size bytes from the byte array and restores the value.

Source§

impl AFastDeserialize for u8

Source§

fn from_bytes(data: &[u8]) -> Result<(Self, usize), Error>

从字节数组中读取 $size 字节并还原为数值。

Reads $size bytes from the byte array and restores the value.

Source§

impl AFastDeserialize for u16

Source§

fn from_bytes(data: &[u8]) -> Result<(Self, usize), Error>

从字节数组中读取 $size 字节并还原为数值。

Reads $size bytes from the byte array and restores the value.

Source§

impl AFastDeserialize for u32

Source§

fn from_bytes(data: &[u8]) -> Result<(Self, usize), Error>

从字节数组中读取 $size 字节并还原为数值。

Reads $size bytes from the byte array and restores the value.

Source§

impl AFastDeserialize for u64

Source§

fn from_bytes(data: &[u8]) -> Result<(Self, usize), Error>

从字节数组中读取 $size 字节并还原为数值。

Reads $size bytes from the byte array and restores the value.

Source§

impl AFastDeserialize for u128

Source§

fn from_bytes(data: &[u8]) -> Result<(Self, usize), Error>

从字节数组中读取 $size 字节并还原为数值。

Reads $size bytes from the byte array and restores the value.

Source§

impl AFastDeserialize for String

Source§

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]

Source§

fn from_bytes(data: &[u8]) -> Result<(Self, usize), Error>

从字节数据中反序列化固定大小数组。

Deserializes a fixed-size array from byte data.

依次反序列化 N 个元素,要求元素类型实现 DefaultCopy

Deserializes N elements sequentially. Requires the element type to implement Default and Copy.

Source§

impl<T: AFastDeserialize> AFastDeserialize for Option<T>

Source§

fn from_bytes(data: &[u8]) -> Result<(Self, usize), Error>

从字节数据中反序列化 Option<T>

Deserializes an Option<T> from byte data.

先读取 1 字节标记:0x00 表示 None0x01 表示 Some 并继续读取值。

First reads a 1-byte tag: 0x00 means None, 0x01 means Some and continues to deserialize the value.

§错误 / Errors

当标记字节不是 0x000x01 时返回错误。

Returns an error when the tag byte is neither 0x00 nor 0x01.

Source§

impl<T: AFastDeserialize> AFastDeserialize for Vec<T>

Source§

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.

Implementors§