[][src]Crate bytestream

This crate provides a convenient way of writing data to a buffer that implements the standard Read or Write traits.

Foreign types

If the std-types feature is enabled (which it is by default), byte conversion of foreign types is done using the byteorder crate and all data is read and written as big endian. Supported foreign types include u8, u16, u32, u64, i8, i16, i32, i64, String, Vec<T> and HashMap<T, V>.

The reason for only supporting big endian data conversion is that this crate was written with sending data over the network in mind. It should be fairly easy to add support for little endian if anyone would have use for it, but for now it's big endian only.

Examples

use std::io::{Cursor, Read, Result, Write};
use bytestream::Streamable;

#[derive(Debug, PartialEq)]
pub struct Foo {
    bar: String,
    baz: u32,
}

impl Streamable for Foo {
    fn read_from<R: Read>(buffer: &mut R) -> Result<Self> {
        Ok(Self {
            bar: String::read_from(buffer)?,
            baz: u32::read_from(buffer)?,
        })
    }

    fn write_to<W: Write>(&self, buffer: &mut W) -> Result<()> {
        self.bar.write_to(buffer)?;
        self.baz.write_to(buffer)?;
        Ok(())
    }
}

// Create a new instance of `Foo`
let foo = Foo {
    bar: "corgi".to_owned(),
    baz: 37
};

// Write it to a buffer that implements the `Write` trait
let mut buffer = Vec::<u8>::new();
foo.write_to(&mut buffer).unwrap();

// Read it back from the buffer
// We wrap the buffer in a Cursor::<T> that implements the `Read` trait
let mut cursor = Cursor::new(buffer);
let other = Foo::read_from(&mut cursor).unwrap();

assert_eq!(foo, other);

Credits

The inspiration from this crate came from the Stevenarella Minecraft client.

Traits

Streamable

The streamable trait allows for reading and writing bytes to and from a buffer.