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, IntoByteArray};
let value = BigEndian::new(0x12345678u32);
// The bytes are always stored in big-endian order
assert_eq!(value.into_byte_array(), [0x12, 0x34, 0x56, 0x78]);
// Get back the native value
assert_eq!(value.get(), 0x12345678u32);§In a struct for network protocols
use byteable::{BigEndian, IntoByteArray};
#[derive(byteable::UnsafeByteableTransmute, 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>
Trait Implementations§
Source§impl<T: EndianConvert> AssociatedByteArray for BigEndian<T>
impl<T: EndianConvert> AssociatedByteArray for BigEndian<T>
Source§impl<T: EndianConvert> From<T> for BigEndian<T>
impl<T: EndianConvert> From<T> for BigEndian<T>
Source§impl<T: EndianConvert> FromByteArray for BigEndian<T>
impl<T: EndianConvert> FromByteArray for BigEndian<T>
Source§fn from_byte_array(byte_array: Self::ByteArray) -> Self
fn from_byte_array(byte_array: Self::ByteArray) -> Self
Constructs a value from its byte array representation. Read more
Source§impl<T: EndianConvert> IntoByteArray for BigEndian<T>
impl<T: EndianConvert> IntoByteArray for BigEndian<T>
Source§fn into_byte_array(self) -> Self::ByteArray
fn into_byte_array(self) -> Self::ByteArray
Converts
self into its byte array representation. Read moreSource§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>
impl ValidBytecastMarker for BigEndian<f32>
impl ValidBytecastMarker for BigEndian<f64>
impl ValidBytecastMarker for BigEndian<i128>
impl ValidBytecastMarker for BigEndian<i16>
impl ValidBytecastMarker for BigEndian<i32>
impl ValidBytecastMarker for BigEndian<i64>
impl ValidBytecastMarker for BigEndian<u128>
impl ValidBytecastMarker for BigEndian<u16>
impl ValidBytecastMarker for BigEndian<u32>
impl ValidBytecastMarker for BigEndian<u64>
Auto Trait Implementations§
impl<T> Freeze for BigEndian<T>where
T: Freeze,
impl<T> RefUnwindSafe for BigEndian<T>where
T: RefUnwindSafe,
impl<T> Send for BigEndian<T>where
T: Send,
impl<T> Sync for BigEndian<T>where
T: Sync,
impl<T> Unpin for BigEndian<T>where
T: Unpin,
impl<T> UnwindSafe for BigEndian<T>where
T: UnwindSafe,
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> TryFromByteArray for Twhere
T: FromByteArray,
impl<T> TryFromByteArray for Twhere
T: FromByteArray,
Source§type Error = Infallible
type Error = Infallible
The type returned in the event of a conversion error.
Source§fn try_from_byte_array(
byte_array: <T as AssociatedByteArray>::ByteArray,
) -> Result<T, <T as TryFromByteArray>::Error>
fn try_from_byte_array( byte_array: <T as AssociatedByteArray>::ByteArray, ) -> Result<T, <T as TryFromByteArray>::Error>
Attempts to construct a value from its byte array representation.
Source§impl<T> TryIntoByteArray for Twhere
T: IntoByteArray,
impl<T> TryIntoByteArray for Twhere
T: IntoByteArray,
Source§type Error = Infallible
type Error = Infallible
The type returned in the event of a conversion error.
Source§fn try_into_byte_array(
self,
) -> Result<<T as AssociatedByteArray>::ByteArray, <T as TryIntoByteArray>::Error>
fn try_into_byte_array( self, ) -> Result<<T as AssociatedByteArray>::ByteArray, <T as TryIntoByteArray>::Error>
Attempts to convert
self into its byte array representation.