[][src]Crate bytebuff

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.


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

struct Point {
    x: f32,
    y: f32,

struct Dummy {
    position: Point,
    name: String,
    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);


pub use marshall_derive;



ByteBuff is a convenient helper for game networking. Using this is similar to using ThreadRng from rand package. Example speaks for it self.


ReadError informs about the read overflow, this happens when you for example try to read usize from buffer where is only one bool. In this case you would get ReadError { size: 1, overflow: 8 }