pigeon

Crates.iopigeon
lib.rspigeon
version0.3.2
sourcesrc
created_at2020-05-22 14:39:38.617651
updated_at2020-06-06 14:33:16.385054
descriptionUtilities for efficient packing and unpacking of big-endian binary data.
homepage
repositoryhttps://gitlab.com/tinytown/pigeon
max_upload_size
id244610
size93,439
lumi (lumi-me-not)

documentation

https://docs.rs/pigeon

README

Pigeon

What's this?

A crate for efficient packing and unpacking of big-endian binary data.

It's used in tinytown for its networking code.

Can I see an example?

use {
    pigeon::{
        Writer,
        Reader,
        Target,
        Unpack,
        Pack,
        WriteResult,
        ReadResult,
        ReadError,
        U3,
        U5,
    },
};

#[derive(Debug, PartialEq)]
pub struct Animal {
    pub position: (u8, u8, u8),
    pub fluffy: bool,
    pub name: String,
    pub weight: f32,
}

impl Pack for Animal {
    fn pack<T: Target>(&self, writer: &mut Writer<T>) -> WriteResult<()> {
        writer.write((U3(self.position.0), U3(self.position.1), U3(self.position.2)))?;
        writer.write(self.fluffy)?;
        let name_bytes = self.name.as_bytes();
        writer.write(U5(name_bytes.len() as u8))?;
        writer.write_bytes(name_bytes)?;
        writer.write(self.weight)?;
        Ok(())
    }
}

impl<'a> Unpack<'a> for Animal {
    fn unpack(reader: &mut Reader<'a>) -> ReadResult<Self> {
        let (U3(x), U3(y), U3(z)) = reader.read()?;
        let position = (x, y, z);
        let fluffy = reader.read()?;
        let U5(name_len_u8) = reader.read()?;
        let name_len = name_len_u8 as usize;
        let name_bytes_unaligned = reader.read_bytes(name_len)?;
        let mut name_bytes = [0; 256];
        name_bytes_unaligned.copy_to_slice(&mut name_bytes[0..name_len]);
        let name = String::from_utf8(name_bytes[0..name_len].to_vec()).map_err(|_| ReadError::UnexpectedData)?;
        let weight = reader.read()?;
        Ok(Animal {
            position,
            fluffy,
            name: name.to_owned(),
            weight,
        })
    }
}

let kitty = Animal {
    position: (3, 2, 7),
    fluffy: true,
    name: "Joseph Joestar".to_owned(),
    weight: 12.,
};

let mut buf = [0; 128];

let mut writer = Writer::new(&mut buf[..]);

writer.write(&kitty).unwrap();

let len = writer.finish().unwrap();

let mut reader = Reader::new(&buf[..len]);

let kitty_: Animal = reader.read().unwrap();

assert_eq!(kitty, kitty_);

What license is it under?

AGPLv3 or later

Commit count: 0

cargo fmt