#derive #networking #send #read #write #macro #marshall

bytebuff

Crate provides usefull derive macro for serializing data to send ower network

2 releases

0.1.1 Oct 15, 2020
0.1.0 Oct 12, 2020

#1824 in Rust patterns

MIT license

13KB
238 lines

This crate is purely for marshalling and unmarshalling a data to send over a network, main deal is ByteBuff witch is used for serilizing and deserilizing. There is also Marshal trait that can be derivated on structs.

example

extern crate bytebuff;
use bytebuff::marshall_derive::Marshall;
use bytebuff::Marshall;
use bytebuff::ByteBuff;

#[derive(Marshall)]
struct Point {
    x: f32,
    y: f32,
}

#[derive(Marshall)]
struct Dummy {
    #[marshall(nested)]
    position: Point,
    name: String,
    #[marshall(ignore)]
    useless_stuff: i128,
}

fn main() {
    let mut buff = ByteBuff::new();

    let dummy = Dummy {
        useless_stuff: 10,
        position: Point { x: 10.0, y: 70.0 },
        name: String::from("Mlokogrgel"),
    };

    //dummy can write it self to ByteBuff
    dummy.marshall(&mut buff);

    //now imagine we sent buffer with dummy over the network
    let mut buff = ByteBuff::from_bytes(buff.data());

    //and dummy can also read its self from buffer
    let de_dummy = Dummy::unmarshall(&mut buff).unwrap();

    //ignored data don't ewen get written to buffer in first place and is replaced
    //by default, in any case you can always implement Default trait to your types
    assert_eq!(de_dummy.useless_stuff, Default::default());

    //you can nest how match you want as long as you annotate so
    assert_eq!(10.0, de_dummy.position.x);
    assert_eq!(70.0, de_dummy.position.y);

    // other then all numbers, strings, booleans and bite vectors are supported
    assert_eq!(String::from("Mlokogrgel"), de_dummy.name);
}

Dependencies

~1.5MB
~37K SLoC