Skip to main content

pi_async_runtime_core/context/
access.rs

1use super::record;
2use crate::{
3    block_on::RuntimeBlockOn, control::RuntimeControl, definitions::CurrentRuntimeAccessError,
4    dispatch::*, time::RuntimeTime, wait::RuntimeWait,
5};
6
7/// 公共访问的目标与本地推进权限不同;显式enter只选择原服务。
8pub trait RuntimeContext: TaskDispatch + RuntimeTime {
9    type Handle: Runtime<Notifier = Self::Notifier, Timer = Self::Timer, Handle = Self::Handle>
10        + Clone
11        + Send
12        + Sync
13        + 'static;
14    /// 取得原服务的共享句柄(shared handle);Clone不复制服务,普通释放不自动关闭。
15    fn handle(&self) -> Self::Handle;
16    /// 取得并克隆(Clone)当前所选原服务的共享句柄(shared handle),不产生本地或推进权限。
17    /// 无当前目标、句柄类型(type)不匹配或线程局部存储(TLS)不可访问时返回对应错误。
18    /// 有效公共选择在原服务关闭后仍可读取关闭事实,不会重新开放服务。
19    fn try_current() -> pi_result::RawResult<Self::Handle, CurrentRuntimeAccessError> {
20        record::try_current::<Self::Handle>()
21    }
22    /// 同步借用不能逃逸或跨await;公共环境关闭后仍可读原关闭事实。
23    fn try_with_current<R>(
24        action: impl FnOnce(&Self::Handle) -> R,
25    ) -> pi_result::RawResult<R, CurrentRuntimeAccessError> {
26        record::try_with_current::<Self::Handle, R>(action)
27    }
28    /// 同目标允许嵌套,不授予本地权限;闭包返回Future不会延长本次选择范围。
29    fn enter<R>(
30        &self,
31        action: impl FnOnce() -> R,
32    ) -> pi_result::RawResult<R, CurrentRuntimeAccessError> {
33        record::with_selected(self.handle(), action)
34    }
35}
36
37/// 运行时(runtime)的公共能力聚合,不包含执行器(executor)的run/run_once。
38pub trait Runtime:
39    TaskDispatch
40    + LocalTaskDispatch
41    + IdleDispatch
42    + TimedTaskDispatch
43    + RuntimeWait
44    + RuntimeControl
45    + RuntimeContext
46    + RuntimeBlockOn
47{
48}
49impl<T> Runtime for T where
50    T: TaskDispatch
51        + LocalTaskDispatch
52        + IdleDispatch
53        + TimedTaskDispatch
54        + RuntimeWait
55        + RuntimeControl
56        + RuntimeContext
57        + RuntimeBlockOn
58{
59}