pi_async_runtime_core/
wait.rs1use crate::{
3 definitions::{TimeoutError, WaitTimeoutError},
4 time::{RuntimeTime, TimeoutOn, TimerAccess},
5};
6use pi_async_wait::{AllSender, AnySender, OneSender, WaitAll, WaitAny, WaitError, WaitOne};
7use std::{
8 future::Future,
9 pin::Pin,
10 task::{Context, Poll},
11 time::Duration,
12};
13
14pub struct WaitOneTimeout<A: TimerAccess, T> {
18 inner: TimeoutOn<A, WaitOne<T>>,
19}
20pub struct WaitAnyTimeout<A: TimerAccess, T> {
24 inner: TimeoutOn<A, WaitAny<T>>,
25}
26pub struct WaitAllTimeout<A: TimerAccess, T> {
31 inner: TimeoutOn<A, WaitAll<T>>,
32}
33impl<A: TimerAccess, T> Future for WaitOneTimeout<A, T> {
34 type Output = pi_result::RawResult<T, WaitTimeoutError>;
35 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
36 wait_one_timeout_poll_inner(self, cx)
37 }
38}
39impl<A: TimerAccess, T> Future for WaitAnyTimeout<A, T> {
40 type Output = pi_result::RawResult<T, WaitTimeoutError>;
41 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
42 wait_any_timeout_poll_inner(self, cx)
43 }
44}
45impl<A: TimerAccess, T> Future for WaitAllTimeout<A, T> {
46 type Output = pi_result::RawResult<Vec<pi_result::RawResult<T, WaitError>>, TimeoutError>;
47 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
48 wait_all_timeout_poll_inner(self, cx)
49 }
50}
51pub trait RuntimeWait: RuntimeTime {
53 fn wait_one_timeout<T>(
55 &self,
56 duration: Duration,
57 ) -> (WaitOneTimeout<Self::Timer, T>, OneSender<T>) {
58 let (wait, sender) = pi_async_wait::wait_one();
59 (
60 WaitOneTimeout {
61 inner: self.timeout_on(wait, duration),
62 },
63 sender,
64 )
65 }
66 fn wait_any_timeout<T>(
68 &self,
69 duration: Duration,
70 ) -> (WaitAnyTimeout<Self::Timer, T>, AnySender<T>) {
71 let (wait, sender) = pi_async_wait::wait_any();
72 (
73 WaitAnyTimeout {
74 inner: self.timeout_on(wait, duration),
75 },
76 sender,
77 )
78 }
79 fn wait_all_timeout<T>(
81 &self,
82 duration: Duration,
83 ) -> (WaitAllTimeout<Self::Timer, T>, AllSender<T>) {
84 let (wait, sender) = pi_async_wait::wait_all();
85 (
86 WaitAllTimeout {
87 inner: self.timeout_on(wait, duration),
88 },
89 sender,
90 )
91 }
92}
93include!("wait_inner.rs");