Trait FromBytes

trait FromBytes: Sized

Associated Types

type Bytes: TraitBound { trait_: Path { path: "NumBytes", id: Id(1880), args: None }, generic_params: [], modifier: None } + TraitBound { trait_: Path { path: "Sized", id: Id(378), args: None }, generic_params: [], modifier: Maybe }

Required Methods

fn from_be_bytes(bytes: &<Self as >::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);
fn from_le_bytes(bytes: &<Self as >::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

fn from_ne_bytes(bytes: &<Self as >::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)

Implementors