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§
Required Methods§
Sourcefn from_le_bytes(bytes: &[u8]) -> Self
fn from_le_bytes(bytes: &[u8]) -> Self
Sourcefn from_be_bytes(bytes: &[u8]) -> Self
fn from_be_bytes(bytes: &[u8]) -> Self
Sourcefn write_le_bytes(self, buf: &mut [u8])
fn write_le_bytes(self, buf: &mut [u8])
Sourcefn write_be_bytes(self, buf: &mut [u8])
fn write_be_bytes(self, buf: &mut [u8])
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.