5 个不稳定版本
| 0.3.1 | 2023年3月12日 |
|---|---|
| 0.3.0 | 2023年2月11日 |
| 0.2.1 | 2022年6月21日 |
| 0.2.0 | 2022年6月16日 |
| 0.1.0 | 2022年6月15日 |
#1194 在 Rust 模式
20KB
413 行
rawcode
欢迎使用 rawcode 🎉
rawcode 是一个与 no-std 兼容的简单原始编码。其理念类似于 bincode,但格式更原始:没有变长编码,没有引用 - 只是几种固定长度的类型:字节、布尔值、整数、(嵌套)数组/列表和 StrArray。
类型
内置支持
u8:字节按原样编码(即 8 位,网络位序)bool:布尔值编码为u8,其中true => 0xFF和false => 0x00i8、u16、i16、u32、i32、u64、i64、u128、i128:整数以二进制补码形式在 小端 表示中编码,并始终使用其完整宽度(即u16= 2 字节,i128= 16 字节)struct和array:字段按声明顺序连接并编码,之间没有任何填充StrArray<LEN>:这是对[u8; LEN]的特殊包装,确保其内容始终有效 UTF-8
然而请注意,您也可以轻松实现基本特征 RawcodeConstSize + RawcodeEncode + RawcodeDecode,以提供您自己的类型/包装器的编码和推导。
示例
use rawcode::{Rawcode, RawcodeConstSize, RawcodeDecode, RawcodeEncode, StrArray};
/// A named test struct
#[derive(Debug, PartialEq, Eq, Rawcode)]
struct Named {
boolean: bool,
i128_: i128,
list: [u64; 7],
strarray: StrArray<9>,
}
/// An unnamed test struct
#[derive(Debug, PartialEq, Eq, Rawcode)]
struct Unnamed(bool, i128, [u64; 7], StrArray<9>);
// Create test struct and target buffer
let raw = Named {
boolean: true,
i128_: -170141183460469231731687303715884105728,
list: [0, 1, 2, 3, 4, 5, 6],
strarray: StrArray::new(b"Testolope"),
};
// Encode the named struct
let mut buf = [0; Named::SIZE];
raw.encode(&mut buf).expect("Failed to encode struct");
let decoded = Unnamed::decode(&buf).expect("Failed to decode struct");
// Validate the decoding
assert_eq!(raw.boolean, decoded.0);
assert_eq!(raw.i128_, decoded.1);
assert_eq!(raw.list, decoded.2);
assert_eq!(raw.strarray, decoded.3);
依赖项
~215KB