[−][src]Crate byte_slice_cast
Safely cast bytes slices from/to slices of built-in fundamental numeric types.
The provided traits here allow safe casting between byte slices and slices of fundamental numeric types, like integers and floating point numbers. During casting, checks are performed to ensure that the output slice is safe to use: the input slice must be properly aligned for the output type and contain an integer number of values.
Instead of working only on slices, the traits work on AsRef<[T]>
in the immutable case and on
AsMut<[T]>
for the mutable case. As such, it is possible to directly work on e.g. Vec<T>
and Box<[T]>
too.
The content of the output slice will be bitwise equivalent to the input slice, as such extra care has to be taken with regard to endianness.
Example with slices
use byte_slice_cast::*; let slice = [0x0102u16, 0x0304u16, 0x0506u16]; let converted_slice = slice.as_byte_slice(); if cfg!(target_endian = "big") { assert_eq!(converted_slice, &[1, 2, 3, 4, 5, 6]); } else { assert_eq!(converted_slice, &[2, 1, 4, 3, 6, 5]); } let converted_back_slice = converted_slice.as_slice_of::<u16>().unwrap(); assert_eq!(converted_back_slice, &slice);
Example with mutable slices
use byte_slice_cast::*; let mut slice = [0u32; 1]; let mut converted_slice = slice.as_mut_byte_slice(); converted_slice.copy_from_slice(&[0x12, 0x34, 0x56, 0x78]); let mut converted_slice = converted_slice.as_mut_slice_of::<u16>().unwrap(); converted_slice[0] = 0xffff; if cfg!(target_endian = "big") { assert_eq!(&slice, &[0xffff5678]); } else { assert_eq!(&slice, &[0x7856ffff]); }
Example with Vec<T>
use byte_slice_cast::*; let vec = vec![0x0102u16, 0x0304u16, 0x0506u16]; let converted_vec = vec.into_byte_vec(); if cfg!(target_endian = "big") { assert_eq!(converted_vec, &[1, 2, 3, 4, 5, 6]); } else { assert_eq!(converted_vec, &[2, 1, 4, 3, 6, 5]); } let converted_back_vec = converted_vec.into_vec_of::<u16>().unwrap(); assert_eq!(&converted_back_vec[..], &[0x0102u16, 0x0304u16, 0x0506u16]);
Enums
Error | Possible errors during slice conversion. |
Traits
AsByteSlice | Trait for converting from an immutable slice of a fundamental, built-in numeric type to an immutable byte slice. |
AsMutByteSlice | Trait for converting from a mutable slice of a fundamental, built-in numeric type to a mutable byte slice. |
AsMutSliceOf | Trait for converting from a mutable byte slice to a mutable slice of a fundamental, built-in numeric type. |
AsSliceOf | Trait for converting from a byte slice to a slice of a fundamental, built-in numeric type. |
FromByteSlice | Trait for converting from a byte slice to a slice of a fundamental, built-in numeric type. |
FromByteVec | Trait for converting from a byte |
IntoByteVec | Trait for converting from a |
IntoVecOf | Trait for converting from a byte |
ToByteSlice | Trait for converting from an immutable slice of a fundamental, built-in numeric type to an immutable byte slice. |
ToMutByteSlice | Trait for converting from a mutable slice of a fundamental, built-in numeric type to a mutable byte slice. |