Watto
Utilities for parsing and serializing Plain Old Data.
Pod
The API is primarily defined on the [Pod] trait, which can be implemented
for #[repr(C)] types. It is then possible to get a reference to that [Pod]
or a slice thereof directly from an underlying buffer.
Similarly, the [Pod] can also be turned into its underlying buffer as well,
for example to write it out into an output buffer.
Features
writer: Exports an additional [Writer] wrapping a [std::io::Write]
which allows explicitly aligning the output buffer by adding padding bytes.
strings: Exports a [StringTable] for serializing and reading deduplicated strings.
End-to-End Example
use mem;
use Write;
use Pod;
/// Our format looks like this:
/// * A header, containing the number of `A`s.
/// * An aligned slice of `A`s (length given by the header)
/// * An aligned slice of `B`s (length implicitly given by end of buffer)
unsafe
;
unsafe
;
unsafe
// Writing into an output buffer:
let mut writer = new;
writer.write_all.unwrap;
writer.align_to.unwrap;
writer.write_all.unwrap;
writer.align_to.unwrap;
writer.write_all.unwrap;
let buffer = writer.into_inner;
// Reading from a buffer:
let buffer = &buffer;
let = ref_from_prefix.unwrap;
let = align_to.unwrap;
let = slice_from_prefix.unwrap;
let = align_to.unwrap;
let bs = slice_from_bytes.unwrap;
assert_eq!;
assert_eq!;
assert_eq!;
Alternatives
Watto is strongly inspired by zerocopy.
Differences between the two include:
zerocopyhas two distinct traits for reading and writing bytes,wattoonly has one for both.- In
zerocopy, reading a value requires wrapping it inLayoutVerified. Inwatto, types implementingPodcan be read directly. wattoincludes aWriterthat allows explicit alignment of output.wattoincludes aStringTablefor (de)serializing strings.zerocopyincludes endianness-aware integer types.
Why Watto?
Qui-Gon Jinn: I have... acquired a pod in a game of chance. The fastest ever built.
Watto: I hope you didn't kill anyone I know for it.
License
Watto is licensed under the Apache-2.0 license.