8 releases (5 breaking)
| 0.6.2 | Jan 14, 2024 |
|---|---|
| 0.6.1 | Jan 11, 2024 |
| 0.6.0 | Dec 24, 2023 |
| 0.5.0 | Nov 13, 2023 |
| 0.1.1 | May 19, 2023 |
#1008 in Encoding
208 downloads per month
Used in 13 crates
(9 directly)
66KB
1K
SLoC
Motivation
-
The motivation for this product is two fold:
-
To be able to map a
byte slice &[u8], typically acquired from a network, into a ruststructdatamodel by simply usingderive macroannotations and attributes auto-generate necessary code. -
This comes very handy when the
byte sliceis not serialized using one of the existing and widely available protocols. Ex: An application specific C-Struct. -
To be the
fastestbyte stream serializer/deserializer on the market for latency sensitive use cases. Benchmark results below show a performance summary of serializing & deserializing a Reference Struct using different frameworks available:byteserde-~15nsread/writebincode-~15nsread /~100nswritermp-serde-~215nsread/writeserde_json-~600nsread/write - understandably slow due to strings usage- this document contains benchmark details.
-
When to use and when to avoid this framework
Use
-
If you work with network protocols that deliver data in a
byte streamformat that is not matching one of the widely available standards, ex:bincode,protobuf. Use this product to efficiently map yourbyte streaminto a ruststruct. -
You have a latency sensitive usecase. Note that this protocol does not add any schema information during serialization process and hence is equivalent of dumping the memory layout of the struct without padding
-
Example of protocols that are a perfect fit for this framework.
- Ouch5
- SoupBinTCP
- Boe US Equities
- .. etc
Avoid
- If the
byte streamis serialized or deserialized using a wideley available standard avoid this framework and instead the that respective standard to work with thebyte stream
The project contains three craits
byteserde_derive@crates.io - byteserde_derive/Cargo.toml
- contains derive macros that generates byteserde@crates.io traits
-
#[derive(ByteSerializeStack)]- generates ByteSerializeStack trait -
#[derive(ByteSerializeHeap)]- generates ByteSerializeHeap trait -
#[derive(ByteDeserializeSlice)]- generates ByteDeserializeSlice<T>trait -
#[derive(ByteSerializedSizeOf)]- generates ByteSerializedSizeOf trait - this trait provides anassociatedmethodbyte_size()which gives you astructmemory size in bytes without alignment. However it does not support types which heap allocate, ex: Vectors, Strings, or their derivations. -
#[derive(ByteSerializedLenOf)]- generates ByteSerializedLenOf trait - this trait provides aninstancemethodbyte_len(&self)which gives you memory size in bytes without alignment of specific instance. It exists specifically to deal with types thatByteSerializedSizeOf traitdoes not support
-
- For more examples follow here
- NOTE: that Union and Unit structure are not supported, but it might change in the future.
byteserde@crates.io - byteserde/Cargo.toml
- Highlights
-
ByteSerializerStack
<CAP>- provides ultra fast serializer 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 multiple times.- works for
structs that implement ByteSerializeStack trait
- works for
-
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.- works for
structs that implement ByteSerializeHeap trait
- works for
-
ByteDeserializerSlice - takes a
byte stream&[u8]irrespctive of heap vs stack allocation and turns it into astruct- works for
structs that implement ByteDeserializeSlice<T>trait
- works for
-
byteserde_types@crates.io - byteserde_types/Cargo.toml
- contains optional ascii string related types and macros, which are typically usefull when dealing with fixed length strings while parsing a
byte stream, follow example section for more details.
Examples & Overview
- Please refer to this document for a number of comprehensive examples and features overview.
Dependencies
~210KB