Watto

Build Status codecov

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

```rust use std::mem; use std::io::Write;

use watto::Pod;

/// Our format looks like this: /// * A header, containing the number of As. /// * An aligned slice of As (length given by the header) /// * An aligned slice of Bs (length implicitly given by end of buffer)

[repr(C)]

struct Header { version: u32, num_as: u32, } unsafe impl Pod for Header {}

[repr(C)]

[derive(Debug, PartialEq)]

struct A(u16); unsafe impl Pod for A {}

[repr(C)]

[derive(Debug, PartialEq)]

struct B(u64); unsafe impl Pod for B {}

// Writing into an output buffer: let mut writer = watto::Writer::new(vec![]);

writer.writeall(Header { version: 1, numas: 3 }.asbytes()).unwrap(); writer.alignto(mem::alignof::()).unwrap(); writer.writeall(&[A(1), A(2), A(3)].asbytes()).unwrap(); writer.alignto(mem::alignof::()).unwrap(); writer.writeall(&[B(4), B(5), B(6)].as_bytes()).unwrap();

let buffer = writer.into_inner();

// Reading from a buffer: let buffer = &buffer;

let (header, buffer) = Header::reffromprefix(buffer).unwrap(); let (, buffer) = watto::alignto(buffer, mem::alignof::()).unwrap(); let (r#as, buffer) = A::slicefromprefix(buffer, header.numas as usize).unwrap(); let (, buffer) = watto::alignto(buffer, mem::alignof::()).unwrap(); let bs = B::slicefrom_bytes(buffer).unwrap();

asserteq!(header.numas, 3); asserteq!(r#as, &[A(1), A(2), A(3)]); asserteq!(bs, &[B(4), B(5), B(6)]); ```

Alternatives

Watto is strongly inspired by zerocopy.

Differences between the two include:

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.

-- Star Wars: Episode I - The Phantom Menace

License

Watto is licensed under the Apache-2.0 license.