Trait byte::TryRead[][src]

pub trait TryRead<'a, Ctx = ()> where
    Self: Sized
{ fn try_read(bytes: &'a [u8], ctx: Ctx) -> Result<(Self, usize)>; }

A data structure that can be deserialized. Types implement this trait can be read() from byte slice.

Required methods

fn try_read(bytes: &'a [u8], ctx: Ctx) -> Result<(Self, usize)>[src]

Try to read from bytes using context.

Read the value out of bytes; the bytes passed in is splitted by offset and should be read at head. If success, try_read() should return a tuple with the value and the number of bytes consumed.

Example

use byte::*;

// Demo type showing how to read boolean from bytes.
// This functionality is already provided by this crate.
pub struct Bool(bool);

impl<'a> TryRead<'a> for Bool {
    #[inline]
    fn try_read(bytes: &'a [u8], _ctx: ()) -> Result<(Self, usize)> {
        check_len(bytes, 1)?;

        Ok((Bool(bytes[0] != 0), 1))
    }
}
Loading content...

Implementors

impl<'a> TryRead<'a, ()> for bool[src]

impl<'a> TryRead<'a, Bytes> for &'a [u8][src]

impl<'a> TryRead<'a, Endian> for f32[src]

impl<'a> TryRead<'a, Endian> for f64[src]

impl<'a> TryRead<'a, Endian> for i8[src]

impl<'a> TryRead<'a, Endian> for i16[src]

impl<'a> TryRead<'a, Endian> for i32[src]

impl<'a> TryRead<'a, Endian> for i64[src]

impl<'a> TryRead<'a, Endian> for isize[src]

impl<'a> TryRead<'a, Endian> for u8[src]

impl<'a> TryRead<'a, Endian> for u16[src]

impl<'a> TryRead<'a, Endian> for u32[src]

impl<'a> TryRead<'a, Endian> for u64[src]

impl<'a> TryRead<'a, Endian> for usize[src]

impl<'a> TryRead<'a, Str> for &'a str[src]

Loading content...