Oct
Oct is a Rust crate for cheaply serialising (encoding) and deserialising (decoding) data structures to and from binary streams
What separates this crate from others such as Bincode or Postcard is that this crate is extensively optimised for directly translating into binary encodings (whilst the mentioned crates specifically use Serde as a middle layer).
The original goal of this project was specifically to guarantee size constraints for encodings on a per-type basis at compile-time. Therefore, this crate may be more suited for networking or other cases where many allocations are unwanted.
Keep in mind that this project is still work-in-progress.
This crate is compatible with no_std
.
Performance
As Oct is optimised exclusively for a single, binary format, it may outperform other libraries that are more generic in nature.
The oct-benchmarks
binary compares multiple scenarios using Oct and other, similar crates.
According to my runs on an AMD Ryzen 7 3700X with default settings, these benchmarks indicate that Oct usually outperforms the other tested crates – as demonstrated in the following table:
Benchmark | Bincode | Borsh | Oct | Postcard |
---|---|---|---|---|
encode_u8 |
0.977s | 0.871s | 0.754s | 0.916s |
encode_u32 |
0.967s | 0.983s | 0.730s | 2.727s |
encode_u128 |
2.178s | 2.175s | 1.481s | 6.002s |
encode_struct_unit |
0.000s | 0.000s | 0.000s | 0.000s |
encode_struct_unnamed |
1.206s | 1.168s | 0.805s | 2.356s |
encode_struct_named |
3.021s | 1.532s | 0.952s | 3.013s |
encode_enum_unit |
0.245s | 0.294s | 0.000s | 0.294s |
decode_u8 |
0.952s | 0.895s | 0.885s | 0.894s |
decode_non_zero_u8 |
1.215s | 1.250s | 1.229s | 1.232s |
decode_bool |
1.204s | 1.224s | 1.126s | 1.176s |
Total time → | 11.964s | 10.392s | 7.963s | 18.609s |
Total deviation (p.c.) → | +50 | +31 | ±0 | +134 |
All quantities are measured in seconds unless otherwise noted.
Currently, Oct's weakest point seems to be decoding. Please note that I myself find large (relatively speaking) inconsistencies between runs in these last three benchmarks. Do feel free to conduct your own tests of Oct.
Data model
Most primitives encode losslessly, with the main exceptions being usize
and isize
.
These are instead first cast as u16
and i16
, respectively, due to portability concerns (with respect to embedded systems).
Numerical primitives in general encode as little endian (and not "network order"). It is recommended for implementors to follow this convention as well.
See specific types' implementations for notes on their data models.
Note that the data model is currently not stabilised, and may not necessarily be in the near future (at least before specialisation). It may therefore be undesired to store encodings long-term.
Usage & Examples
This crate revolves around the Encode
and Decode
traits, both of which handle conversions to and from byte streams.
Many standard types come implemented with Oct, including most primitives as well as some standard library types such as Option
and Result
.
Some features enable an extended set of implementations.
It is recommended in most cases to simply derive these two traits for user-defined types, although this is only supported for enumerations and structures – not untagged unions. When deriving, each field is chained according to declaration order:
use Decode;
use Encode;
use Slot;
const VALUE: Ints = Ints ;
let mut buf = with_capacity;
buf.write.unwrap;
assert_eq!;
assert_eq!;
assert_eq!;
The following is a more complete example of a UDP server/client for geographic data:
use Decode;
use ;
use Slot;
use io;
use ;
use spawn;
// City, region, etc.:
// Client-to-server message:
// Server-to-client message:
let mut server = new.unwrap;
let mut client = new.unwrap;
spawn;
spawn;
Feature flags
Oct defines the following, default features:
alloc
: Enables theSlot
type and implementations for types inalloc
, e.g.Box
andArc
proc-macro
: Pulls procedural macros from theoct-macros
cratestd
: Enables implementations for typesstd
, e.g.Mutex
andRwLock
Documentation
Oct has its documentation written alongside its source code for use by rustdoc
.
See Docs.rs for an on-line, rendered instance.
Currently, these docs make use of some unstable features for the sake of readability. The nightly toolchain is therefore required when rendering them.
Contribution
Oct does not accept source code contributions at the moment. This is a personal choice by the maintainer and may be undone in the future.
Do however feel free to open an issue on GitLab or GitHub if you feel the need to express any concerns over the project.
Copyright & Licence
Copyright 2024-2025 Gabriel Bjørnager Jensen.
The Source Code Forms of this project are – where noted as such – subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this project, you can obtain one at https://mozilla.org/MPL/2.0/.
Note that the oct-benchmarks
executable is differently released under an MIT licence.