Skip to main content

leodos_libcfs/
cell.rs

1//! Single-task cell for static storage.
2//!
3//! [`TaskLocalCell`] wraps a value in an [`UnsafeCell`] with a
4//! `Sync` impl, allowing it to be used as a `static`. This is
5//! sound in cFS apps where each app runs as exactly one task.
6
7use core::cell::UnsafeCell;
8
9/// A cell for static storage in single-task cFS apps.
10///
11/// Provides `&mut T` access without runtime overhead. Only
12/// sound when the static is accessed by a single cFS task.
13pub struct TaskLocalCell<T>(UnsafeCell<T>);
14
15// SAFETY: cFS apps run as a single task — no concurrent access.
16unsafe impl<T> Sync for TaskLocalCell<T> {}
17
18impl<T> TaskLocalCell<T> {
19    /// Creates a new cell with the given value.
20    pub const fn new(val: T) -> Self {
21        Self(UnsafeCell::new(val))
22    }
23
24    /// Returns a mutable reference to the inner value.
25    ///
26    /// # Safety contract
27    ///
28    /// The caller must ensure that only one cFS task accesses
29    /// this cell. This is guaranteed by default for any static
30    /// inside a single cFS app's `.so`.
31    pub fn get_mut(&self) -> &mut T {
32        // SAFETY: single cFS task, no concurrent access.
33        unsafe { &mut *self.0.get() }
34    }
35}