3个不稳定版本
| 0.2.1 | 2020年4月1日 | 
|---|---|
| 0.2.0 | 2020年2月20日 | 
| 0.1.0 | 2020年2月15日 | 
#2301 in 编码
50KB
 1K  SLoC
Nimble
Rust中的异步友好,简单快速的二进制编码/解码。
二进制编码方案
此crate使用最小化的二进制编码方案。例如,考虑以下 struct
struct MyStruct {
    a: u8,
    b: u16,
}
encode() 将此序列化为大小为 Vec 的 3 (这是 u8 和 u16 大小的总和)。
同样,对于具有动态大小的类型(Vec、String等),encode() 在编码值前添加编码值的长度作为 u64。
用法
将 nimble 添加到您的 Cargo.toml 的 dependencies 部分
[dependencies]
nimble = { version = "0.2", features = ["derive"] }
或者,如果您处于基于 tokio 的环境中,请使用
[dependencies]
nimble = { version = "0.2", default-features = false, features = ["derive", "tokio"] }
对于编码和解码,任何类型都必须实现此crate提供的两个特质,即 Encode 和 Decode。为了方便,nimble 提供了 derive 宏(仅在启用 "derive" 功能时)以实现这些特质。
use nimble::{Encode, Decode};
#[derive(Encode, Decode)]
struct MyStruct {
    a: u8,
    b: u16,
}
现在您可以使用 encode() 和 decode() 函数来编码和解码 MyStruct 的值。除此之外,您还可以使用 MyStruct::encode_to() 函数将值直接编码到实现 AsyncWrite 的类型,并使用 MyStruct::decode_from() 函数将值直接从实现 AsyncRead 的类型解码。
注意:此crate公开的大部分函数都是
async函数,并返回Future值。因此,您需要一个执行器来驱动这些函数返回的Future。async-std和tokio是两个流行的选择。
功能
- futures:当您想使用- futures的- AsyncRead/- AsyncWrite特性实现- Encode和- Decode时,选择此功能。- 默认启用。
 
- tokio:当您想使用- tokio的- AsyncRead/- AsyncWrite特性实现- Encode和- Decode时,选择此功能。- 默认禁用。
 
- derive:启用 derive 宏以实现- Encode和- Decode特性。- 默认禁用。
 
注意:功能
futures和tokio是互斥的,即一次只能启用其中一个。如果同时启用这两个功能或都未启用,则编译将失败。
许可证
根据您的选择,许可协议为以下之一:
- Apache License, Version 2.0 (LICENSE-APACHE)
- MIT 许可证 (LICENSE-MIT)
任选其一。
贡献
除非您明确声明,否则根据 Apache-2.0 许可证定义,您有意提交的任何贡献均应双许可,如上所述,不附加任何其他条款或条件。
依赖项
~0.4–1.9MB
~34K SLoC