Path: blob/main/crates/test-programs/src/bin/async_backpressure_caller.rs
1693 views
mod bindings {1wit_bindgen::generate!({2path: "../misc/component-async-tests/wit",3world: "backpressure-caller",4});56use super::Component;7export!(Component);8}910use {11bindings::{12exports::local::local::run::Guest,13local::local::{backpressure, run},14},15futures::future,16std::{17future::Future,18pin::Pin,19task::{Context, Poll},20},21};2223struct Component;2425impl Guest for Component {26async fn run() {27backpressure::set_backpressure(true);2829let mut a = Some(Box::pin(run::run()));30let mut b = Some(Box::pin(run::run()));31let mut c = Some(Box::pin(run::run()));3233let mut backpressure_is_set = true;34future::poll_fn(move |cx| {35let a_ready = is_ready(cx, &mut a);36let b_ready = is_ready(cx, &mut b);37let c_ready = is_ready(cx, &mut c);3839if backpressure_is_set {40assert!(!a_ready);41assert!(!b_ready);42assert!(!c_ready);4344backpressure::set_backpressure(false);45backpressure_is_set = false;4647Poll::Pending48} else if a_ready && b_ready && c_ready {49Poll::Ready(())50} else {51Poll::Pending52}53})54.await55}56}5758fn is_ready(cx: &mut Context, fut: &mut Option<Pin<Box<impl Future<Output = ()>>>>) -> bool {59if let Some(v) = fut.as_mut() {60if v.as_mut().poll(cx).is_ready() {61*fut = None;62true63} else {64false65}66} else {67true68}69}7071// Unused function; required since this file is built as a `bin`:72fn main() {}737475