Expand description
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
Defaultfor reading as there is nothing to read from. Some types likeStringorVechave 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
StringBoxHashMapOptionandVecof types that implementIoDurationandSystemTime. 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.
Traits§
- Io
- A type that can be serialized to and from bytes.
Should only be implemented on types that own their data, i.e.
Stringand not&str. - Len
- Any primitive integer that can be written.
Used for
LenIofunctions. - LenIo
- Like Io but when a length type is needed.
All functions have a type parameter
Lenwhich should be an integer primitive. For example String encodes its content length then its contents. The user can select what type to use for length, which determines max string length. For custom types that implement this the length type should be passed to fields that implement LenIo. If you do not write any length fields that could benefit from this, do not use LenIo and useIoinstead with the derive macro. The derive macro can be used for example with#[io(len = u16)]to set the length type to u16. - Read
Bytes Ext - Extends
Readwith methods for reading numbers. (Forstd::io.) - Write
Bytes Ext - Extends
Writewith methods for writing numbers. (Forstd::io.)
Functions§
- derive_
enum_ variant_ error - Helper for enum Io::read implementations to use.