Crates.io | const-serialize |
lib.rs | const-serialize |
version | |
source | src |
created_at | 2024-12-03 01:29:28.875392 |
updated_at | 2024-12-07 03:47:55.656033 |
description | A serialization framework that works in const contexts |
homepage | https://dioxuslabs.com/learn/0.5/getting_started |
repository | https://github.com/dioxuslabs/dioxus |
max_upload_size | |
id | 1469470 |
Cargo.toml error: | TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
A rust serialization library that works in const with complex(ish) types like enums, nested structs and arrays. Const rust does not have an allocator, so this library cannot work in a cross architecture environment with Vecs, slices or strings.
use const_serialize::{deserialize_const, serialize_const, serialize_eq, ConstVec, SerializeConst};
#[derive(Clone, Copy, Debug, PartialEq, SerializeConst)]
struct Struct {
a: u32,
b: u8,
c: u32,
d: Enum,
}
#[derive(Clone, Copy, Debug, PartialEq, SerializeConst)]
#[repr(C, u8)]
enum Enum {
A { one: u32, two: u16 },
B { one: u8, two: u16 } = 15,
}
const {
let data = [Struct {
a: 0x11111111,
b: 0x22,
c: 0x33333333,
d: Enum::A {
one: 0x44444444,
two: 0x5555,
},
}; 3];
let mut buf = ConstVec::new();
buf = serialize_const(&data, buf);
let buf = buf.read();
let (buf, deserialized) = match deserialize_const!([Struct; 3], buf) {
Some(data) => data,
None => panic!("data mismatch"),
};
if !serialize_eq(&data, &deserialized) {
panic!("data mismatch");
}
}
const-serialize
relies heavily on well defined layouts for the types you want to serialize. The serialization format is the linear sequence of unaligned bytes stored in the order of the fields, items or variants of the type. Numbers are stored in little endian order.
In order to support complex nested types, serialization is done using a trait. Since functions in traits cannot be const, const-serialize
uses a macro to generate constant associated items that describe the memory layout of the type. That layout is then used to read all of the bytes in the type into the serialized buffer.
The deserialization is done in a similar way, but the layout is used to write the bytes from the serialized buffer into the type.
The rust nomicon defines the memory layout of different types. It is used as a reference for the layout of the types implemented in const-serialize
.
Vec<T>
. These types are difficult to create in const contexts in generalrepr(Rust)
enums don't have a well defined layout, so they are not supported. repr(C, u8)
enums can be used instead