Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Getting started with desert

Desert is a binary serialization library for Rust (and Scala), focusing on creating small binaries while still enabling binary compatible evolution of the data model.

It is suitable for any kind of short or long term storage.

First add desert as a dependency:

desert_rust = "0.1.0"

The most simple use case is to serialize a known type to an array of bytes and read it back:

extern crate desert_rust;
use desert_rust::serialize_to_byte_vec;

fn main() {
let data_or_failure = serialize_to_byte_vec(&"Hello world".to_string());
}
extern crate desert_rust;
use desert_rust::serialize_to_byte_vec;
use desert_rust::deserialize;

fn main() {
 let data_or_failure = serialize_to_byte_vec(&"Hello world".to_string());
let y = data_or_failure.and_then(|data| deserialize::<String>(&data));
}

Codecs

This works because the BinaryCodec (a combination of BinarySerializer and BinaryDeserializer) trait is implemented for String. Read the codecs page to learn about the available codecs and how to define custom ones.

Low level input/output

The above example shows the convenient functions to work with Vec<u8> and &[u8] directly, but they have a more generic version working on the low level BinaryInput and BinaryOutput interfaces. These are described on the input/output page.

Evolution

One of the primary features of the library is the support for evolving the data model. The possibilities are described on a separate page.

Type registry

For cases when the exact type to be deserialized is not known at compile type, the possibilities can be registered to a type registry.

Input/output

Codecs

A BinaryCodec is a type class that defines both the serializer and the deserializer for a given type:

#![allow(unused)]
fn main() {
extern crate desert_rust;
use desert_rust::{BinarySerializer, BinaryDeserializer};

trait BinaryCodec: BinarySerializer + BinaryDeserializer {}
}

Primitive types

The library defines implementations of BinaryCodec for the following primitive types:

extern crate desert_rust;
use desert_rust::*;
fn main() {
let byte = serialize_to_byte_vec(&100u8).unwrap();    
}
100
extern crate desert_rust;
use desert_rust::*;
fn main() {
let short = serialize_to_byte_vec(&100u16).unwrap();    
}
0 100
extern crate desert_rust;
use desert_rust::*;
fn main() {
let int = serialize_to_byte_vec(&100u32).unwrap();    
}
0 0 0 100
extern crate desert_rust;
use desert_rust::*;
fn main() {
let long = serialize_to_byte_vec(&100u64).unwrap();    
}
0 0 0 0 0 0 0 100
extern crate desert_rust;
use desert_rust::*;
fn main() {
let float = serialize_to_byte_vec(&3.14f32).unwrap();    
}
64 72 245 195

Evolution

Type registry