Skip to main content

nros_node/executor/
declared_shape.rs

1//! Which registration entry point a DECLARED
2//! subscription reaches, as ONE value.
3//!
4//! # The hole this fills
5//!
6//! W3 made `[[endpoint]] registration_path`'s third row an OBSERVATION:
7//! `SubscriptionRequest::in_place_capable` is stated at each of the executor's
8//! eleven subscription entry points, read at
9//! [`Executor::open_subscription`][open], and reported from there through
10//! [`registration_observer`][obs]. A row nobody observed REFUSES, and every
11//! consumer then budgets the receive region — the safe direction.
12//!
13//! The **Rust producer observes nothing**. `nros::__macro::record_node_metadata::<C>`
14//! runs a component's `register()` against a recording `NodeContext` and opens
15//! no executor at all, so no registration happens in the probe and every Rust
16//! endpoint's row refuses. That was the bug this module fixes.
17//!
18//! # Why a classifier is allowed here, when W3 deleted one
19//!
20//! W3 deleted an inference: the sizing descriptor composed the in-place row
21//! from the entry's LANGUAGE, which is wrong for nine of the eleven entry
22//! points. That was a SECOND OPINION — a table in the CLI about code it cannot
23//! see.
24//!
25//! The declarative road is not that shape. Its registrar is ONE function —
26//! `nros::node_runtime`'s `EntityKind::Subscription` arm — and it lowers every
27//! declared subscription to exactly one of two entry points. So the shape a
28//! declared endpoint takes is a FUNCTION OF THE DECLARATION, computed in one
29//! place, and this enum is that function's codomain.
30//!
31//! It stays one opinion only because of the two rules below, both gated by
32//! `check-declared-subscription-shape`:
33//!
34//! 1. **The registrar branches on it.** The subscription arm does not test the
35//!    declaration's `safety` flag itself; it asks for a
36//!    [`DeclaredSubscriptionShape`] and dispatches on the answer. A variant it
37//!    does not name is a gate failure, not a silent fall-through.
38//! 2. **The entry point takes its value from here.** Each entry point's
39//!    `SubscriptionRequest::in_place_capable` is spelled
40//!    `DeclaredSubscriptionShape::<V>.in_place_capable()`, so there is exactly
41//!    one `bool` per shape in the tree, and the entry point and the probe move
42//!    together when it changes — which is what makes this cheaper than
43//!    teaching the probe to register.
44//!
45//!    **Moving together is necessary, not sufficient.** A fix flipped
46//!    `BufferedRaw` to `true`, and this doc used to call that "a one-line
47//!    change here". It was not, and the one-line version is UNSAFE: the probe
48//!    would state `in_place`, the build would price the row at no receive
49//!    region, and the executor — which then ignored `SubscriptionOpen::in_place`
50//!    on this path — would still build a buffered entry and claim the full
51//!    region. That is the under-size direction, `BufferTooSmall` at boot. The
52//!    flip is safe only because `register_subscription_buffered_raw_on` now
53//!    BRANCHES on the report; a `true` here is a promise that the entry point
54//!    acts on it. `a_generic_subscription_on_an_in_place_backend_claims_no_
55//!    receive_region` fails first on a flip without the branch.
56//!
57//! [open]: super::Executor
58//! [obs]: super::registration_observer
59
60/// Which of the executor's registration entry points a DECLARED subscription
61/// lowers to.
62///
63/// One variant per lowering the declarative registrar can choose. Exhaustive:
64/// a new lowering is a new variant, which breaks
65/// [`in_place_capable`](Self::in_place_capable) at compile time and the
66/// registrar at its gate.
67#[derive(Debug, Clone, Copy, PartialEq, Eq)]
68pub enum DeclaredSubscriptionShape {
69    /// `Node::create_generic_subscription_with_qos_and_rx_hint` →
70    /// `Executor::register_subscription_buffered_raw_on`.
71    BufferedRaw,
72    /// A `.safety()` declaration:
73    /// `Node::create_generic_subscription_with_integrity_and_rx_hint` →
74    /// `Executor::register_subscription_buffered_raw_safety_on`.
75    BufferedRawSafety,
76    /// A declaration that asked for the message's metadata:
77    /// `Node::create_generic_subscription_with_info_and_rx_hint` →
78    /// `Executor::register_subscription_buffered_raw_msg_info_on`, the path
79    /// C++'s `(msg, MessageInfo)` callback takes too.
80    BufferedRawInfo,
81}
82
83impl DeclaredSubscriptionShape {
84    /// Classify a declaration.
85    ///
86    /// `message_info` is the declaration's request for the message's
87    /// metadata (`CallbackCtx::message_info`). A declaration states at most
88    /// one of the two: each has its own declaration call, and neither call
89    /// sets the other's flag. If both were set, `safety` wins, because a
90    /// validated sample must not reach a callback unvalidated.
91    ///
92    /// `safety` is the declaration's `.safety()` flag **as the runtime will
93    /// see it**: a build whose `safety-e2e` capability is off ignores the flag
94    /// and registers the basic path, so its caller masks the flag to `false`
95    /// before asking. That mask has exactly one home on the caller's side, for
96    /// the same reason this classifier has one here.
97    pub const fn of_declaration(safety: bool, message_info: bool) -> Self {
98        if safety {
99            Self::BufferedRawSafety
100        } else if message_info {
101            Self::BufferedRawInfo
102        } else {
103            Self::BufferedRaw
104        }
105    }
106
107    /// Can a registration of this shape dispatch out of the backend's own
108    /// receive slot?
109    ///
110    /// **The definition, not a copy of one.** Each entry point writes this
111    /// call into its `SubscriptionRequest`, so the answer a probe states and
112    /// the answer the executor acts on are the same expression.
113    ///
114    /// The two shapes differ, and the reason each has its answer is worth a
115    /// sentence:
116    ///
117    /// * **`BufferedRaw` — `true`**. Its callback is
118    ///   `FnMut(&[u8])`, borrowed bytes and nothing else, which is exactly what
119    ///   `process_raw_in_place` hands over. It was `false` while its arena entry
120    ///   was believed shared with `add_arena_subscription_callback`; that caller
121    ///   never reaches `open_subscription`, so the in-place arm could be local to
122    ///   the one entry point. A true here is a claim the build PRICES: the
123    ///   probe states `in_place` for every declared non-safety subscription, and
124    ///   on an in-place backend that endpoint reserves no receive region.
125    /// * **`BufferedRawSafety` — `false`.** `process_raw_in_place` has no
126    ///   validating form: an integrity-checked sample must be verified before
127    ///   the callback sees it, and the borrowed slot offers nowhere to put the
128    ///   verdict. Not a limit anyone plans to lift by flipping this.
129    /// * **`BufferedRawInfo` — `false`.** Its metadata comes with a COPYING
130    ///   take (`take_serialized_with_info`); `process_raw_in_place` hands over
131    ///   borrowed payload bytes and nothing else. This is the price of asking
132    ///   for the metadata, and why asking is opt-in.
133    pub const fn in_place_capable(self) -> bool {
134        match self {
135            Self::BufferedRaw => true,
136            Self::BufferedRawSafety | Self::BufferedRawInfo => false,
137        }
138    }
139
140    /// Does a registration of this shape claim the closure buffer (`RX_BUF`)
141    /// when it does NOT dispatch in place, rather than a bound the call site
142    /// stated?
143    ///
144    /// Both entry points are type-erased and write
145    /// `slot_bytes: RX_BUF` into their `SubscriptionRequest`, which is what
146    /// `registration_observer` reports as `claims_closure_buffer`. The Rust
147    /// probe declares without registering, so it states this from the shape —
148    /// beside [`in_place_capable`](Self::in_place_capable), because a row that
149    /// carries one of the two facts and not the other was priced by a backend
150    /// default: a scaffolded Cyclone listener got the 12-byte bound of its type
151    /// and died at boot short of `RX_BUF`.
152    pub const fn claims_closure_buffer(self) -> bool {
153        match self {
154            Self::BufferedRaw | Self::BufferedRawSafety | Self::BufferedRawInfo => true,
155        }
156    }
157}
158
159#[cfg(test)]
160mod tests {
161    use super::*;
162
163    #[test]
164    fn a_declaration_classifies_by_its_flags() {
165        use DeclaredSubscriptionShape as S;
166        assert_eq!(S::of_declaration(false, false), S::BufferedRaw);
167        assert_eq!(S::of_declaration(true, false), S::BufferedRawSafety);
168        assert_eq!(S::of_declaration(false, true), S::BufferedRawInfo);
169        // A validated sample never reaches a callback unvalidated.
170        assert_eq!(S::of_declaration(true, true), S::BufferedRawSafety);
171    }
172
173    /// Not a restatement of `in_place_capable`'s body: this is the claim the
174    /// PROBE ships, so it is what every Rust image's declared subscriptions are
175    /// PRICED at.
176    ///
177    /// It was ~~`neither_declarative_shape_dispatches_in_place_today`~~ until issue
178    /// 1340 flipped the raw arm, and that rename is the point: this test is
179    /// where a change of price is said out loud. A declared non-safety
180    /// subscription now states `in_place`, so on zenoh and XRCE it claims no
181    /// receive region; a `.safety()` one still buffers.
182    #[test]
183    fn only_the_non_safety_declarative_shape_dispatches_in_place() {
184        assert!(DeclaredSubscriptionShape::BufferedRaw.in_place_capable());
185        assert!(!DeclaredSubscriptionShape::BufferedRawSafety.in_place_capable());
186        assert!(!DeclaredSubscriptionShape::BufferedRawInfo.in_place_capable());
187    }
188}