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
use core::marker::PhantomData;
use crate::{
kernel::{cfg::CfgBuilder, startup, Port},
utils::ComptimeVec,
};
impl<System: Port> startup::StartupHook<System> {
pub const fn build() -> CfgStartupHookBuilder<System> {
CfgStartupHookBuilder::new()
}
}
#[must_use = "must call `finish()` to complete registration"]
pub struct CfgStartupHookBuilder<System> {
_phantom: PhantomData<System>,
start: Option<fn(usize)>,
param: usize,
priority: i32,
unchecked: bool,
}
impl<System: Port> CfgStartupHookBuilder<System> {
const fn new() -> Self {
Self {
_phantom: PhantomData,
start: None,
param: 0,
priority: 0,
unchecked: false,
}
}
pub const fn start(self, start: fn(usize)) -> Self {
Self {
start: Some(start),
..self
}
}
pub const fn param(self, param: usize) -> Self {
Self { param, ..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(self, cfg: &mut CfgBuilder<System>) -> startup::StartupHook<System> {
let inner = &mut cfg.inner;
if self.priority < 0 && !self.unchecked {
panic!("negative priority is unsafe and should be unlocked by `unchecked`");
}
let order = inner.startup_hooks.len();
inner.startup_hooks.push(CfgBuilderStartupHook {
start: if let Some(x) = self.start {
x
} else {
panic!("`start` is not specified")
},
param: self.param,
priority: self.priority,
order,
});
startup::StartupHook::new()
}
}
#[doc(hidden)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CfgBuilderStartupHook {
start: fn(usize),
param: usize,
priority: i32,
order: usize,
}
pub(super) const fn sort_hooks(startup_hooks: &mut ComptimeVec<CfgBuilderStartupHook>) {
sort_unstable_by!(
startup_hooks.len(),
|i| startup_hooks.get_mut(i),
|x, y| if x.priority != y.priority {
x.priority < y.priority
} else {
x.order < y.order
}
);
}
impl CfgBuilderStartupHook {
pub const fn to_attr(&self) -> startup::StartupHookAttr {
startup::StartupHookAttr {
start: self.start,
param: self.param,
}
}
}