Skip to main content

ResourceSlot

Struct ResourceSlot 

Source
pub struct ResourceSlot<T> { /* private fields */ }
Expand description

A one-value handoff cell threading an owned resource from main into a supervised task — the safe replacement for Peripherals::steal() inside the task body.

Declared (as a pub static) by supervisor_graph! for each entry in a node’s resources: clause. The protocol:

  1. main splits Peripherals and moves the resource in with provide. This is where the compile-time guarantee lives: the singleton field is consumed, so no second owner — and no unsafe steal — can exist.
  2. The generated spawn glue takes it just before spawning the node. An empty slot fails the spawn with SpawnError::Busy — a fail-closed error out of Supervisor::start, not a panic inside the task (compare static_cell::StaticCell, which panics on misuse).
  3. The generated task shell hands the worker &mut T and restores the value after the worker returns, so a Terminate respawn re-takes the same instance instead of stealing a fresh one. (A Pause worker never returns — it parks — so it simply retains the resource, exactly like a hand-written parked task.)

Same primitives as SpawnerSlot: a critical-section BlockingMutex<Cell<Option<T>>> for the value (Sync for T: Send, provided by embassy-sync — no unsafe here) plus a latching Signal so the supervisor can await late provisioning (bounded; see Supervisor::start).

Implementations§

Source§

impl<T> ResourceSlot<T>

Source

pub const fn new() -> Self

An empty slot (const — it lives in a static the macro emits).

Source

pub fn provide(&self, value: T)

Move the resource in (from main’s Peripherals split) and wake the supervisor’s pre-spawn wait. Call before Supervisor::start; a slot still empty after the supervisor’s bounded wait fails that node’s spawn with SpawnError::Busy. Filling an occupied slot replaces (drops) the old value — don’t: one resource, one slot, moved exactly once.

Source

pub fn take(&self) -> Option<T>

Take the resource out, leaving the slot empty. Called by the generated spawn glue just before the spawn; None means “not provided yet” or “currently held by a live task instance”.

Source

pub fn get(&self) -> Option<T>
where T: Copy,

Copy the resource out without emptying the slot — the shared resource kind’s read: any number of consumers (several nodes, a whole pool) get the same Copy handle, and the slot stays filled for the next one. Only for T: Copy (a Stack-like handle, a &'static registry ref); an owned singleton uses take.

Source

pub fn restore(&self, value: T)

Put the resource back for the next spawn. Called by the generated task shell after the worker returns (i.e. after its clean shutdown ack), so a respawn re-takes the same instance.

Trait Implementations§

Source§

impl<T> Default for ResourceSlot<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T: Send> ResourceGate for ResourceSlot<T>

Source§

fn is_filled(&self) -> bool

Non-consuming “is the slot currently filled” check.
Source§

fn filled_signal(&self) -> &Signal<CriticalSectionRawMutex, ()>

The latching Signal fired by provide/restore, for the supervisor’s bounded pre-spawn wait (see Supervisor::start).

Auto Trait Implementations§

§

impl<T> !Freeze for ResourceSlot<T>

§

impl<T> !RefUnwindSafe for ResourceSlot<T>

§

impl<T> Send for ResourceSlot<T>
where T: Send,

§

impl<T> Sync for ResourceSlot<T>
where T: Send,

§

impl<T> Unpin for ResourceSlot<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for ResourceSlot<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for ResourceSlot<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.