nros/sizes.rs
1//! Single source of truth for FFI opaque-storage sizes.
2//!
3//! Each `export_size!` invocation produces two artefacts:
4//!
5//! * `pub const FOO_SIZE: usize = core::mem::size_of::<T>();` — a normal
6//! Rust const suitable for in-crate `const _: () = assert!(...)` checks and
7//! direct use by `no_std` consumers.
8//! * `pub static __NROS_SIZE_FOO: [u8; FOO_SIZE] = [0; FOO_SIZE];` — an
9//! array-sized static whose *symbol storage size* in the compiled rlib
10//! equals `FOO_SIZE`. `nros-c`/`nros-cpp` build scripts read the sizes out
11//! via [`nros_sizes_build::extract_sizes`](../../../nros-sizes-build/index.html)
12//! to derive opaque-storage macros for the generated C/C++ headers.
13//!
14//! Feature gating follows the rest of the crate: the statics only exist when
15//! an RMW backend (`rmw-zenoh` / `rmw-xrce` / `rmw-cyclonedds` / `rmw-cffi`) is
16//! active, which is exactly the condition under which the `Rmw*` type
17//! aliases resolve. Workspace-level `cargo check` without any RMW feature
18//! sees this module as empty.
19
20#[cfg(feature = "rmw-cffi")]
21mod rmw_sizes {
22 use crate::internals::{
23 RmwPublisher, RmwServiceClient, RmwServiceServer, RmwSession, RmwSubscriber,
24 };
25
26 // Phase 77.25: per-name v0-mangled markers so the probe works
27 // under fat LTO. Each call to `export_size!(NAME = Ty)` expands to
28 // a distinct generic fn `__nros_size_NAME<const N: usize>` plus a
29 // monomorphised fn-pointer static. The monomorphisation's v0
30 // mangled symbol name contains both the name ("NAME") *and* the
31 // const-generic value (the size) — e.g. demangles as
32 // `nros::sizes::rmw_sizes::__nros_size_PUBLISHER_SIZE::<48>`.
33 // Symbol names survive LTO because the linker still needs them,
34 // even when the object file is LLVM bitcode and `object::parse`
35 // can't read symbol byte sizes. The original `__NROS_SIZE_<NAME>`
36 // static kept for backwards-compat is also emitted for consumers
37 // that still walk the legacy path.
38 #[doc(hidden)]
39 pub mod _size_markers {}
40
41 macro_rules! export_size {
42 ($vis:vis $name:ident = $ty:ty) => {
43 $vis const $name: usize = core::mem::size_of::<$ty>();
44 paste::paste! {
45 #[cfg_attr(feature = "ffi-size-markers", used)]
46 #[unsafe(no_mangle)]
47 #[doc(hidden)]
48 pub static [<__NROS_SIZE_ $name>]: [u8; $name] = [0u8; $name];
49
50 #[doc(hidden)]
51 #[allow(non_snake_case)]
52 #[inline(never)]
53 pub fn [<__nros_size_ $name>]<const N: usize>() -> usize { N }
54
55 #[cfg_attr(feature = "ffi-size-markers", used)]
56 #[doc(hidden)]
57 pub static [<__NROS_SIZE_FN_ $name>]: fn() -> usize =
58 [<__nros_size_ $name>]::<{ $name }>;
59 }
60 };
61 }
62
63 export_size!(pub SESSION_SIZE = RmwSession);
64 export_size!(pub PUBLISHER_SIZE = RmwPublisher);
65 export_size!(pub SUBSCRIBER_SIZE = RmwSubscriber);
66 export_size!(pub SERVICE_CLIENT_SIZE = RmwServiceClient);
67 export_size!(pub SERVICE_SERVER_SIZE = RmwServiceServer);
68
69 // Phase 122.3.c.3 — L1 polling-mode Raw* handles. Defaults to
70 // `DEFAULT_RX_BUF_SIZE` on each const-generic slot, which is
71 // exactly the value `nros-c::config::MESSAGE_BUFFER_SIZE` resolves
72 // to (both derive from `NROS_SUBSCRIPTION_BUFFER_SIZE` via
73 // `nros-node`'s build.rs). nros-c's build.rs reads these probe
74 // values and emits the matching `*_OPAQUE_U64S` macros into the
75 // per-build variant header so C struct `_opaque` storage agrees
76 // with the Rust struct layout. Without this probe, cbindgen
77 // ships a `_OPAQUE_U64S = 1` placeholder which silently
78 // truncates the C-side `_opaque` slot.
79 export_size!(pub RAW_SUBSCRIPTION_SIZE = nros_node::RawSubscription);
80 export_size!(pub RAW_SERVICE_SERVER_SIZE = nros_node::RawServiceServer);
81 export_size!(pub RAW_SERVICE_CLIENT_SIZE = nros_node::RawServiceClient);
82
83 // Phase 122.3.c.6 — typeless action `*Core` types are the raw
84 // L1 polling-mode entities (typed `ActionServer<A>` / `ActionClient<A>`
85 // wrap them with serdes glue). Default const generics resolve to
86 // `DEFAULT_RX_BUF_SIZE` (= `MESSAGE_BUFFER_SIZE` in nros-c) and
87 // `MAX_GOALS = 4`, matching the L2 callback path.
88 export_size!(pub RAW_ACTION_SERVER_SIZE = nros_node::ActionServerCore);
89 export_size!(pub RAW_ACTION_CLIENT_SIZE = nros_node::ActionClientCore);
90
91 export_size!(pub EXECUTOR_SIZE = nros_node::Executor);
92 export_size!(pub GUARD_CONDITION_SIZE = nros_node::GuardConditionHandle);
93 export_size!(pub LIFECYCLE_CTX_SIZE = nros_node::lifecycle::LifecyclePollingNodeCtx);
94 // Phase 91.C: nros-c's `ActionServerInternal` embeds this nros-node
95 // type as a typed field. cbindgen (which can't recurse into deps)
96 // emits the field as `ActionServerRawHandle handle;` referencing a
97 // type it cannot define. nros-c's build.rs reads this size and emits
98 // an opaque type-compatible declaration into nros_config_generated.h
99 // so the cbindgen output is self-contained.
100 export_size!(pub ACTION_SERVER_RAW_HANDLE_SIZE = nros_node::ActionServerRawHandle);
101
102 // Layout-mirror struct for `nros_c::action::ActionServerInternal`.
103 // ActionServerInternal lives in the `nros-c` crate (it embeds C-API
104 // pointer types like `*mut nros_action_server_t`), so it can't be
105 // referenced from `nros` directly. This mirror has the same `#[repr(C)]`
106 // field shape — `*mut c_void` and `unsafe extern "C" fn(*mut c_void, ...)`
107 // pointer slots — and therefore the same byte size, since fn-pointer
108 // size is independent of parameter types. nros-c asserts at compile
109 // time that `size_of::<ActionServerInternal>() ==
110 // size_of::<ActionServerInternalLayout>()`.
111 use core::ffi::c_void;
112 type CGoalCallbackLayout =
113 unsafe extern "C" fn(*mut c_void, *const c_void, *const u8, usize, *mut c_void) -> i32;
114 type CCancelCallbackLayout =
115 Option<unsafe extern "C" fn(*const c_void, i32, *mut c_void) -> i32>;
116 type CAcceptedCallbackLayout =
117 Option<unsafe extern "C" fn(*mut c_void, *const c_void, *mut c_void)>;
118
119 #[repr(C)]
120 #[doc(hidden)]
121 pub struct ActionServerInternalLayout {
122 pub handle: nros_node::ActionServerRawHandle,
123 pub executor_ptr: *mut c_void,
124 pub c_goal_callback: CGoalCallbackLayout,
125 pub c_cancel_callback: CCancelCallbackLayout,
126 pub c_accepted_callback: CAcceptedCallbackLayout,
127 pub c_context: *mut c_void,
128 pub server_ptr: *mut c_void,
129 }
130 export_size!(pub ACTION_SERVER_INTERNAL_SIZE = ActionServerInternalLayout);
131
132 // Layout-mirrors for nros-cpp's `CppActionServer` and `CppActionClient`.
133 //
134 // Same approach as `ActionServerInternalLayout` above: nros-cpp's
135 // wrapper structs live in a downstream crate but their byte sizes can
136 // be reconstructed from the field shape. This eliminates the
137 // hand-math in `nros-cpp/build.rs` (was Phase 87.11).
138 //
139 // The C++-side `nros::ActionServer<A>` / `nros::ActionClient<A>`
140 // classes hold opaque storage sized to these probe values. nros-cpp
141 // asserts `size_of::<CppActionServer>() == size_of::<CppActionServerLayout>()`
142 // (and the same for CppActionClient) so any field-shape drift in the
143 // real wrapper trips the build immediately.
144
145 type CppGoalCallbackLayout =
146 unsafe extern "C" fn(*const [u8; 16], *const u8, usize, *mut c_void) -> i32;
147 type CppCancelCallbackLayout = unsafe extern "C" fn(*const [u8; 16], *mut c_void) -> i32;
148
149 // Byte-shape mirror of one of `nros-cpp`'s `nros_cpp_qos_t` policy enums
150 // (`nros_cpp_qos_reliability_t` et al). These are `#[repr(C)]` fieldless
151 // enums, so their width follows the *target C ABI*: `c_int` (4 bytes) on
152 // x86_64, but **1 byte on ARM EABI** (`armv7a-nuttx-eabihf` defaults to
153 // `-fshort-enums`). Mirroring them as `c_int` (the pre-fix shape) over-sized
154 // the qos block by 12 bytes on ARM and tripped the `CppActionServer`
155 // layout assert. A `#[repr(C)]` enum here tracks the same short-enum width
156 // on every target. Variant count is irrelevant to width while ≤ 255 (all
157 // four real enums have ≤ 4 variants), so one placeholder mirrors all four.
158 #[repr(C)]
159 #[doc(hidden)]
160 pub enum CppQosEnumLayout {
161 A = 0,
162 B = 1,
163 }
164
165 // Phase 193.4b: byte-shape mirror of `nros-cpp`'s `nros_cpp_qos_t`
166 // (4 C-ABI enums + `c_int` depth + 3×u32 + u8). The action server now
167 // stores the create-time QoS until registration; the real
168 // `CppActionServer` field-shape assert keeps this in sync.
169 #[repr(C)]
170 #[doc(hidden)]
171 pub struct CppQosLayout {
172 pub reliability: CppQosEnumLayout,
173 pub durability: CppQosEnumLayout,
174 pub history: CppQosEnumLayout,
175 pub liveliness_kind: CppQosEnumLayout,
176 pub depth: core::ffi::c_int,
177 pub deadline_ms: u32,
178 pub lifespan_ms: u32,
179 pub liveliness_lease_ms: u32,
180 pub avoid_ros_namespace_conventions: u8,
181 }
182
183 // Phase 87.6 thin-wrapper: `action_name` / `type_name` / `type_hash`
184 // buffers moved to the C++ `nros::ActionServer<A>` class.
185 // Phase 104.C.9.b: `node_id` field added so action create→register
186 // can route through the per-Node session.
187 // Phase 193.4b: `qos` field added so create-time QoS reaches the three
188 // underlying service servers at register time.
189 #[repr(C)]
190 #[doc(hidden)]
191 pub struct CppActionServerLayout {
192 pub handle: Option<nros_node::ActionServerRawHandle>,
193 pub goal_cb: Option<CppGoalCallbackLayout>,
194 pub cancel_cb: Option<CppCancelCallbackLayout>,
195 pub cb_ctx: *mut c_void,
196 pub node_id: u8,
197 pub _reserved: [u8; 7],
198 pub qos: CppQosLayout,
199 }
200 export_size!(pub CPP_ACTION_SERVER_SIZE = CppActionServerLayout);
201
202 type CppActionGoalResponseCb = Option<unsafe extern "C" fn(bool, *const [u8; 16], *mut c_void)>;
203 type CppActionFeedbackCb =
204 Option<unsafe extern "C" fn(*const [u8; 16], *const u8, usize, *mut c_void)>;
205 type CppActionResultCb =
206 Option<unsafe extern "C" fn(*const [u8; 16], i32, *const u8, usize, *mut c_void)>;
207
208 #[repr(C)]
209 #[doc(hidden)]
210 pub struct CppActionClientCallbacksLayout {
211 pub goal_response: CppActionGoalResponseCb,
212 pub feedback: CppActionFeedbackCb,
213 pub result: CppActionResultCb,
214 pub context: *mut c_void,
215 }
216
217 // Phase 87.6 thin-wrapper: `action_name` buffer moved to the C++
218 // `nros::ActionClient<A>` class.
219 #[repr(C)]
220 #[doc(hidden)]
221 pub struct CppActionClientLayout {
222 pub callbacks: CppActionClientCallbacksLayout,
223 pub arena_entry_index: i32,
224 pub executor_ptr: *mut c_void,
225 }
226 export_size!(pub CPP_ACTION_CLIENT_SIZE = CppActionClientLayout);
227}
228
229#[cfg(feature = "rmw-cffi")]
230pub use rmw_sizes::*;