Skip to main content

Primitive

Trait Primitive 

Source
pub trait Primitive:
    Sized
    + Copy
    + Sealed {
    const SIZE: usize;

    // Required methods
    fn from_le_bytes(bytes: &[u8]) -> Self;
    fn from_be_bytes(bytes: &[u8]) -> Self;
    fn write_le_bytes(self, buf: &mut [u8]);
    fn write_be_bytes(self, buf: &mut [u8]);
}
Expand description

Sealed trait for numeric primitives that support endian-aware byte I/O.

Implemented for: u8, i8, u16, i16, u32, i32, u64, i64, f32, f64.

This trait is sealed — it cannot be implemented outside of this crate. For user-defined types, implement ReadFrom and WriteTo instead.

§Examples

use cowfile::CowFile;

let pf = CowFile::from_vec(vec![0u8; 16]);

pf.write_le::<u32>(0, 0xDEADBEEF).unwrap();
assert_eq!(pf.read_le::<u32>(0).unwrap(), 0xDEADBEEF);

pf.write_be::<u16>(8, 0xCAFE).unwrap();
assert_eq!(pf.read_be::<u16>(8).unwrap(), 0xCAFE);

Required Associated Constants§

Source

const SIZE: usize

The size of this type in bytes.

Required Methods§

Source

fn from_le_bytes(bytes: &[u8]) -> Self

Decodes a value from little-endian bytes.

§Panics

Panics if bytes.len() < Self::SIZE.

Source

fn from_be_bytes(bytes: &[u8]) -> Self

Decodes a value from big-endian bytes.

§Panics

Panics if bytes.len() < Self::SIZE.

Source

fn write_le_bytes(self, buf: &mut [u8])

Encodes this value as little-endian bytes into buf.

§Panics

Panics if buf.len() < Self::SIZE.

Source

fn write_be_bytes(self, buf: &mut [u8])

Encodes this value as big-endian bytes into buf.

§Panics

Panics if buf.len() < Self::SIZE.

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 Primitive for f32

Source§

const SIZE: usize

Source§

fn from_le_bytes(bytes: &[u8]) -> Self

Source§

fn from_be_bytes(bytes: &[u8]) -> Self

Source§

fn write_le_bytes(self, buf: &mut [u8])

Source§

fn write_be_bytes(self, buf: &mut [u8])

Source§

impl Primitive for f64

Source§

const SIZE: usize

Source§

fn from_le_bytes(bytes: &[u8]) -> Self

Source§

fn from_be_bytes(bytes: &[u8]) -> Self

Source§

fn write_le_bytes(self, buf: &mut [u8])

Source§

fn write_be_bytes(self, buf: &mut [u8])

Source§

impl Primitive for i8

Source§

const SIZE: usize

Source§

fn from_le_bytes(bytes: &[u8]) -> Self

Source§

fn from_be_bytes(bytes: &[u8]) -> Self

Source§

fn write_le_bytes(self, buf: &mut [u8])

Source§

fn write_be_bytes(self, buf: &mut [u8])

Source§

impl Primitive for i16

Source§

const SIZE: usize

Source§

fn from_le_bytes(bytes: &[u8]) -> Self

Source§

fn from_be_bytes(bytes: &[u8]) -> Self

Source§

fn write_le_bytes(self, buf: &mut [u8])

Source§

fn write_be_bytes(self, buf: &mut [u8])

Source§

impl Primitive for i32

Source§

const SIZE: usize

Source§

fn from_le_bytes(bytes: &[u8]) -> Self

Source§

fn from_be_bytes(bytes: &[u8]) -> Self

Source§

fn write_le_bytes(self, buf: &mut [u8])

Source§

fn write_be_bytes(self, buf: &mut [u8])

Source§

impl Primitive for i64

Source§

const SIZE: usize

Source§

fn from_le_bytes(bytes: &[u8]) -> Self

Source§

fn from_be_bytes(bytes: &[u8]) -> Self

Source§

fn write_le_bytes(self, buf: &mut [u8])

Source§

fn write_be_bytes(self, buf: &mut [u8])

Source§

impl Primitive for u8

Source§

const SIZE: usize

Source§

fn from_le_bytes(bytes: &[u8]) -> Self

Source§

fn from_be_bytes(bytes: &[u8]) -> Self

Source§

fn write_le_bytes(self, buf: &mut [u8])

Source§

fn write_be_bytes(self, buf: &mut [u8])

Source§

impl Primitive for u16

Source§

const SIZE: usize

Source§

fn from_le_bytes(bytes: &[u8]) -> Self

Source§

fn from_be_bytes(bytes: &[u8]) -> Self

Source§

fn write_le_bytes(self, buf: &mut [u8])

Source§

fn write_be_bytes(self, buf: &mut [u8])

Source§

impl Primitive for u32

Source§

const SIZE: usize

Source§

fn from_le_bytes(bytes: &[u8]) -> Self

Source§

fn from_be_bytes(bytes: &[u8]) -> Self

Source§

fn write_le_bytes(self, buf: &mut [u8])

Source§

fn write_be_bytes(self, buf: &mut [u8])

Source§

impl Primitive for u64

Source§

const SIZE: usize

Source§

fn from_le_bytes(bytes: &[u8]) -> Self

Source§

fn from_be_bytes(bytes: &[u8]) -> Self

Source§

fn write_le_bytes(self, buf: &mut [u8])

Source§

fn write_be_bytes(self, buf: &mut [u8])

Implementors§