1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use super::{raw, raw_cfg, Cfg};
use crate::{
closure::{Closure, IntoClosureConst},
utils::{slice_sort_unstable_by, ComptimeVec, Init, PhantomInvariant},
};
#[doc = include_str!("../common.md")]
pub struct StartupHook<System: raw::KernelBase>(PhantomInvariant<System>);
impl<System: raw::KernelBase> StartupHook<System> {
pub const fn define() -> StartupHookDefiner<System> {
StartupHookDefiner::new()
}
const fn new() -> Self {
Self(Init::INIT)
}
}
#[must_use = "must call `finish()` to complete registration"]
pub struct StartupHookDefiner<System> {
_phantom: PhantomInvariant<System>,
start: Option<Closure>,
priority: i32,
unchecked: bool,
}
impl<System: raw::KernelBase> StartupHookDefiner<System> {
const fn new() -> Self {
Self {
_phantom: Init::INIT,
start: None,
priority: 0,
unchecked: false,
}
}
pub const fn start<C: ~const IntoClosureConst>(self, start: C) -> Self {
Self {
start: Some(start.into_closure_const()),
..self
}
}
pub const fn priority(self, priority: i32) -> Self {
Self { priority, ..self }
}
pub const unsafe fn unchecked(self) -> Self {
Self {
unchecked: true,
..self
}
}
pub const fn finish<C: ~const raw_cfg::CfgBase<System = System>>(
self,
cfg: &mut Cfg<C>,
) -> StartupHook<System> {
if self.priority < 0 && !self.unchecked {
panic!("negative priority is unsafe and should be unlocked by `unchecked`");
}
let startup_hooks = &mut cfg.startup_hooks;
let order = startup_hooks.len();
startup_hooks.push(CfgStartupHook {
start: self.start.expect("`start` is not specified"),
priority: self.priority,
order,
});
StartupHook::new()
}
}
#[derive(Debug, Clone, Copy)]
pub(crate) struct CfgStartupHook {
start: Closure,
priority: i32,
order: usize,
}
pub(crate) const fn sort_hooks(startup_hooks: &mut ComptimeVec<CfgStartupHook>) {
slice_sort_unstable_by(
startup_hooks.as_mut_slice(),
closure!(|x: &CfgStartupHook, y: &CfgStartupHook| -> bool {
if x.priority != y.priority {
x.priority < y.priority
} else {
x.order < y.order
}
}),
);
}
#[doc(hidden)]
#[derive(Clone, Copy)]
pub struct StartupHookAttr {
pub(super) start: Closure,
}
impl Init for StartupHookAttr {
const INIT: Self = Self {
start: Closure::INIT,
};
}
impl CfgStartupHook {
#[allow(clippy::wrong_self_convention)]
pub const fn to_attr(&self) -> StartupHookAttr {
StartupHookAttr { start: self.start }
}
}