Skip to main content

pi_async_runtime_core/
wait.rs

1//! 普通等待与计时能力组合;不要求具体执行器(executor)。
2use 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
14/// 一次交值的带超时等待,正常返回原值,原等待错误保留为WaitTimeoutError::Wait。
15/// 超时返回Elapsed,计时设施失败返回Timer;关闭后未取得接受许可的交值按原关闭规则失败并返还输入值。
16/// 超时或释放(drop)不回退此前已接受的并发交值;发送成功不承诺等待方已取得该值。
17pub struct WaitOneTimeout<A: TimerAccess, T> {
18    inner: TimeoutOn<A, WaitOne<T>>,
19}
20/// 并发竞争一次交值的带超时等待,只接收原规则选定的一个值。
21/// 原等待失败、超时、计时失败分别为Wait/Elapsed/Timer;其它发送者(senders)沿原竞争及关闭规则处理。
22/// 超时或释放(drop)不撤销此前已接受的交值;未取得接受许可的后续交值保留原值和实际拒绝原因。
23pub struct WaitAnyTimeout<A: TimerAccess, T> {
24    inner: TimeoutOn<A, WaitAny<T>>,
25}
26/// 全部交值的带超时等待,正常返回按登记顺序排列的完整逐项结果向量(vector)。
27/// 发送者(sender)释放造成的原WaitError保留在对应项;超时或计时失败返回TimeoutError,不返回部分向量。
28/// 超时或释放(drop)结束内部等待;外部句柄可安全释放,未取得接受许可的后续交值返还值和关闭原因。
29/// 此前已接受的并发交值不因关闭回退,仍由原等待机制承担保存与必要清理责任。
30pub 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}
51/// 只依赖原等待与RuntimeTime;不增加T的Send/Sync/'static/Unpin约束。
52pub trait RuntimeWait: RuntimeTime {
53    /// 返回一次交值等待与原单发送者;通过timeout_on组合,不新增T的Send/Sync/'static/Unpin约束。
54    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    /// 返回并发竞争一次交值的等待与可克隆发送者;保留原胜者及关闭错误语义。
67    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    /// 返回全部交值等待与原登记发送者;正常返回完整有序向量,超时不交部分结果。
80    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");