rainforestt-contract

Rainforestt gRPC 服务的 Rust SDK。基于 tonic 构建。
安装
将以下依赖添加到你的 Cargo.toml:
[dependencies]
rainforestt-contract = "1.0.0"
tokio = { version = "1.0", features = ["full"] }
快速开始
use rainforestt_contract::ai::{self, ai_service_client::AiServiceClient};
use tonic::transport::Channel;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let channel = Channel::from_static("http://[::1]:50051")
.connect()
.await?;
let mut client = AiServiceClient::new(channel);
let request = tonic::Request::new(ai::YourRequest {
});
let response = client
.your_rpc_method(request)
.await?;
println!("Response: {:?}", response);
Ok(())
}
异步流示例
如果服务定义了流式 RPC,你可以这样使用:
use futures::StreamExt;
async fn stream_example() -> Result<(), Box<dyn std::error::Error>> {
let mut client =
let request = tonic::Request::new(ai::StreamRequest {
});
let mut stream = client
.streaming_rpc(request)
.await?
.into_inner();
while let Some(response) = stream.next().await {
match response {
Ok(msg) => println!("Got: {:?}", msg),
Err(e) => eprintln!("Error: {:?}", e),
}
}
Ok(())
}
特性
- 完整的 Protocol Buffers 生成的类型定义
- 异步客户端实现(基于 tokio)
- 支持单向 RPC 和流式 RPC
- tonic 的所有功能(TLS、负载均衡等)