use crate::{
definitions::{TimeoutError, WaitTimeoutError},
time::{RuntimeTime, TimeoutOn, TimerAccess},
};
use pi_async_wait::{AllSender, AnySender, OneSender, WaitAll, WaitAny, WaitError, WaitOne};
use std::{
future::Future,
pin::Pin,
task::{Context, Poll},
time::Duration,
};
pub struct WaitOneTimeout<A: TimerAccess, T> {
inner: TimeoutOn<A, WaitOne<T>>,
}
pub struct WaitAnyTimeout<A: TimerAccess, T> {
inner: TimeoutOn<A, WaitAny<T>>,
}
pub struct WaitAllTimeout<A: TimerAccess, T> {
inner: TimeoutOn<A, WaitAll<T>>,
}
impl<A: TimerAccess, T> Future for WaitOneTimeout<A, T> {
type Output = pi_result::RawResult<T, WaitTimeoutError>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
wait_one_timeout_poll_inner(self, cx)
}
}
impl<A: TimerAccess, T> Future for WaitAnyTimeout<A, T> {
type Output = pi_result::RawResult<T, WaitTimeoutError>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
wait_any_timeout_poll_inner(self, cx)
}
}
impl<A: TimerAccess, T> Future for WaitAllTimeout<A, T> {
type Output = pi_result::RawResult<Vec<pi_result::RawResult<T, WaitError>>, TimeoutError>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
wait_all_timeout_poll_inner(self, cx)
}
}
pub trait RuntimeWait: RuntimeTime {
fn wait_one_timeout<T>(
&self,
duration: Duration,
) -> (WaitOneTimeout<Self::Timer, T>, OneSender<T>) {
let (wait, sender) = pi_async_wait::wait_one();
(
WaitOneTimeout {
inner: self.timeout_on(wait, duration),
},
sender,
)
}
fn wait_any_timeout<T>(
&self,
duration: Duration,
) -> (WaitAnyTimeout<Self::Timer, T>, AnySender<T>) {
let (wait, sender) = pi_async_wait::wait_any();
(
WaitAnyTimeout {
inner: self.timeout_on(wait, duration),
},
sender,
)
}
fn wait_all_timeout<T>(
&self,
duration: Duration,
) -> (WaitAllTimeout<Self::Timer, T>, AllSender<T>) {
let (wait, sender) = pi_async_wait::wait_all();
(
WaitAllTimeout {
inner: self.timeout_on(wait, duration),
},
sender,
)
}
}
include!("wait_inner.rs");