Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/crates/test-programs/src/bin/async_backpressure_caller.rs
1693 views
1
mod bindings {
2
wit_bindgen::generate!({
3
path: "../misc/component-async-tests/wit",
4
world: "backpressure-caller",
5
});
6
7
use super::Component;
8
export!(Component);
9
}
10
11
use {
12
bindings::{
13
exports::local::local::run::Guest,
14
local::local::{backpressure, run},
15
},
16
futures::future,
17
std::{
18
future::Future,
19
pin::Pin,
20
task::{Context, Poll},
21
},
22
};
23
24
struct Component;
25
26
impl Guest for Component {
27
async fn run() {
28
backpressure::set_backpressure(true);
29
30
let mut a = Some(Box::pin(run::run()));
31
let mut b = Some(Box::pin(run::run()));
32
let mut c = Some(Box::pin(run::run()));
33
34
let mut backpressure_is_set = true;
35
future::poll_fn(move |cx| {
36
let a_ready = is_ready(cx, &mut a);
37
let b_ready = is_ready(cx, &mut b);
38
let c_ready = is_ready(cx, &mut c);
39
40
if backpressure_is_set {
41
assert!(!a_ready);
42
assert!(!b_ready);
43
assert!(!c_ready);
44
45
backpressure::set_backpressure(false);
46
backpressure_is_set = false;
47
48
Poll::Pending
49
} else if a_ready && b_ready && c_ready {
50
Poll::Ready(())
51
} else {
52
Poll::Pending
53
}
54
})
55
.await
56
}
57
}
58
59
fn is_ready(cx: &mut Context, fut: &mut Option<Pin<Box<impl Future<Output = ()>>>>) -> bool {
60
if let Some(v) = fut.as_mut() {
61
if v.as_mut().poll(cx).is_ready() {
62
*fut = None;
63
true
64
} else {
65
false
66
}
67
} else {
68
true
69
}
70
}
71
72
// Unused function; required since this file is built as a `bin`:
73
fn main() {}
74
75