#no-std #serde #serialization

no-std pinecone

A no_std + serde compatible message library for Rust

7 releases

0.2.5 Jan 5, 2022
0.2.4 Dec 13, 2021
0.2.3 Mar 5, 2020
0.2.2 Dec 8, 2019
0.1.0 Nov 24, 2019

#147 in Encoding

Download history 6/week @ 2022-03-06 31/week @ 2022-03-13 22/week @ 2022-03-20 3/week @ 2022-03-27 7/week @ 2022-04-03 4/week @ 2022-04-10 4/week @ 2022-04-17 25/week @ 2022-04-24 31/week @ 2022-05-01 45/week @ 2022-05-08 122/week @ 2022-05-15 34/week @ 2022-05-22 45/week @ 2022-05-29 39/week @ 2022-06-05 3/week @ 2022-06-12 6/week @ 2022-06-19

100 downloads per month

MIT/Apache

54KB
1.5K SLoC

Pinecone - Yet another binary format for Serde

Pinecone is a minimalistic no_std + alloc fork of Postcard.

Features

Pinecone always assumes that deserialization target is correct. It is fully possible to deserialize into an incorrect type. However, this requires less space and is faster to decode.

Usage

Works just like any other normal serde:

use pinecone::{from_bytes, to_slice, to_vec};
use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
struct Example {
    foo: String,
    bar: Option<u32>,
    zot: bool,
}

fn main() {
    let original = Example {
        foo: "Vec test".to_string(),
        bar: Some(0x1337),
        zot: true,
    };

    let bytes: Vec<u8> = to_vec(&original).expect("Serialization failed");
    assert_eq!(from_bytes(&bytes), Ok(original));

    let original = Example {
        foo: "Slice test".to_string(),
        bar: Some(0x1337),
        zot: true,
    };

    let mut buffer = [0; 1024];
    to_slice(&original, &mut buffer).expect("Serialization failed");
    assert_eq!(from_bytes(&buffer), Ok(original));
}

Variable Length Data

Variable length data (such as slices) are prefixed by their length.

Length is encoded as a Varint. This is done for two reasons: to minimize wasted bytes on the wire when sending slices with items less than 127 items (typical for embedded), and to reduce compatibility issues between 32-bit and 64-bit targets due to differing sizes of usize.

Similarly, enum descriminants are encoded as varints, meaning that any enum with less than 127 variants will encode its discriminant as a single byte (rather than a u32).

Varints in pinecone have a maximum value of the usize for that platform. In practice, this means that 64-bit targets should not send messages with slices containing (1 << 32) - 1 items to 32-bit targets, which is uncommon in practice. Enum discriminants already have a fixed maximum value of (1 << 32) - 1 as currently defined in Rust. Varints larger than the current platform's usize will cause the deserialization process to return an Err.

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

~0.4–1MB
~21K SLoC