expression

Tonic: return a finite server stream

For a server-streaming method, the generated trait requires an associated stream type. Each item is either a message or a status error. The demo limits the number of items to three and consumes the stream directly in process; it does not exercise tonic::Streaming wire decoding. Run in the accompanying generated-bindings Cargo project. Expected stdout (not executed): 1 2 Common mistake: Collecting an unbounded stream into a Vec without an explicit budget.

Formal Syntax & Structure

rust syntaxrust
type ListItemsStream = Pin<Box<dyn Stream<Item = Result<ItemReply, Status>> + Send>>;

Working Example

Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() {    use bm_tonic_inventory::{InventoryDemo, pb::{ListItemsRequest, inventory_server::Inventory}};    use tonic::Request;    use tokio_stream::StreamExt;    let response = InventoryDemo.list_items(Request::new(ListItemsRequest { limit: 2 })).await.unwrap();    let mut stream = response.into_inner();    while let Some(result) = stream.next().await {        println!("{}", result.unwrap().id);    }}
Rust Reference