6 个版本
0.3.0 | 2021 年 11 月 25 日 |
---|---|
0.2.0 | 2021 年 2 月 24 日 |
0.1.3 | 2021 年 2 月 23 日 |
510 在 构建实用工具 中
1,093 每月下载量
在 7 个 crate 中使用 (通过 redgold-schema)
7KB
71 行
prost-helper
提供了两个 crate,以帮助 prost 更好地与 protobuf 一起工作
- prost-serde:帮助您使用 prost 和 protobuf 文件生成与 serde 兼容的代码。更多信息请参阅 文档。
- prost-helper:提供 prost 的宏和函数。更多信息请参阅 文档。
在 Cargo 项目中使用 prost-helper
首先,将 prost-serde
和 prost-helper
添加到您的 Cargo.toml
[dependencies]
prost-helper = "0.1"
[build-dependencies]
prost-serde = "0.1"
然后将 default_build_config.json 复制到您的项目并自定义它。更多信息请参阅 prost-serde/examples/build_config.json。然后您可以在您的 build.rs 中添加以下内容
fn main() {
let json = include_str!("path/to/your/build_config.json");
build_with_serde(json);
}
就是这样!
如果您想为 From
特性生成 Vec<u8>
的实现,或者从 Vec<u8>
到 protobuf 数据结构的 TryFrom
特性,您可以使用 prost_into_vec!
和 vec_try_into_prost!
宏。以下是一个示例
use prost::Message;
use std::convert::TryInto;
#[derive(Clone, PartialEq, Message)]
pub struct Hello {
#[prost(string, tag = "1")]
pub msg: String,
}
// implement the traits
prost_into_vec!(Hello, 32);
vec_try_into_prost!(Hello);
let hello = Hello::default();
// use `Into` to convert message to `Vec<u8>`
let data: Vec<u8> = hello.into();
// use `TryInto` to convert `Vec<u8>` back to message
let hello_result: Result<Hello, prost::DecodeError> = data.try_into();
享受 prost 的乐趣!
许可证
prost-helper
根据 MIT 许可证分发。
有关详细信息,请参阅 LICENSE。
版权 2021 Tyr Chen
依赖关系
~8–19MB
~255K SLoC