pi_async_runtime_core/time/
composition.rs1use super::{access::TimerAccess, timeout::Timeout};
2use crate::definitions::{TimeoutError, TimerError};
3use std::{
4 future::Future,
5 pin::Pin,
6 task::{Context, Poll},
7 time::Duration,
8};
9
10#[must_use = "须await或poll才开始等待"]
12pub struct Sleep<A: TimerAccess> {
13 timeout: Option<Timeout<A>>,
14 error: Option<TimerError>,
15 done: bool,
16}
17impl<A: TimerAccess> Sleep<A> {
18 pub fn new(access: A, duration: Duration) -> Self {
22 sleep_new_inner(access, duration)
23 }
24}
25impl<A: TimerAccess> Unpin for Sleep<A> {}
26impl<A: TimerAccess> Future for Sleep<A> {
27 type Output = pi_result::RawResult<(), TimerError>;
28 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
29 sleep_poll_inner(self, cx)
30 }
31}
32
33#[must_use = "须await或poll才推进业务及计时"]
35pub struct TimeoutOn<A: TimerAccess, F: Future> {
40 future: Option<F>,
42 access: Option<A>,
43 duration: Duration,
44 timeout: Option<Timeout<A>>,
45 done: bool,
46}
47impl<A: TimerAccess, F: Future> TimeoutOn<A, F> {
48 pub fn new(access: A, future: F, duration: Duration) -> Self {
51 timeout_on_new_inner(access, future, duration)
52 }
53}
54impl<A: TimerAccess, F: Future> Future for TimeoutOn<A, F> {
55 type Output = pi_result::RawResult<F::Output, TimeoutError>;
56 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
57 timeout_on_poll_inner(self, cx)
58 }
59}
60include!("composition_inner.rs");