TryRead

Trait TryRead 

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

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

Required Methods§

Source

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

Try to read from a byte slice using a specific context.

Read the value out of bytes; the bytes passed in are splitted by offset and should be read at head. If successful, 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))
    }
}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<'a> TryRead<'a> for bool

Source§

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

Source§

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

Source§

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

Source§

impl<'a> TryRead<'a, Endian> for f32

Source§

fn try_read(bytes: &'a [u8], endian: Endian) -> Result<(Self, usize)>

Source§

impl<'a> TryRead<'a, Endian> for f64

Source§

fn try_read(bytes: &'a [u8], endian: Endian) -> Result<(Self, usize)>

Source§

impl<'a> TryRead<'a, Endian> for i8

Source§

fn try_read(bytes: &'a [u8], endian: Endian) -> Result<(Self, usize)>

Source§

impl<'a> TryRead<'a, Endian> for i16

Source§

fn try_read(bytes: &'a [u8], endian: Endian) -> Result<(Self, usize)>

Source§

impl<'a> TryRead<'a, Endian> for i32

Source§

fn try_read(bytes: &'a [u8], endian: Endian) -> Result<(Self, usize)>

Source§

impl<'a> TryRead<'a, Endian> for i64

Source§

fn try_read(bytes: &'a [u8], endian: Endian) -> Result<(Self, usize)>

Source§

impl<'a> TryRead<'a, Endian> for isize

Source§

fn try_read(bytes: &'a [u8], endian: Endian) -> Result<(Self, usize)>

Source§

impl<'a> TryRead<'a, Endian> for u8

Source§

fn try_read(bytes: &'a [u8], endian: Endian) -> Result<(Self, usize)>

Source§

impl<'a> TryRead<'a, Endian> for u16

Source§

fn try_read(bytes: &'a [u8], endian: Endian) -> Result<(Self, usize)>

Source§

impl<'a> TryRead<'a, Endian> for u32

Source§

fn try_read(bytes: &'a [u8], endian: Endian) -> Result<(Self, usize)>

Source§

impl<'a> TryRead<'a, Endian> for u64

Source§

fn try_read(bytes: &'a [u8], endian: Endian) -> Result<(Self, usize)>

Source§

impl<'a> TryRead<'a, Endian> for usize

Source§

fn try_read(bytes: &'a [u8], endian: Endian) -> Result<(Self, usize)>

Source§

impl<'a> TryRead<'a, Str> for &'a str

Source§

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

Implementors§