#binary-data #packing #unpacking #networking

pigeon

Utilities for efficient packing and unpacking of big-endian binary data

6 releases

0.3.2 Jun 6, 2020
0.3.1 Jun 5, 2020
0.2.0 May 30, 2020
0.1.1 May 29, 2020

#1881 in Encoding

AGPL-3.0-or-later

51KB
704 lines

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

No runtime deps