FromBytes

Trait FromBytes 

Source
pub trait FromBytes: Sized {
    type Error;

    // Required method
    fn from_bytes(bytes: &mut Bytes<'_>) -> Result<Self, Self::Error>;
}
Expand description

Fallible conversion of bytes to a new type.

§Examples

use frombytes::{Bytes, Error, FromBytes};

struct MyStruct {
    a: u32,
    b: i16,
}

impl FromBytes for MyStruct {
    // we simply use the same error that primitives use
    // but any error could be used
    type Error = Error;

    fn from_bytes(bytes: &mut Bytes) -> Result<Self, Self::Error> {
        // uses type inference to know to read a `u32`
        let a = bytes.read()?;
        // uses type inference to know to read a `i16`
        let b = bytes.read()?;
        Ok(Self { a, b })
    }
}

Required Associated Types§

Source

type Error

The associated error which can be returned from parsing.

All primitive types as well as radiotap fields implementing this trait set this error to Error.

Required Methods§

Source

fn from_bytes(bytes: &mut Bytes<'_>) -> Result<Self, Self::Error>

Construct a type from bytes.

This method is often used implicitly through Bytes’s read method.

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 FromBytes for i8

Source§

type Error = Error

Source§

fn from_bytes(bytes: &mut Bytes<'_>) -> Result<Self>

Source§

impl FromBytes for i16

Source§

type Error = Error

Source§

fn from_bytes(bytes: &mut Bytes<'_>) -> Result<Self>

Source§

impl FromBytes for i32

Source§

type Error = Error

Source§

fn from_bytes(bytes: &mut Bytes<'_>) -> Result<Self>

Source§

impl FromBytes for i64

Source§

type Error = Error

Source§

fn from_bytes(bytes: &mut Bytes<'_>) -> Result<Self>

Source§

impl FromBytes for i128

Source§

type Error = Error

Source§

fn from_bytes(bytes: &mut Bytes<'_>) -> Result<Self>

Source§

impl FromBytes for u8

Source§

type Error = Error

Source§

fn from_bytes(bytes: &mut Bytes<'_>) -> Result<Self>

Source§

impl FromBytes for u16

Source§

type Error = Error

Source§

fn from_bytes(bytes: &mut Bytes<'_>) -> Result<Self>

Source§

impl FromBytes for u32

Source§

type Error = Error

Source§

fn from_bytes(bytes: &mut Bytes<'_>) -> Result<Self>

Source§

impl FromBytes for u64

Source§

type Error = Error

Source§

fn from_bytes(bytes: &mut Bytes<'_>) -> Result<Self>

Source§

impl FromBytes for u128

Source§

type Error = Error

Source§

fn from_bytes(bytes: &mut Bytes<'_>) -> Result<Self>

Source§

impl<T, E> FromBytes for [T; 1]
where T: FromBytes<Error = E> + Default,

Source§

type Error = E

Source§

fn from_bytes(bytes: &mut Bytes<'_>) -> Result<Self, E>

Source§

impl<T, E> FromBytes for [T; 2]
where T: FromBytes<Error = E> + Default,

Source§

type Error = E

Source§

fn from_bytes(bytes: &mut Bytes<'_>) -> Result<Self, E>

Source§

impl<T, E> FromBytes for [T; 3]
where T: FromBytes<Error = E> + Default,

Source§

type Error = E

Source§

fn from_bytes(bytes: &mut Bytes<'_>) -> Result<Self, E>

Source§

impl<T, E> FromBytes for [T; 4]
where T: FromBytes<Error = E> + Default,

Source§

type Error = E

Source§

fn from_bytes(bytes: &mut Bytes<'_>) -> Result<Self, E>

Implementors§