pub trait TryFromBytes: Sized {
    type Bytes: ByteArray;
    type Error;

    const PREFERS_LE: bool = true;

    // Required methods
    fn try_from_le_bytes(bytes: Self::Bytes) -> Result<Self, Self::Error>;
    fn try_from_be_bytes(bytes: Self::Bytes) -> Result<Self, Self::Error>;

    // Provided methods
    fn try_from_bytes(bytes: Self::Bytes) -> Result<Self, Self::Error> { ... }
    fn try_from_ne_bytes(bytes: Self::Bytes) -> Result<Self, Self::Error> { ... }
}
Expand description

Try to create a value from its representation as a packed stack byte array of a fixed size.

Most times, the method try_from_bytes should be used, as it ensures consistency by respecting the byte order set by the PREFERS_LE associated constant.

Required Associated Types§

source

type Bytes: ByteArray

A byte array which can store a packed representation of this type.

source

type Error

A type containing the failure of creating a value of the type from bytes.

Provided Associated Constants§

source

const PREFERS_LE: bool = true

Is it preferred to represent this type as bytes in the little endian order?

Required Methods§

source

fn try_from_le_bytes(bytes: Self::Bytes) -> Result<Self, Self::Error>

Try to create a value of this type from its representation as a byte array in little endian.

source

fn try_from_be_bytes(bytes: Self::Bytes) -> Result<Self, Self::Error>

Try to create a value of this type from its representation as a byte array in big endian.

Provided Methods§

source

fn try_from_bytes(bytes: Self::Bytes) -> Result<Self, Self::Error>

Try to create a value of this type from its representation as a byte array in the preferred byte order, set in the associated constant PREFERS_LE.

source

fn try_from_ne_bytes(bytes: Self::Bytes) -> Result<Self, Self::Error>

Try to create a value of this type from its representation as a byte array in native endian.

As the target platform’s native endianness is used, portable code likely wants to use try_from_le_bytes or try_from_be_bytes, as appropriate instead.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl TryFromBytes for bool

source§

impl TryFromBytes for char

Implementors§

source§

impl<B: ByteArray, T: FromBytes<Bytes = B>> TryFromBytes for T

§

type Bytes = B

§

type Error = Infallible