pub struct LittleEndian<T: EndianConvert>(/* private fields */);Expand description
A wrapper type that stores a value in little-endian byte order.
This type ensures that the wrapped value is always stored in little-endian format, regardless of the system’s native endianness. This is particularly useful for:
- File formats that specify little-endian storage (e.g., BMP, WAV, PE executables)
- USB and Bluetooth protocols
- x86/x64 architecture data structures
- Cross-platform binary data interchange
The wrapper is #[repr(transparent)], meaning it has the same memory layout as
its inner byte array, making it safe to use in packed structs.
§Type Parameters
T- The underlying numeric type that implementsEndianConvert
§Examples
§Basic usage
use byteable::LittleEndian;
let value = LittleEndian::new(0x12345678u32);
// The bytes are always stored in little-endian order
assert_eq!(value.raw_bytes(), [0x78, 0x56, 0x34, 0x12]);
// Get back the native value
assert_eq!(value.get(), 0x12345678u32);§In a struct for file formats
use byteable::{LittleEndian, UnsafeByteable};
#[derive(UnsafeByteable, Debug)]
#[repr(C, packed)]
struct BmpHeader {
signature: [u8; 2], // "BM"
file_size: LittleEndian<u32>, // Little-endian
reserved: u32,
data_offset: LittleEndian<u32>, // Little-endian
}
let header = BmpHeader {
signature: *b"BM",
file_size: 1024.into(),
reserved: 0,
data_offset: 54.into(),
};§Convenient conversions
use byteable::LittleEndian;
// Using From trait
let le: LittleEndian<u16> = 1000.into();
assert_eq!(le.get(), 1000);
// Direct construction
let le = LittleEndian::new(2000u16);
assert_eq!(le.get(), 2000);§Comparison and hashing
use byteable::LittleEndian;
use std::collections::HashSet;
let a = LittleEndian::new(100u32);
let b = LittleEndian::new(100u32);
let c = LittleEndian::new(200u32);
assert_eq!(a, b);
assert!(a < c);
// Can be used in HashSet
let mut set = HashSet::new();
set.insert(a);
assert!(set.contains(&b));Implementations§
Source§impl<T: EndianConvert> LittleEndian<T>
impl<T: EndianConvert> LittleEndian<T>
Sourcepub fn new(value: T) -> Self
pub fn new(value: T) -> Self
Creates a new LittleEndian value from a native value.
The value is converted to little-endian byte order upon construction.
§Examples
use byteable::LittleEndian;
let le = LittleEndian::new(0x1234u16);
assert_eq!(le.raw_bytes(), [0x34, 0x12]);Trait Implementations§
Source§impl<T: EndianConvert> Byteable for LittleEndian<T>
impl<T: EndianConvert> Byteable for LittleEndian<T>
Source§type ByteArray = <T as EndianConvert>::ByteArray
type ByteArray = <T as EndianConvert>::ByteArray
The byte array type used to represent this type. Read more
Source§fn as_byte_array(self) -> Self::ByteArray
fn as_byte_array(self) -> Self::ByteArray
Converts this value into a byte array. Read more
Source§fn from_byte_array(byte_array: Self::ByteArray) -> Self
fn from_byte_array(byte_array: Self::ByteArray) -> Self
Constructs a value from a byte array. Read more
Source§impl<T: Clone + EndianConvert> Clone for LittleEndian<T>
impl<T: Clone + EndianConvert> Clone for LittleEndian<T>
Source§fn clone(&self) -> LittleEndian<T>
fn clone(&self) -> LittleEndian<T>
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl<T: Debug + EndianConvert> Debug for LittleEndian<T>
impl<T: Debug + EndianConvert> Debug for LittleEndian<T>
Source§impl<T: EndianConvert + Default> Default for LittleEndian<T>
impl<T: EndianConvert + Default> Default for LittleEndian<T>
Source§impl<T: EndianConvert> From<T> for LittleEndian<T>
impl<T: EndianConvert> From<T> for LittleEndian<T>
Source§impl<T: Hash + EndianConvert> Hash for LittleEndian<T>
impl<T: Hash + EndianConvert> Hash for LittleEndian<T>
Source§impl<T: Ord + EndianConvert> Ord for LittleEndian<T>
impl<T: Ord + EndianConvert> Ord for LittleEndian<T>
Source§impl<T: PartialEq + EndianConvert> PartialEq for LittleEndian<T>
impl<T: PartialEq + EndianConvert> PartialEq for LittleEndian<T>
Source§impl<T: PartialOrd + EndianConvert> PartialOrd for LittleEndian<T>
impl<T: PartialOrd + EndianConvert> PartialOrd for LittleEndian<T>
impl<T: Copy + EndianConvert> Copy for LittleEndian<T>
impl<T: Eq + EndianConvert> Eq for LittleEndian<T>
Auto Trait Implementations§
impl<T> Freeze for LittleEndian<T>
impl<T> RefUnwindSafe for LittleEndian<T>
impl<T> Send for LittleEndian<T>
impl<T> Sync for LittleEndian<T>
impl<T> Unpin for LittleEndian<T>
impl<T> UnwindSafe for LittleEndian<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more