ie.*_*ie. 4 rust proto tonic grpc prost
我正在尝试使用 crates prost 和 tonic 在 Rust 中构建一个简单的 gRPC 客户端。我的 proto 定义非常简单,但我突然坚持使用从其他 proto 导入的消息。
// file src/protos/types.proto
syntax = "proto3";
package Types;
message Message1 {
uint32 value1 = 1;
bytes value2 = 2;
}
message Message2 {
uint32 value1 = 1;
uint32 value2 = 2;
uint32 value3 = 3;
uint32 value4 = 4;
}
Run Code Online (Sandbox Code Playgroud)
// file src/protos/service.proto
syntax = "proto3";
import "types.proto";
package Service;
service Worker {
rpc Do (Request) returns (Reply);
}
message Request {
Types.Message1 message1 = 1;
Types.Message2 message2 = 2;
}
message Reply {
bool success = 1;
}
Run Code Online (Sandbox Code Playgroud)
我build.rs的很简单:
fn main() -> Result<(), Box<dyn std::error::Error>> {
tonic_build::compile_protos("src/protos/types.proto")?;
tonic_build::compile_protos("src/protos/service.proto")?;
Ok(())
}
Run Code Online (Sandbox Code Playgroud)
当我在 protos 中包含 protos 时,问题就开始了main.rs:
pub mod service {
tonic::include_proto!("types");
tonic::include_proto!("service");
}
Run Code Online (Sandbox Code Playgroud)
编译失败并出现以下错误:
--> D:\temp\rust-proto\target\debug\build\rust-proto-11c38604fbc7ce30\out/service.rs:4:48
|
4 | pub message1: ::std::option::Option<super::types::Message1>,
| ^^^^^ maybe a missing crate `types`?
Run Code Online (Sandbox Code Playgroud)
这里可能有什么问题?!我将我的 Playground 项目上传到github,以防它可能有用。
我对 tonic 了解不多,但是您必须将包含的内容放在与其 proto 包相对应的模块中,例如:
pub mod types {
tonic::include_proto!("types");
}
pub mod service {
tonic::include_proto!("service");
}
fn main() {
let msg = types::Message1::default();
println!("Hello, world! {:?}", msg);
}
Run Code Online (Sandbox Code Playgroud)
这编译正确。
顺便说一句,您可以在此处的设置中的以下位置检查生成的 Rust 代码:
D:\temp\rust-proto\target\debug\build\rust-proto-11c38604fbc7ce30\out
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1324 次 |
| 最近记录: |