Skip to main content

nros_platform/board/
dispatch.rs

1//! `DispatchStrategy` enum.
2//!
3//! Declares how a Node pkg expects its callbacks to fire. The board
4//! crate's `NodeDispatchRuntime::dispatch_strategy()` (lives at
5//! `super::runtime`) tells the codegen + check layers which strategies
6//! it can serve; `Node::DISPATCH` (lives at `nros::Node`) tells which
7//! strategy a Node requires.
8//!
9//! `nros check` cross-validates Node `DISPATCH` against the Entry pkg's
10//! board framework on a `(framework, strategy)` matrix.
11//!
12//! `#[repr(u8)]` for FFI stability: the `nros::node!()` macro emits a
13//! per-Node `__nros_node_<pkg>_dispatch_strategy() -> u8` ABI symbol so
14//! `nros check` can read the strategy without linking the Node crate.
15
16/// How a Node expects its callbacks to fire.
17#[repr(u8)]
18#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
19pub enum DispatchStrategy {
20    /// Callbacks fire from the executor's spin loop (current default).
21    /// Served by every runtime: POSIX, RTOS (FreeRTOS/NuttX/Zephyr/
22    /// ThreadX), bare-metal, RTIC (proxied via `__nros_dispatch` task
23    /// when the board demands it). The default for
24    /// every existing Node pkg — preserves backward compat.
25    Inline = 0,
26
27    /// Callbacks fire from a framework-owned task (the RTIC dispatcher). The board-side `NodeDispatchRuntime` enqueues
28    /// signaled callbacks; the framework's scheduler dequeues + drives
29    /// `ExecutableNode::on_callback` from its own task context. Needed
30    /// for Nodes whose callbacks must not run from the spin task (e.g.
31    /// callbacks that take RTIC locks or share priority with custom
32    /// tasks).
33    Deferred = 1,
34
35    /// Callbacks fire directly from an ISR handler. Design slot only —
36    /// impl deferred to Phase 216.E.1. Requires a reentrancy audit of
37    /// the dispatch path + a lock-free SPSC variant tolerant of
38    /// ISR-priority producers + a per-Node `#[isr_safe]` proof
39    /// contract. Reserved here so the matrix in Phase 216.D.1 has a
40    /// stable discriminant to reject against.
41    FromIsr = 2,
42}
43
44impl DispatchStrategy {
45    /// Compile-time default for `Node::DISPATCH`. Inline preserves
46    /// every existing Node pkg unchanged.
47    pub const DEFAULT: Self = Self::Inline;
48
49    /// FFI round-trip discriminant. Mirrors `as u8` but expressible in
50    /// `const` contexts where `as` casts on enums currently require an
51    /// `#[allow(non_upper_case_globals)]` dance.
52    #[inline]
53    pub const fn to_u8(self) -> u8 {
54        self as u8
55    }
56
57    /// Inverse of `to_u8`. Returns `None` for unknown discriminants —
58    /// `nros check` surfaces the rejection with a clear diagnostic
59    /// rather than silently treating a future strategy as `Inline`.
60    #[inline]
61    pub const fn from_u8(value: u8) -> Option<Self> {
62        match value {
63            0 => Some(Self::Inline),
64            1 => Some(Self::Deferred),
65            2 => Some(Self::FromIsr),
66            _ => None,
67        }
68    }
69}
70
71#[cfg(test)]
72mod tests {
73    use super::*;
74
75    #[test]
76    fn default_is_inline() {
77        assert_eq!(DispatchStrategy::DEFAULT, DispatchStrategy::Inline);
78    }
79
80    #[test]
81    fn u8_round_trip() {
82        for s in [
83            DispatchStrategy::Inline,
84            DispatchStrategy::Deferred,
85            DispatchStrategy::FromIsr,
86        ] {
87            assert_eq!(DispatchStrategy::from_u8(s.to_u8()), Some(s));
88        }
89    }
90
91    #[test]
92    fn from_u8_rejects_unknown() {
93        assert_eq!(DispatchStrategy::from_u8(3), None);
94        assert_eq!(DispatchStrategy::from_u8(255), None);
95    }
96}