expression

Tonic: wrap a client-side stream of messages

A stream of request messages can be wrapped in a Request for a generated client-streaming or bidirectional call. This local example uses the IntoStreamingRequest trait and consumes the original stream; the inventory schema in this batch does not declare such a method, so no client call is made. Run in the accompanying generated-bindings Cargo project. Expected stdout (not executed): 3 4 Common mistake: Assuming an arbitrary unary RPC accepts a stream of requests.

Formal Syntax & Structure

rust syntaxrust
Request::new(stream); IntoStreamingRequest::into_streaming_request(request)

Working Example

Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() {    use bm_tonic_inventory::pb::GetItemRequest;    use tonic::{Request, IntoStreamingRequest};    use tokio_stream::{self as stream, StreamExt};    let input = stream::iter([GetItemRequest { id: 3 }, GetItemRequest { id: 4 }]);    let mut messages = Request::new(input).into_streaming_request().into_inner();    while let Some(message) = messages.next().await { println!("{}", message.id); }}
Rust Reference