pub trait Bytes: Sized {
type Array: AsRef<[u8]>;
// Required methods
fn to_bytes(&self) -> Self::Array;
fn from_bytes(bytes: &[u8]) -> Result<Self>;
}Expand description
Trait for types that can be serialized to/from bytes with explicit byte order.
This trait uses LITTLE-ENDIAN byte order for all numeric types, making the data
portable across different endianness systems (x86, ARM, etc.). This is the key
difference from ZeroCopyVec, which uses native byte order and is not portable.
Use this trait when:
- You need cross-platform compatibility
- You’re sharing data between systems with different endianness
- You need custom serialization logic
For maximum performance on a single system, use ZeroCopyVec instead.
Required Associated Types§
Required Methods§
Sourcefn to_bytes(&self) -> Self::Array
fn to_bytes(&self) -> Self::Array
Serializes this value to bytes.
For numeric types, this uses little-endian byte order (via to_le_bytes).
Sourcefn from_bytes(bytes: &[u8]) -> Result<Self>
fn from_bytes(bytes: &[u8]) -> Result<Self>
Deserializes a value from bytes.
For numeric types, this uses little-endian byte order (via from_le_bytes).
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.