num_traits::ops::bytes

Trait FromBytes

source
pub trait FromBytes: Sized {
    type Bytes: NumBytes + ?Sized;

    // Required methods
    fn from_be_bytes(bytes: &Self::Bytes) -> Self;
    fn from_le_bytes(bytes: &Self::Bytes) -> Self;

    // Provided method
    fn from_ne_bytes(bytes: &Self::Bytes) -> Self { ... }
}

Required Associated Types§

Required Methods§

source

fn from_be_bytes(bytes: &Self::Bytes) -> Self

Create a number from its representation as a byte array in big endian.

§Examples
use num_traits::FromBytes;

let value: u32 = FromBytes::from_be_bytes(&[0x12, 0x34, 0x56, 0x78]);
assert_eq!(value, 0x12345678);
source

fn from_le_bytes(bytes: &Self::Bytes) -> Self

Create a number from its representation as a byte array in little endian.

§Examples
use num_traits::FromBytes;

let value: u32 = FromBytes::from_le_bytes(&[0x78, 0x56, 0x34, 0x12]);
assert_eq!(value, 0x12345678);

Provided Methods§

source

fn from_ne_bytes(bytes: &Self::Bytes) -> Self

Create a number from its memory representation as a byte array in native endianness.

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

§Examples
use num_traits::FromBytes;

#[cfg(target_endian = "big")]
let bytes = [0x12, 0x34, 0x56, 0x78];

#[cfg(target_endian = "little")]
let bytes = [0x78, 0x56, 0x34, 0x12];

let value: u32 = FromBytes::from_ne_bytes(&bytes);
assert_eq!(value, 0x12345678)

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl FromBytes for f32

source§

type Bytes = [u8; 4]

source§

fn from_be_bytes(bytes: &Self::Bytes) -> Self

source§

fn from_le_bytes(bytes: &Self::Bytes) -> Self

source§

fn from_ne_bytes(bytes: &Self::Bytes) -> Self

source§

impl FromBytes for f64

source§

type Bytes = [u8; 8]

source§

fn from_be_bytes(bytes: &Self::Bytes) -> Self

source§

fn from_le_bytes(bytes: &Self::Bytes) -> Self

source§

fn from_ne_bytes(bytes: &Self::Bytes) -> Self

source§

impl FromBytes for i8

source§

type Bytes = [u8; 1]

source§

fn from_be_bytes(bytes: &Self::Bytes) -> Self

source§

fn from_le_bytes(bytes: &Self::Bytes) -> Self

source§

fn from_ne_bytes(bytes: &Self::Bytes) -> Self

source§

impl FromBytes for i16

source§

type Bytes = [u8; 2]

source§

fn from_be_bytes(bytes: &Self::Bytes) -> Self

source§

fn from_le_bytes(bytes: &Self::Bytes) -> Self

source§

fn from_ne_bytes(bytes: &Self::Bytes) -> Self

source§

impl FromBytes for i32

source§

type Bytes = [u8; 4]

source§

fn from_be_bytes(bytes: &Self::Bytes) -> Self

source§

fn from_le_bytes(bytes: &Self::Bytes) -> Self

source§

fn from_ne_bytes(bytes: &Self::Bytes) -> Self

source§

impl FromBytes for i64

source§

type Bytes = [u8; 8]

source§

fn from_be_bytes(bytes: &Self::Bytes) -> Self

source§

fn from_le_bytes(bytes: &Self::Bytes) -> Self

source§

fn from_ne_bytes(bytes: &Self::Bytes) -> Self

source§

impl FromBytes for i128

source§

type Bytes = [u8; 16]

source§

fn from_be_bytes(bytes: &Self::Bytes) -> Self

source§

fn from_le_bytes(bytes: &Self::Bytes) -> Self

source§

fn from_ne_bytes(bytes: &Self::Bytes) -> Self

source§

impl FromBytes for isize

source§

type Bytes = [u8; 8]

source§

fn from_be_bytes(bytes: &Self::Bytes) -> Self

source§

fn from_le_bytes(bytes: &Self::Bytes) -> Self

source§

fn from_ne_bytes(bytes: &Self::Bytes) -> Self

source§

impl FromBytes for u8

source§

type Bytes = [u8; 1]

source§

fn from_be_bytes(bytes: &Self::Bytes) -> Self

source§

fn from_le_bytes(bytes: &Self::Bytes) -> Self

source§

fn from_ne_bytes(bytes: &Self::Bytes) -> Self

source§

impl FromBytes for u16

source§

type Bytes = [u8; 2]

source§

fn from_be_bytes(bytes: &Self::Bytes) -> Self

source§

fn from_le_bytes(bytes: &Self::Bytes) -> Self

source§

fn from_ne_bytes(bytes: &Self::Bytes) -> Self

source§

impl FromBytes for u32

source§

type Bytes = [u8; 4]

source§

fn from_be_bytes(bytes: &Self::Bytes) -> Self

source§

fn from_le_bytes(bytes: &Self::Bytes) -> Self

source§

fn from_ne_bytes(bytes: &Self::Bytes) -> Self

source§

impl FromBytes for u64

source§

type Bytes = [u8; 8]

source§

fn from_be_bytes(bytes: &Self::Bytes) -> Self

source§

fn from_le_bytes(bytes: &Self::Bytes) -> Self

source§

fn from_ne_bytes(bytes: &Self::Bytes) -> Self

source§

impl FromBytes for u128

source§

type Bytes = [u8; 16]

source§

fn from_be_bytes(bytes: &Self::Bytes) -> Self

source§

fn from_le_bytes(bytes: &Self::Bytes) -> Self

source§

fn from_ne_bytes(bytes: &Self::Bytes) -> Self

source§

impl FromBytes for usize

source§

type Bytes = [u8; 8]

source§

fn from_be_bytes(bytes: &Self::Bytes) -> Self

source§

fn from_le_bytes(bytes: &Self::Bytes) -> Self

source§

fn from_ne_bytes(bytes: &Self::Bytes) -> Self

Implementors§