Byte
A low-level, zero-copy, panic-free, binary serializer and deserializer (parser and encoder)
This crate is inspired by m4b/scroll
Documentation
Usage
First, add the following to your Cargo.toml
:
[]
= "0.2"
Next, add this to your crate root:
extern crate byte;
Byte
is no_std
library; it can directly be used in a #![no_std]
situation or crate.
Overview
Byte
is mainly used to encode and decode binary data with standard or protocol,
such as network TCP packages and hardware communication packages.
So it's more similar to crate nom
but more ligthweight and specialized for operating binary in low-level or hardware programing.
Byte
delivers two core traits TryRead
and TryWrite
.
Types implement these traits can be serialize into or deserialize from byte slices.
Byte slices [u8]
derives methods read()
and write()
to serialize, deserialize and handle offset.
All functionality is kept minimum in order to work in more situations.
For example, Byte
can take byte slice from MMap to read binary file,
or take heap-allocated byte buffer from Bytes.
Example
Byte
consumes byte slice continuously. The first parameter of read
is offset,
instructing the position to begin, and it must be a mutable referece of usize,
which will be increaed by the size operation consumed.
Serializing types usually requires some context such as the endian for numbers,
in such situations, read_with
is used and we can pass context as the second parameter.
use *;
let bytes: & = &;
let offset = &mut 0;
let num = bytes..unwrap;
assert_eq!;
assert_eq!;
Byte
supports language primitives by default.
&str
(with contextStr
)&[u8]
(with contextByte
)u8
,i8
,u64
,f64
... (with contextEndian
)bool
- ...
&str
and &[u8]
have references to the byte slice so there is no copy when read
and it has the same lifetime as the byte slice.
use *;
use ;
let bytes: & = b"hello, world!\0more";
let str: &str = bytes.read_with.unwrap;
assert_eq!;
Define custom serializable type
In this example, we defined a custom type Header
, which have a varibal-length name and a bool
field.
We implement TryRead
and TryWrite
to enable this type to be serialzed and deserialized.
Byte Representation
| | Length of name (Big Endian) | Name | Enabled |
| ----- | --------------------------- | ---- | ---- | ---- | ---- | ---- | ------- |
| Byte | 0 | 5 | 'H' | 'E' | 'L' | 'L' | 'O' | 0 |
Note that the passed-in bytes
is implicitly splitted by offset and should be read at head.
And the type Result
is an alias defind in Byte
as core::result::Result<(T, size), byte::Error>
,
where the size is the number of bytes read
or write
consumed and it will be used to incread the offset.
use *;
use *;
Usage
let bytes = ;
let header: Header = bytes.read_with.unwrap;
assert_eq!;
assert_eq!;
let mut write = ;
write.write_with.unwrap;
assert_eq!;
Contribution
All kinds of contribution are welcomed.
- Issus. Feel free to open an issue when you find typos, bugs, or have any question.
- Pull requests. New collection, better implementation, more tests, more documents and typo fixes are all welcomed.
License
Licensed under MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)