Motivation
-
The goal of this product is to provide a set of utilities that enable frictionless transitioning between a
byte stream, ex:&[u8], and an arbitrarystruct. In other words, the project provides a set oftraitsandimpl's that can be used to manuallyserializean arbitrarystructinto abyte streamas well as todeserializea givenbyte streaminto it originalstruct. -
In addition to be able to custom serialize an arbitrary
struct, you can leverage an included#[derive(..)]proc_macroand a number usefullmacro attributesto create automatically generated serialize and deserializetraitimplementation that covers most of typical usecases.
Benefit case
-
If you work with network streams which deliver data in
byte streamformat and a well defined sequence you can use this product to quickly and efficently map yourbyte streaminto astructof your choice and focus on the business logic instead of parsing and mapping. -
if you have two or more systems which need to communicate with each other, either over a network socket or a shared memory, but at a very
low latency/cpu cost, this product is a good choice for you.
Structure
- The project contains three craits
-
byteserde- byteserde/Cargo.toml-
contains ByteSerializeStack, ByteSerializeHeap & ByteDeserialize
<T>traitsand helperstruct's that make it easy to manually create custombyte streamserailizer and deserializer -
ByteSerializerStack
<CAP>- provides ultra fast speed by serializing into a pre allocatedbyte array[u8; CAP]onstack, hence the name, it is very fast but at the cost of you needing to specify the size of the LARGESTstructyou will attempt to serialize. If you reach the boundary of this preallocated byte array, your serialization will fail. This utility provides a reset features, which moves the internal counter to the begining, and allows you to recycle the buffer for multiple purpoces. -
ByteSerializerHeap - provides a fast enough for most speed by serializing into a
byte vectorVec<u8>, hence the name. This utility trades some performance in return for not having to worry about knowing the LARGESTstructsize in advance. -
ByteDeserializer - takes a
byte stream&[u8]irrespctive of heap vs stack allocation and turns it into astruct
-
-
byteserde_derive- byteserde_derive/Cargo.toml- contains procedural macro that generaters implementation of these traits on regular & tuple rust structure.
- This crate supports three attributes:
-
#[byteserde( endian = "le" )]- this will cause entirestructormemberto serialize in desired endian. Valid options arele,be,ne -
#[byteserde( replace( ... ))]- this only affectsserializationof thememberwhose value will be ignored and value of...expresion will instead be serialized used . Ex: This is usefull when one of the fields contains length of the packet but you don't know its value until the instance is created. Using this attribute you can create an expression which will be evaluated during serialization. See: Examples for more details. -
#[byteserde( deplete( ... ) )]- this only affectsdeserializationof thememberby limiting the number of bytes the member is allowed to read from the stream. Must evaluate tousize. Ex: This is usefull when part of thebyte streamcontains infomation about numbers of bytes representing one of following members. See: Examples for more details.
-
- NOTE: that Union, Enum, and Unit structure are not not currently supported
-
byteserde_tyeps- byteserde_types/Cargo.toml- contains optional ascii string related types, which are typically usefull when dealing with fixed length strings when parsing a
byte stream, see examples section for more details.
- contains optional ascii string related types, which are typically usefull when dealing with fixed length strings when parsing a
-
Examples & Overview
- Please refer to this document for a number of helpfull examples and feature review.