externally_driven_async_rt/
context.rs1use core::{future::Future, time::Duration};
2
3use crate::{
4 state::State,
5 storage::LoopStorageGuard,
6 tasks::{JoinHandle, ReserveTask},
7 time::{Instant, Sleep, Timeout},
8};
9
10pub struct Context<
14 'a,
15 S: State,
16 const TASKS: usize,
17 const TASK_SIZE: usize,
18 const ROOT_TASK_SIZE: usize,
19 const TIMERS: usize,
20 const FDS: usize,
21> {
22 storage: LoopStorageGuard<'a, S, TASKS, TASK_SIZE, ROOT_TASK_SIZE, TIMERS, FDS>,
23}
24
25impl<
26 'a,
27 S: State,
28 const TASKS: usize,
29 const TASK_SIZE: usize,
30 const ROOT_TASK_SIZE: usize,
31 const TIMERS: usize,
32 const FDS: usize,
33> Context<'a, S, TASKS, TASK_SIZE, ROOT_TASK_SIZE, TIMERS, FDS>
34{
35 pub(crate) fn new(
36 storage: LoopStorageGuard<'a, S, TASKS, TASK_SIZE, ROOT_TASK_SIZE, TIMERS, FDS>,
37 ) -> Self {
38 Self { storage }
39 }
40
41 pub(crate) fn storage(
42 &self,
43 ) -> &LoopStorageGuard<'a, S, TASKS, TASK_SIZE, ROOT_TASK_SIZE, TIMERS, FDS> {
44 &self.storage
45 }
46
47 pub fn dispatch(&self, event: S::Event) {
49 self.storage
50 .state_slot()
51 .borrow_mut()
52 .as_mut()
53 .expect("the state is set while tasks run")
54 .apply(event);
55 }
56
57 pub fn spawn<F, Fut>(&self, f: F) -> JoinHandle<'a, TASKS, TASK_SIZE, ROOT_TASK_SIZE>
69 where
70 F: FnOnce(Self) -> Fut,
71 Fut: Future<Output = ()> + 'a,
72 {
73 let id = self.storage.tasks().spawn(f(*self));
74 JoinHandle::new(self.storage.tasks(), id)
75 }
76
77 pub fn reserve_task(
84 &self,
85 ) -> ReserveTask<'a, S, TASKS, TASK_SIZE, ROOT_TASK_SIZE, TIMERS, FDS> {
86 ReserveTask::new(self.storage)
87 }
88
89 pub(crate) fn spawn_root<F, Fut>(
90 storage: LoopStorageGuard<'a, S, TASKS, TASK_SIZE, ROOT_TASK_SIZE, TIMERS, FDS>,
91 f: F,
92 ) where
93 F: FnOnce(Self) -> Fut,
94 Fut: Future<Output = ()> + 'a,
95 {
96 storage.tasks().spawn_root(f(Self::new(storage)));
97 }
98
99 pub fn sleep(&self, duration: Duration) -> Sleep<'_, TIMERS> {
101 Sleep::new(self.storage.timers(), Instant::now() + duration)
102 }
103
104 pub fn timeout<F: Future>(&self, duration: Duration, future: F) -> Timeout<'_, F, TIMERS> {
106 Timeout::new(future, self.sleep(duration))
107 }
108}
109
110impl<
111 S: State,
112 const TASKS: usize,
113 const TASK_SIZE: usize,
114 const ROOT_TASK_SIZE: usize,
115 const TIMERS: usize,
116 const FDS: usize,
117> Clone for Context<'_, S, TASKS, TASK_SIZE, ROOT_TASK_SIZE, TIMERS, FDS>
118{
119 fn clone(&self) -> Self {
120 *self
121 }
122}
123
124impl<
125 S: State,
126 const TASKS: usize,
127 const TASK_SIZE: usize,
128 const ROOT_TASK_SIZE: usize,
129 const TIMERS: usize,
130 const FDS: usize,
131> Copy for Context<'_, S, TASKS, TASK_SIZE, ROOT_TASK_SIZE, TIMERS, FDS>
132{
133}
134
135impl<
136 S: State,
137 const TASKS: usize,
138 const TASK_SIZE: usize,
139 const ROOT_TASK_SIZE: usize,
140 const TIMERS: usize,
141 const FDS: usize,
142> core::fmt::Debug for Context<'_, S, TASKS, TASK_SIZE, ROOT_TASK_SIZE, TIMERS, FDS>
143{
144 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
145 f.write_str("Context")
146 }
147}