Path: blob/main/crates/test-programs/src/bin/async_closed_streams.rs
1693 views
mod bindings {1wit_bindgen::generate!({2path: "../misc/component-async-tests/wit",3world: "closed-streams",4async: true,5});67use super::Component;8export!(Component);9}1011use {12bindings::exports::local::local::closed::Guest,13std::mem,14wit_bindgen::{FutureReader, StreamReader, StreamResult},15};1617struct Component;1819impl Guest for Component {20async fn read_stream(mut rx: StreamReader<u8>, expected: Vec<u8>) {21let mut buffer = Vec::with_capacity(expected.len());22loop {23let (result, buf) = rx.read(mem::replace(&mut buffer, Vec::new())).await;24buffer = buf;25if !matches!(result, StreamResult::Complete(_)) {26break;27}28}29assert_eq!(buffer, expected);30}3132async fn read_future(rx: FutureReader<u8>, expected: u8, _rx_ignored: FutureReader<u8>) {33assert_eq!(rx.await, expected);34}3536async fn read_future_post_return(37rx: FutureReader<u8>,38expected: u8,39_rx_ignored: FutureReader<u8>,40) {41wit_bindgen::spawn(async move {42assert_eq!(rx.await, expected);43});44}45}4647// Unused function; required since this file is built as a `bin`:48fn main() {}495051