The main goal of this crate is to simplify serialization of type's and structures to bytes.
Why?
If one works with binary formats/protocols a lot of time is spent implementing decoding and encoding types and structures of the format/protocol in order to further process the contained data.
For decoding parsers generators like nom are very helpful and easy the implmentation. This create tries to provide a lightweight encoding/output conbinator by just introducing 2 new traits which in turn then can make use of the iterator facilites to crate the desired output chain of bytes.
Why this extra step with the trait's
-
By introducing such a trait complex (compsites) structures in a lot of cases can be implemented by just encoding and chaining the childs in order
-
The fileds of a type still can be used for encoding but there is no hard dependency on their order nor their actual size
e.g. a protocol field size with the encoded size of 2 Bytes (u16), still can be represented e.g. as usize withn the structures/type which save quite some converting and casting.
-
There is no need of a type to provide a specific amount of memory in order to be serialized (the serialization of a type or a type could be 100% computational)
e.g.: assume this protocol type/structure (Packet)
+-----------------+-------------------+-----------------+ | field1 (1 Byte) | reserved (7 Byte) | filed2 (8 Byte) | +-----------------+-------------------+-----------------+
internally it could be represented and implemented like this
use ByteView; use ToBytes; let field1 = 0xaau8; let field2 = 0xaabbccddeeff11u64.to_be; let p = Packet ; let mut bytes = p.to_bytes; assert_eq!; assert_eq!;
How? (Usage)
Example(s)
How to serialize integers of different endianess and size
use ByteView;
use ToBytes;
let uint16_be : u16 = 0x0A0Bu16.to_be;
let uint16_le : u16 = 0x0C0Du16.to_le;
let uint32_le : u32 = 0x01020304u32.to_le;
let uint16_be_bytes = uint16_be.to_bytes;
let uint16_le_bytes = uint16_le.to_bytes;
let uint32_le_bytes = uint32_le.to_bytes;
let mut bytes = uint16_be_bytes.chain;
assert_eq!
How to serialize a custom type which contains different endinesses and types
TBD
How to serialize a custom type which contains types which also implent the ByteView trait
TBD
Todo's
- Implement ByteView for built in integer and float types
- Implement ByteView for slice like types
- Implement derive macro for ByteView