pub struct BigEndian<T: EndianConvert>(/* private fields */);Expand description
A wrapper type that stores a value in big-endian (network) byte order.
This type ensures that the wrapped value is always stored in big-endian format, regardless of the system’s native endianness. This is particularly useful for:
- Network protocols (which typically use big-endian/“network byte order”)
- File formats that specify big-endian storage
- 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::BigEndian;
let value = BigEndian::new(0x12345678u32);
// The bytes are always stored in big-endian order
assert_eq!(value.raw_bytes(), [0x12, 0x34, 0x56, 0x78]);
// Get back the native value
assert_eq!(value.get(), 0x12345678u32);§In a struct for network protocols
use byteable::{BigEndian, UnsafeByteable};
#[derive(UnsafeByteable, Debug)]
#[repr(C, packed)]
struct TcpHeader {
source_port: BigEndian<u16>, // Network byte order
dest_port: BigEndian<u16>, // Network byte order
sequence: BigEndian<u32>, // Network byte order
}
let header = TcpHeader {
source_port: 80.into(),
dest_port: 8080.into(),
sequence: 12345.into(),
};§Comparison and hashing
use byteable::BigEndian;
use std::collections::HashMap;
let a = BigEndian::new(100u32);
let b = BigEndian::new(100u32);
let c = BigEndian::new(200u32);
assert_eq!(a, b);
assert!(a < c);
// Can be used as HashMap keys
let mut map = HashMap::new();
map.insert(a, "one hundred");
assert_eq!(map.get(&b), Some(&"one hundred"));Implementations§
Source§impl<T: EndianConvert> BigEndian<T>
impl<T: EndianConvert> BigEndian<T>
Sourcepub fn new(value: T) -> Self
pub fn new(value: T) -> Self
Creates a new BigEndian value from a native value.
The value is converted to big-endian byte order upon construction.
§Examples
use byteable::BigEndian;
let be = BigEndian::new(0x1234u16);
assert_eq!(be.raw_bytes(), [0x12, 0x34]);Trait Implementations§
Source§impl<T: EndianConvert> Byteable for BigEndian<T>
impl<T: EndianConvert> Byteable for BigEndian<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: EndianConvert> From<T> for BigEndian<T>
impl<T: EndianConvert> From<T> for BigEndian<T>
Source§impl<T: Ord + EndianConvert> Ord for BigEndian<T>
impl<T: Ord + EndianConvert> Ord for BigEndian<T>
Source§impl<T: PartialOrd + EndianConvert> PartialOrd for BigEndian<T>
impl<T: PartialOrd + EndianConvert> PartialOrd for BigEndian<T>
impl<T: Copy + EndianConvert> Copy for BigEndian<T>
impl<T: Eq + EndianConvert> Eq for BigEndian<T>
Auto Trait Implementations§
impl<T> Freeze for BigEndian<T>
impl<T> RefUnwindSafe for BigEndian<T>
impl<T> Send for BigEndian<T>
impl<T> Sync for BigEndian<T>
impl<T> Unpin for BigEndian<T>
impl<T> UnwindSafe for BigEndian<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