1use pi_async_task::definitions::BuildErrorKind;
3use pi_async_task_queue::{
4 external::ExternalPushErrorKind, idle::IdleQueueError, local::QueuePushErrorKind,
5 timer::TimerQueueError,
6};
7use pi_result::prelude::*;
8use std::num::NonZeroU64;
9use std::time::Duration;
10
11#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
13pub struct RuntimeId(NonZeroU64);
15
16impl RuntimeId {
17 pub fn try_new() -> pi_result::Result<Self> {
19 runtime_id_new_inner()
20 }
21 pub fn get(self) -> u64 {
23 self.0.get()
24 }
25}
26
27#[derive(Clone, Copy, Debug, Eq, PartialEq)]
29pub enum RuntimeStatus {
30 Open,
32 Closing,
34 Closed,
37}
38#[derive(Clone, Copy, Debug, Eq, PartialEq)]
40pub enum IdleState {
41 Unknown,
42 Idle,
43 NotIdle,
44}
45#[derive(Clone, Copy, Debug, Eq, PartialEq)]
47pub enum CloseState {
48 NotRequested,
49 Requested,
50 Completed,
51}
52#[derive(Clone, Copy, Debug, Eq, PartialEq)]
54pub enum CloseRequestStatus {
55 Requested,
56 AlreadyRequested,
57 AlreadyClosed,
58}
59
60#[derive(Debug, ThisError)]
62pub enum CurrentRuntimeAccessError {
63 #[error("没有当前运行时(runtime)环境")]
64 NoCurrentRuntime,
65 #[error("当前运行时(runtime)的公开访问类型不匹配")]
67 TypeMismatch,
68 #[error("当前范围已选择另一运行时(runtime)")]
70 DifferentRuntime,
71 #[error("线程局部存储(TLS)不可访问")]
73 ContextUnavailable,
74}
75
76#[derive(Debug, ThisError)]
78pub enum LocalRuntimeAccessError {
79 #[error("没有当前有效的本地运行时(runtime)")]
80 NoLocalRuntime,
81 #[error("本地访问类型(type)不匹配")]
83 TypeMismatch,
84 #[error("本地权限已经撤销")]
85 AccessRevoked,
86 #[error("线程局部存储(TLS)不可访问")]
88 ContextUnavailable,
89}
90
91pub enum CreateErrorKind {
93 Task(BuildErrorKind),
94 LocalContext(LocalRuntimeAccessError),
95 Timer(TimerError),
96}
97
98pub enum SubmitErrorKind {
100 WrongRuntime,
102 LocalSubmissionRequired,
104 OrdinarySubmissionRequired,
106 WrongOwner,
108 LocalContext(LocalRuntimeAccessError),
109 External(ExternalPushErrorKind),
110 LocalQueue(QueuePushErrorKind),
111 Idle(IdleQueueError),
112 Timer(TimerQueueError),
113}
114
115pub enum TimerError {
117 InvalidDuration {
119 value: Duration,
120 min_positive: Duration,
121 max: Option<Duration>,
122 },
123 Clock(pi_result::Error),
124 DeadlineOutOfRange(pi_result::Error),
125 RegistrationPreparation(pi_result::Error),
126 Queue(TimerQueueError),
127 Cancelled,
129 RegistrationRemoved,
131 ServiceClosed,
133}
134
135pub enum TimeoutError {
137 Elapsed,
138 Timer(TimerError),
139}
140pub enum WaitTimeoutError {
142 Wait(pi_async_wait::WaitError),
143 Elapsed,
144 Timer(TimerError),
145}
146
147macro_rules! error_format {
150 ($ty:ty, $debug:ident, $display:ident) => {
151 impl std::fmt::Debug for $ty {
152 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
153 $debug(self, f)
154 }
155 }
156 impl std::fmt::Display for $ty {
157 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
158 $display(self, f)
159 }
160 }
161 };
162}
163error_format!(
164 CreateErrorKind,
165 create_error_debug_inner,
166 create_error_display_inner
167);
168error_format!(
169 SubmitErrorKind,
170 submit_error_debug_inner,
171 submit_error_display_inner
172);
173error_format!(
174 TimerError,
175 timer_error_debug_inner,
176 timer_error_display_inner
177);
178error_format!(
179 TimeoutError,
180 timeout_error_debug_inner,
181 timeout_error_display_inner
182);
183error_format!(
184 WaitTimeoutError,
185 wait_timeout_error_debug_inner,
186 wait_timeout_error_display_inner
187);
188
189impl std::error::Error for TimerError {
190 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
191 timer_error_source_inner(self)
192 }
193}
194impl TimerError {
195 pub fn into_report(self) -> pi_result::Error {
197 timer_error_report_inner(self)
198 }
199}
200include!("definitions_inner.rs");