1 mod bindings {
2     wit_bindgen::generate!({
3         path: "../misc/component-async-tests/wit",
4         world: "unit-stream-callee",
5     });
6 
7     use super::Component;
8     export!(Component);
9 }
10 
11 use {
12     bindings::{exports::local::local::unit_stream::Guest, wit_stream},
13     wit_bindgen::StreamReader,
14 };
15 
16 struct Component;
17 
18 impl Guest for Component {
run(count: u32) -> StreamReader<()>19     async fn run(count: u32) -> StreamReader<()> {
20         let (mut tx, rx) = wit_stream::new();
21 
22         wit_bindgen::spawn(async move {
23             let mut sent = 0;
24             let mut chunk_size = 1;
25             while sent < count {
26                 let n = (count - sent).min(chunk_size);
27                 let remaining = tx.write_all(vec![(); usize::try_from(n).unwrap()]).await;
28                 assert!(remaining.is_empty());
29                 sent += n;
30                 chunk_size *= 2;
31             }
32         });
33 
34         rx
35     }
36 }
37 
38 // Unused function; required since this file is built as a `bin`:
main()39 fn main() {}
40