Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/crates/test-programs/src/bin/async_closed_streams.rs
1693 views
1
mod bindings {
2
wit_bindgen::generate!({
3
path: "../misc/component-async-tests/wit",
4
world: "closed-streams",
5
async: true,
6
});
7
8
use super::Component;
9
export!(Component);
10
}
11
12
use {
13
bindings::exports::local::local::closed::Guest,
14
std::mem,
15
wit_bindgen::{FutureReader, StreamReader, StreamResult},
16
};
17
18
struct Component;
19
20
impl Guest for Component {
21
async fn read_stream(mut rx: StreamReader<u8>, expected: Vec<u8>) {
22
let mut buffer = Vec::with_capacity(expected.len());
23
loop {
24
let (result, buf) = rx.read(mem::replace(&mut buffer, Vec::new())).await;
25
buffer = buf;
26
if !matches!(result, StreamResult::Complete(_)) {
27
break;
28
}
29
}
30
assert_eq!(buffer, expected);
31
}
32
33
async fn read_future(rx: FutureReader<u8>, expected: u8, _rx_ignored: FutureReader<u8>) {
34
assert_eq!(rx.await, expected);
35
}
36
37
async fn read_future_post_return(
38
rx: FutureReader<u8>,
39
expected: u8,
40
_rx_ignored: FutureReader<u8>,
41
) {
42
wit_bindgen::spawn(async move {
43
assert_eq!(rx.await, expected);
44
});
45
}
46
}
47
48
// Unused function; required since this file is built as a `bin`:
49
fn main() {}
50
51