#serialization

macro speedy-derive

A fast binary serialization framework, #[derive(Readable, Writable)] support

8 unstable releases (3 breaking)

✓ Uses Rust 2018 edition

0.6.0 Feb 29, 2020
0.5.0 Oct 4, 2019
0.3.6 Jan 30, 2019
0.3.4 Dec 19, 2018
0.1.0 Dec 6, 2017

#198 in Encoding

Download history 158/week @ 2020-01-17 19/week @ 2020-01-24 57/week @ 2020-01-31 67/week @ 2020-02-07 13/week @ 2020-02-14 79/week @ 2020-02-21 97/week @ 2020-02-28 125/week @ 2020-03-06 68/week @ 2020-03-13 82/week @ 2020-03-20 119/week @ 2020-03-27 150/week @ 2020-04-03 186/week @ 2020-04-10 74/week @ 2020-04-17 192/week @ 2020-04-24 101/week @ 2020-05-01

374 downloads per month
Used in 2 crates

MIT/Apache

49KB
1K SLoC

Build Status

A fast binary serialization framework

Documentation

The goal of this crate is to provide fast, simple and easy binary serialization.

Benchmarks

See serde-bench for benchmarks.

Example

use std::borrow::Cow;
use speedy::{Readable, Writable, Endianness};

#[derive(PartialEq, Debug, Readable, Writable)]
enum Enum {
    A,
    B,
    C,
}

#[derive(PartialEq, Debug, Readable, Writable)]
struct Struct< 'a > {
    number: u64,
    string: String,
    vector: Vec< u8 >,
    cow: Cow< 'a, [i64] >,
    float: f32,
    enumeration: Enum
}

fn main() {
    let original = Struct {
        number: 0x12345678ABCDEF00,
        string: "A totally pointless string".to_owned(),
        vector: vec![ 1, 2, 3 ],
        cow: Cow::Borrowed( &[ 4, 5, 6 ] ),
        float: 3.1415,
        enumeration: Enum::C
    };

    let endian = Endianness::LittleEndian;
    let bytes = original.write_to_vec_with_ctx( endian ).unwrap();
    let deserialized: Struct =
        Struct::read_from_buffer_with_ctx( endian, &bytes ).unwrap();

    assert_eq!( original, deserialized );
}

Supported types

Out-of-box the following types are supported:

Type Serialized as
u8 as-is
u16 as-is
u32 as-is
u64 as-is
usize u64
i8 as-is
i16 as-is
i32 as-is
i64 as-is
f32 as-is
f64 as-is
bool u8, either 0 or 1
String {length: u32, bytes: [u8]}
Cow<'a, str> {length: u32, bytes: [u8]}
Vec<T> {length: u32, values: [T]}
Cow<'a, [T]> {length: u32, values: [T]}
Range<T> (T, T)
Option<T> (1_u8, T) or 0_u8
() nothing
(T) as-is
(T, T) as-is
(T, .., T) as-is
enums {tag: u32, variant: T}

These are stable and will not change in the future.

Attributes

#[speedy(length = ...)]

Can be used on a Vec<T> or on a Cow<'a, [T]> to specify the field's length. Can refer to any of the previous fields.

For example:

use speedy::{Readable, Writable};

#[derive(Readable, Writable)]
struct Struct {
    byte_count: u8,
    #[speedy(length = byte_count / 4)]
    data: Vec< u32 >
}

Before serializing you need to make sure that whatever is set as length is equal to the .len() of the field; if it's not then you will get an error when trying to serialize it.

Setting this attribute changes the serialization format as follows:

Type Serialized as
Vec<T> [T]
Cow<'a, [T]> [T]

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~425–740KB
~18K SLoC