byte_slice_cast

Trait FromByteSlice

Source
pub unsafe trait FromByteSlice
where Self: Sized,
{ // Required methods fn from_byte_slice<T: AsRef<[u8]> + ?Sized>(_: &T) -> Result<&[Self], Error>; fn from_mut_byte_slice<T: AsMut<[u8]> + ?Sized>( _: &mut T, ) -> Result<&mut [Self], Error>; }
Expand description

Trait for converting from a byte slice to a slice of a fundamental, built-in numeric type.

Usually using the AsSliceOf and AsMutSliceOf traits is more convenient.

§Example

use byte_slice_cast::*;

let slice = [1u8, 2u8, 3u8, 4u8, 5u8, 6u8];
let converted_slice = <u16 as FromByteSlice>::from_byte_slice(&slice).unwrap();

if cfg!(target_endian = "big") {
    assert_eq!(converted_slice, &[0x0102, 0x0304, 0x0506]);
} else {
    assert_eq!(converted_slice, &[0x0201, 0x0403, 0x0605]);
}

Required Methods§

Source

fn from_byte_slice<T: AsRef<[u8]> + ?Sized>(_: &T) -> Result<&[Self], Error>

Convert from an immutable byte slice to a immutable slice of a fundamental, built-in numeric type

Source

fn from_mut_byte_slice<T: AsMut<[u8]> + ?Sized>( _: &mut T, ) -> Result<&mut [Self], Error>

Convert from an mutable byte slice to a mutable slice of a fundamental, built-in numeric type

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

Source§

fn from_byte_slice<T: AsRef<[u8]> + ?Sized>(slice: &T) -> Result<&[f32], Error>

Source§

fn from_mut_byte_slice<T: AsMut<[u8]> + ?Sized>( slice: &mut T, ) -> Result<&mut [f32], Error>

Source§

impl FromByteSlice for f64

Source§

fn from_byte_slice<T: AsRef<[u8]> + ?Sized>(slice: &T) -> Result<&[f64], Error>

Source§

fn from_mut_byte_slice<T: AsMut<[u8]> + ?Sized>( slice: &mut T, ) -> Result<&mut [f64], Error>

Source§

impl FromByteSlice for i8

Source§

fn from_byte_slice<T: AsRef<[u8]> + ?Sized>(slice: &T) -> Result<&[i8], Error>

Source§

fn from_mut_byte_slice<T: AsMut<[u8]> + ?Sized>( slice: &mut T, ) -> Result<&mut [i8], Error>

Source§

impl FromByteSlice for i16

Source§

fn from_byte_slice<T: AsRef<[u8]> + ?Sized>(slice: &T) -> Result<&[i16], Error>

Source§

fn from_mut_byte_slice<T: AsMut<[u8]> + ?Sized>( slice: &mut T, ) -> Result<&mut [i16], Error>

Source§

impl FromByteSlice for i32

Source§

fn from_byte_slice<T: AsRef<[u8]> + ?Sized>(slice: &T) -> Result<&[i32], Error>

Source§

fn from_mut_byte_slice<T: AsMut<[u8]> + ?Sized>( slice: &mut T, ) -> Result<&mut [i32], Error>

Source§

impl FromByteSlice for i64

Source§

fn from_byte_slice<T: AsRef<[u8]> + ?Sized>(slice: &T) -> Result<&[i64], Error>

Source§

fn from_mut_byte_slice<T: AsMut<[u8]> + ?Sized>( slice: &mut T, ) -> Result<&mut [i64], Error>

Source§

impl FromByteSlice for i128

Source§

fn from_byte_slice<T: AsRef<[u8]> + ?Sized>(slice: &T) -> Result<&[i128], Error>

Source§

fn from_mut_byte_slice<T: AsMut<[u8]> + ?Sized>( slice: &mut T, ) -> Result<&mut [i128], Error>

Source§

impl FromByteSlice for u8

Source§

fn from_byte_slice<T: AsRef<[u8]> + ?Sized>(slice: &T) -> Result<&[u8], Error>

Source§

fn from_mut_byte_slice<T: AsMut<[u8]> + ?Sized>( slice: &mut T, ) -> Result<&mut [u8], Error>

Source§

impl FromByteSlice for u16

Source§

fn from_byte_slice<T: AsRef<[u8]> + ?Sized>(slice: &T) -> Result<&[u16], Error>

Source§

fn from_mut_byte_slice<T: AsMut<[u8]> + ?Sized>( slice: &mut T, ) -> Result<&mut [u16], Error>

Source§

impl FromByteSlice for u32

Source§

fn from_byte_slice<T: AsRef<[u8]> + ?Sized>(slice: &T) -> Result<&[u32], Error>

Source§

fn from_mut_byte_slice<T: AsMut<[u8]> + ?Sized>( slice: &mut T, ) -> Result<&mut [u32], Error>

Source§

impl FromByteSlice for u64

Source§

fn from_byte_slice<T: AsRef<[u8]> + ?Sized>(slice: &T) -> Result<&[u64], Error>

Source§

fn from_mut_byte_slice<T: AsMut<[u8]> + ?Sized>( slice: &mut T, ) -> Result<&mut [u64], Error>

Source§

impl FromByteSlice for u128

Source§

fn from_byte_slice<T: AsRef<[u8]> + ?Sized>(slice: &T) -> Result<&[u128], Error>

Source§

fn from_mut_byte_slice<T: AsMut<[u8]> + ?Sized>( slice: &mut T, ) -> Result<&mut [u128], Error>

Implementors§