[][src]Trait endio::Deserialize

pub trait Deserialize<E: Endianness, R>: Sized {
    fn deserialize(reader: &mut R) -> Res<Self>;
}

Implement this for your types to be able to read them.

Examples

Deserialize a struct:

Note how the trait bound for R is ERead<E>, as we want to use the functionality of this crate to delegate deserialization to the struct's fields.

Note: Rust currently can't recognize sealed traits, so even though the primitive types are implemented, you may need to write where clauses like below for this to work. If/When the compiler gets smarter about sealed traits this won't be necessary.

struct Example {
    a: u8,
    b: bool,
    c: u32,
}
{
    use std::io::Result;
    use endio::{Deserialize, Endianness, ERead};

    impl<E: Endianness, R: ERead<E>> Deserialize<E, R> for Example
        where bool: Deserialize<E, R>,
              u8  : Deserialize<E, R>,
              u32 : Deserialize<E, R> {
        fn deserialize(reader: &mut R) -> Result<Self> {
            let a = reader.read()?;
            let b = reader.read()?;
            let c = reader.read()?;
            Ok(Example { a, b, c })
        }
    }
}
// will then allow you to directly write:
{
    use endio::LERead;
    let mut reader = &b"\x2a\x01\xcf\xfe\xf3\x2c"[..];
    let e: Example = reader.read().unwrap();

    assert_eq!(e, Example { a: 42, b: true, c: 754187983 });
}

Deserialize a primitive / something where you need the bare std::io::Read functionality:

Note how the trait bound for R is Read.

use std::io::{Read, Result};
use endio::{Deserialize, Endianness, ERead};

struct new_u8(u8);

impl<E: Endianness, R: Read> Deserialize<E, R> for new_u8 {
    fn deserialize(reader: &mut R) -> Result<Self> {
        let mut buf = [0; 1];
        reader.read_exact(&mut buf);
        Ok(new_u8(buf[0]))
    }
}

Deserialize with endian-specific code:

Note how instead of using a trait bound on Endianness, we implement Deserialize twice, once for BigEndian and once for LittleEndian.

use std::io::{Read, Result};
use std::mem::size_of;
use endio::{BigEndian, Deserialize, LittleEndian};

struct new_u16(u16);

impl<R: Read> Deserialize<BigEndian, R> for new_u16 {
    fn deserialize(reader: &mut R) -> Result<Self> {
        let mut buf = [0; size_of::<u16>()];
        reader.read_exact(&mut buf)?;
        Ok(new_u16(u16::from_be_bytes(buf)))
    }
}

impl<R: Read> Deserialize<LittleEndian, R> for new_u16 {
    fn deserialize(reader: &mut R) -> Result<Self> {
        let mut buf = [0; size_of::<u16>()];
        reader.read_exact(&mut buf)?;
        Ok(new_u16(u16::from_le_bytes(buf)))
    }
}

Required methods

fn deserialize(reader: &mut R) -> Res<Self>

Deserializes the type by reading from the reader.

Loading content...

Implementations on Foreign Types

impl<E: Endianness, R: Read> Deserialize<E, R> for bool[src]

Reads a bool by reading a byte, returning false for 0, true for 1, and an InvalidData error for any other value.

impl<E: Endianness, R: Read> Deserialize<E, R> for i8[src]

impl<E: Endianness, R: Read> Deserialize<E, R> for u8[src]

impl<R: Read> Deserialize<BigEndian, R> for u16[src]

impl<R: Read> Deserialize<LittleEndian, R> for u16[src]

impl<R: Read> Deserialize<BigEndian, R> for u32[src]

impl<R: Read> Deserialize<LittleEndian, R> for u32[src]

impl<R: Read> Deserialize<BigEndian, R> for u64[src]

impl<R: Read> Deserialize<LittleEndian, R> for u64[src]

impl<R: Read> Deserialize<BigEndian, R> for u128[src]

impl<R: Read> Deserialize<LittleEndian, R> for u128[src]

impl<R: Read> Deserialize<BigEndian, R> for i16[src]

impl<R: Read> Deserialize<LittleEndian, R> for i16[src]

impl<R: Read> Deserialize<BigEndian, R> for i32[src]

impl<R: Read> Deserialize<LittleEndian, R> for i32[src]

impl<R: Read> Deserialize<BigEndian, R> for i64[src]

impl<R: Read> Deserialize<LittleEndian, R> for i64[src]

impl<R: Read> Deserialize<BigEndian, R> for i128[src]

impl<R: Read> Deserialize<LittleEndian, R> for i128[src]

impl<E: Endianness, R: ERead<E>> Deserialize<E, R> for f32 where
    u32: Deserialize<E, R>, 
[src]

impl<E: Endianness, R: ERead<E>> Deserialize<E, R> for f64 where
    u64: Deserialize<E, R>, 
[src]

Loading content...

Implementors

Loading content...