pub unsafe trait ToByteSlicewhere
    Self: Sized,{
    // Required method
    fn to_byte_slice<T: AsRef<[Self]> + ?Sized>(
        slice: &T
    ) -> Result<&[u8], Error>;
}
Expand description

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

Usually using the AsByteSlice trait is more convenient.

Example

use byte_slice_cast::*;

let slice: [u16; 3] = [0x0102, 0x0304, 0x0506];
let converted_slice = ToByteSlice::to_byte_slice(&slice).unwrap();

if cfg!(target_endian = "big") {
    assert_eq!(converted_slice, &[1u8, 2u8, 3u8, 4u8, 5u8, 6u8]);
} else {
    assert_eq!(converted_slice, &[2u8, 1u8, 4u8, 3u8, 6u8, 5u8]);
}

Required Methods§

source

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

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

Implementations on Foreign Types§

source§

impl ToByteSlice for u8

source§

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

source§

impl ToByteSlice for i128

source§

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

source§

impl ToByteSlice for i8

source§

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

source§

impl ToByteSlice for u32

source§

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

source§

impl ToByteSlice for u128

source§

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

source§

impl ToByteSlice for i32

source§

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

source§

impl ToByteSlice for f32

source§

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

source§

impl ToByteSlice for f64

source§

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

source§

impl ToByteSlice for u16

source§

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

source§

impl ToByteSlice for u64

source§

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

source§

impl ToByteSlice for i64

source§

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

source§

impl ToByteSlice for i16

source§

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

Implementors§