[−][src]Crate endiannezz
This crate provides the ability to encode and decode all primitive types into different endianness
How it works?
Crate automatically implements Primitive
trait for each primitive type.
This allows to write abstractions and call the appropriate method depending on
the byte order that you passed to the function template. Endian
it's something
like a proxy to do it.
Macros create implementations for I/O endianness:
NativeEndian
, LittleEndian
and BigEndian
All these types are enums, which means that you cannot create them, only pass to the template.
Now it's possible to have traits that expand Read
and Write
with new methods.
Example
use std::io::Result; use endiannezz::{BigEndian, EndianReader, EndianWriter, LittleEndian, NativeEndian}; fn main() -> Result<()> { let mut vec = Vec::new(); vec.try_write::<LittleEndian, i32>(1)?; vec.try_write::<BigEndian, _>(2)?; vec.try_write::<NativeEndian, _>(3_u16)?; let mut slice = vec.as_slice(); slice.try_read::<LittleEndian, i32>()?; let _num32: i32 = slice.try_read::<BigEndian, _>()?; let _num16: u16 = slice.try_read::<NativeEndian, _>()?; Ok(()) }
You can also use this syntax:
use endiannezz::{Endian, BigEndian, LittleEndian}; fn main() { let mut vec = Vec::new(); BigEndian::write(1, &mut vec).unwrap(); LittleEndian::write::<u16, _>(2, &mut vec).unwrap(); assert_eq!(vec.as_slice(), &[0, 0, 0, 1, 2, 0]) }
Enums
BigEndian | |
LittleEndian | |
NativeEndian |
Traits
Endian | Proxy for reading and writing primitive types |
EndianReader | Allows to read primitive types with differents representation of bytes |
EndianWriter | Allows to write primitive types with differents representation of bytes |
Primitive | This trait is implemented for all primitive types that exist in rust, and allows to read types from bytes or write them into bytes |