Crate orio

source ·
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:

  1. Ignore the field when writing the full struct/enum.
  2. Use Default for reading as there is nothing to read from. Some types like String or Vec 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 and Vec of types that implement Io
  • Duration and SystemTime. 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§

  • A type that can be serialized to and from bytes. Should only be implemented on types that own their data, i.e. String and not &str.
  • Any primitive integer that can be written. Used for LenIo functions.
  • Like Io but when a length type is needed. All functions have a type parameter Len which 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 use Io instead with the derive macro. The derive macro can be used for example with #[io(len = u16)] to set the length type to u16.
  • Extends Read with methods for reading numbers. (For std::io.)
  • Extends Write with methods for writing numbers. (For std::io.)

Functions§

Type Aliases§

Derive Macros§