https://github.com/andylokandy/byte
A low-level, zero-copy, panic-free, binary serializer and deserializer. (parser and encoder)
https://github.com/andylokandy/byte
byte no-std parse rust
Last synced: 10 months ago
JSON representation
A low-level, zero-copy, panic-free, binary serializer and deserializer. (parser and encoder)
- Host: GitHub
- URL: https://github.com/andylokandy/byte
- Owner: andylokandy
- License: mit
- Created: 2017-06-13T17:56:17.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2024-02-10T23:28:31.000Z (over 2 years ago)
- Last Synced: 2024-12-09T16:45:08.211Z (over 1 year ago)
- Topics: byte, no-std, parse, rust
- Language: Rust
- Homepage:
- Size: 58.6 KB
- Stars: 42
- Watchers: 3
- Forks: 7
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# `Byte`
[](https://travis-ci.org/andylokandy/byte)
[](https://crates.io/crates/byte)
[](https://docs.rs/byte)
A low-level, zero-copy and panic-free binary serializer and deserializer.
### [**Documentation**](https://docs.rs/byte)
## Usage
Add the following to your `Cargo.toml`:
```toml
[dependencies]
byte = "0.2"
```
`Byte` is a `no_std` library; it can be used in any `#![no_std]` situation or crate.
# Overview
`Byte` is designed to encode or decode binary data in a fast and low-level way.
A classical use case is I2C communication en/decoding.
`Byte` provides two core traits `TryRead` and `TryWrite`.
Types that implement these traits can be serialized into or deserialized from byte slices.
The library is meant to be simple, and it will always be.
# Example
```rust
use byte::*;
let bytes: &[u8] = &[0xde, 0xad, 0xbe, 0xef];
let offset = &mut 0;
let num = bytes.read_with::(offset, BE).unwrap();
assert_eq!(num, 0xdeadbeef);
assert_eq!(*offset, 4);
```
```rust
use byte::*;
use byte::ctx::{Str, NULL};
let bytes: &[u8] = b"hello, world!\0dump";
let offset = &mut 0;
let str = bytes.read_with::<&str>(offset, Str::Delimiter(NULL)).unwrap();
assert_eq!(str, "hello, world!");
assert_eq!(*offset, 14);
```
## Contribution
All kinds of contribution are welcomed.
- **Issues.** 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](LICENSE-MIT) or http://opensource.org/licenses/MIT)