https://github.com/adinack/tiny-serde
A statically determined serialization and deserialization system for sized types.
https://github.com/adinack/tiny-serde
embedded-rust no-std serialization
Last synced: about 1 year ago
JSON representation
A statically determined serialization and deserialization system for sized types.
- Host: GitHub
- URL: https://github.com/adinack/tiny-serde
- Owner: AdinAck
- Archived: true
- Created: 2023-11-13T00:17:41.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-01-16T03:47:47.000Z (over 2 years ago)
- Last Synced: 2025-02-21T21:38:37.426Z (over 1 year ago)
- Topics: embedded-rust, no-std, serialization
- Language: Rust
- Homepage:
- Size: 20.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# tiny-serde
A statically determined serialization and deserialization system for sized types.
## no_std
This crate is intended for use in `no_std` environments.
# Usage
This crate creates two traits: `Serialize` and `Deserialize`.
They are extremely simple:
```rust
pub trait Serialize: Sized {
fn serialize(self) -> [u8; N];
}
```
```rust
pub trait Deserialize: Sized {
fn deserialize(data: [u8; N]) -> Option;
}
```
As you can see, since these traits are restricted to sized types, the size of the serialized representation (`N`) is known as well.
Two convenience derive macros `Serialize` and `Deserialize` are provided and can be used like so:
```rust
#[derive(Serialize, Deserialize)]
#[repr(u16)]
enum Foo {
A,
B(bool) = 0xde,
C(u8)
}
#[derive(Serialize, Deserialize)]
struct Bar {
a: u8,
foo: Foo,
b: u16
}
```
> Since `Foo` implements `Serialize` and `Deserialize`, `Bar` can as well.
# Design Considerations
## Safety
No `unsafe` blocks are used in this crate. The derive macros cannot generate unsafe code. Worst case their output will just not compile.
## Note
The derive macros are a **zero-cost abstraction**. They employ constant evaluation to generate static code that extracts bytes from inner structures and places it into the result array. No counting pointer, no runtime checks (other than that of `array::copy_from_slice(...)`), direct insertion only.