12 releases (5 breaking)
0.6.0 | Jun 5, 2024 |
---|---|
0.5.3 | May 13, 2024 |
0.5.0 | Dec 30, 2023 |
0.4.2 | Dec 27, 2023 |
0.1.0 | Nov 1, 2023 |
#713 in Encoding
26KB
760 lines
Orio
Lightweight and clean alternative to serde, focusing on writing to and from Vec<u8>
s.
#[derive(Io)]
struct Thingy {
#[io(len(u16))]
name: String,
kind: ThingyKind,
#[io(ignore)]
debug: String
}
#[derive(Io)]
enum ThingyKind {
Normal,
Special(#[io(len(u8))] String)
}
let thingy = Thingy {
name: "thing".to_owned(),
kind: ThingyKind::Normal,
debug: "not written".to_owned()
}
let mut bytes = vec![];
thingy.write(&mut bytes);
Under the hood its a generic wrapper over little-endian byteorder with a derive macro for structs and enums to use.
Named after the cookie :)
License
lib.rs
:
Orio is a fairly small library that works on top of little-endian byteorder. It's intended to automate the boilerplate of serializing types like packets and still give the user control of the data's representation.
Io trait
The [Io] trait allows types to be written and read generically to byte vecs. See its documentation for usage and manual implementation.
Derive macro
The derive macro #[derive(Io)]
can be used to trivially implement Io
for your types.
All serialized fields must implement the Io
trait for it to work.
Fields can have the #[io(ignore)]
attribute added which will:
- Ignore the field when writing the full struct/enum.
- Use
Default
for reading as there is nothing to read from. Some types likeString
orVec
have a length field which can be changed. To use these, add the#[io(len(TYPE))]
attribute, where TYPE is an int like u8, u16, etc. This lets you customize how much data you want a field to support, e.g. for a filename 255 bytes might be fine but you'd want u64 for the file contents.
Std types
Currently a few types from std are supported:
- All numbers
String
Box
HashMap
Option
andVec
of types that implementIo
Duration
andSystemTime
. These are both stored as milliseconds. Duration uses#[io(len)]
to change the lengths of times you want to support. Since u16 is a little over a minute, you'l usually want u32 or u64.
Dependencies
~145–290KB