Crates.io | nimble-derive |
lib.rs | nimble-derive |
version | 0.2.0 |
source | src |
created_at | 2020-02-15 10:51:13.279531 |
updated_at | 2020-02-20 10:04:11.532269 |
description | Async friendly, simple and fast binary encoding/decoding |
homepage | https://github.com/devashishdxt/nimble |
repository | https://github.com/devashishdxt/nimble |
max_upload_size | |
id | 209422 |
size | 24,404 |
Async friendly, simple and fast binary encoding/decoding in Rust.
This crate uses a minimal binary encoding scheme. For example, consider the following struct
:
struct MyStruct {
a: u8,
b: u16,
}
encode()
will serialize this into Vec
of size 3
(which is the sum of sizes of u8
and u16
).
Similarly, for types which can have dynamic size (Vec
, String
, etc.), encode()
prepends the size of encoded value
as u64
.
Add nimble
in your Cargo.toml
's dependencies
section:
[dependencies]
nimble = { version = "0.2", features = ["derive"] }
Or, if you are in an environment based on tokio
, use:
[dependencies]
nimble = { version = "0.2", default-features = false, features = ["derive", "tokio"] }
For encoding and decoding, any type must implement two traits provided by this crate, i.e., Encode
and Decode
. For
convenience, nimble
provides derive
macros (only when "derive"
feature is enabled) to implement these traits.
use nimble::{Encode, Decode};
#[derive(Encode, Decode)]
struct MyStruct {
a: u8,
b: u16,
}
Now you can use encode()
and decode()
functions to encode and decode values of MyStruct
. In addition to this, you
can also use MyStruct::encode_to()
function to encode values directly to a type implementing AsyncWrite
and
MyStruct::decode_from()
function to decode values directly from a type implementing AsyncRead
.
Note: Most of the functions exposed by this crate are
async
functions and returnsFuture
values. So, you'll need an executor to drive theFuture
returned from these functions.async-std
andtokio
are two popular options.
futures
: Select this feature when you want to implement Encode
and Decode
using futures
'
AsyncRead
/AsyncWrite
traits.
tokio
: Select this feature when you want to implement Encode
and Decode
using tokio
's AsyncRead
/AsyncWrite
traits.
derive
: Enables derive macros for implementing Encode
and Decode
traits.
Note: Features
futures
andtokio
are mutually exclusive, i.e., only one of them can be enabled at a time. Compilation will fail if either both of them are enabled or none of them are enabled.
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.