dope_sys/engine/life/
mod.rs1mod error;
2mod step;
3
4use std::ops::ControlFlow;
5
6pub use error::Error;
7pub use step::Step;
8
9use crate::{engine, probe::Stopped};
10
11pub(super) type Retirement<T, R> = Result<
12 T,
13 Stopped<
14 R,
15 Error<<<R as Resolver>::Backend as engine::raw::Poll>::Error, <R as Resolver>::Fault>,
16 >,
17>;
18
19pub trait Resolver: Sized {
21 type Backend: engine::raw::Poll;
22 type Fault: Copy;
23
24 fn backend(&mut self) -> &mut Self::Backend;
25 fn retire_one(&mut self) -> Step;
26 fn retire(&mut self) {
28 while let Step::Advanced = self.retire_one() {}
29 }
30 fn has_pending(&self) -> bool;
31 fn has_flights(&self) -> bool;
32 fn has_inflight(&self) -> bool;
33}
34
35pub trait Reclaim: Resolver {
37 fn reclaim(self) -> Result<ControlFlow<Self, Self>, Stopped<Self, Self::Fault>>;
38 fn fault(failure: Stopped<Self, Self::Fault>) -> Self;
40 fn reactor(&mut self, failure: <Self::Backend as engine::raw::Poll>::Error);
42
43 fn drain(mut self) -> Result<Self, Stopped<Self, Self::Fault>> {
46 let mut first = None;
47 let state = loop {
48 match self.reclaim() {
49 Ok(ControlFlow::Continue(state)) => self = state,
50 Ok(ControlFlow::Break(state)) => break state,
51 Err(failure) => {
52 first.get_or_insert(failure.cause);
53 self = Self::fault(failure);
54 }
55 }
56 };
57 match first {
58 None => Ok(state),
59 Some(cause) => Err(Stopped { state, cause }),
60 }
61 }
62}
63
64pub trait Terminal {
66 fn has_flights(&self) -> bool;
67 fn has_inflight(&self) -> bool;
68 fn has_pending(&self) -> bool;
69 fn source(&self) -> Option<engine::register::Source<'_>>;
70}
71
72pub trait Resume: Reclaim {
74 type Terminal<'s>: Terminal
75 where
76 Self: 's;
77
78 fn terminal(&mut self) -> (&mut Self::Backend, Self::Terminal<'_>);
79}
80
81pub trait Close: Resolver {
83 type Closing: Reclaim<Backend = Self::Backend, Fault = Self::Fault>;
84
85 fn into_closing(self) -> Self::Closing;
86}
87
88pub trait Cancel: Close {
90 type Cancelling: Reclaim<Backend = Self::Backend, Fault = Self::Fault>
91 + Close<Closing = Self::Closing>;
92
93 fn cancel(self) -> Self::Cancelling;
94}
95
96pub trait Quiesced {
100 type Resolver: Reclaim;
101
102 fn into_inner(self) -> Self::Resolver;
103}
104
105pub trait Finish<'a, R>
107where
108 R: Reclaim + 'a,
109 <R::Backend as engine::raw::Poll>::Context: 'a,
110{
111 type Quiesced: Quiesced<Resolver = R>;
112
113 fn try_finish(resolver: R) -> Retirement<Self::Quiesced, R>;
114}
115
116pub trait Quiesce<'a, R>
118where
119 R: Cancel + 'a,
120 <R::Backend as engine::raw::Poll>::Context: 'a,
121{
122 type Quiesced: Quiesced<Resolver = R::Closing>;
123
124 fn try_quiesce(resolver: R) -> Retirement<Self::Quiesced, R::Closing>;
125}