Skip to main content

Module shared

Module shared 

Source
Expand description

Wrappers around shared, interior-mutable cell pairs.

NautilusTrader engines store many components as Rc<RefCell<T>> for shared ownership with interior mutability. Spelling that type at every boundary is verbose and risks accidentally holding a strong reference where a weak one is required, leading to reference cycles.

SharedCell<T> and WeakCell<T> are zero-cost newtypes that name the intent and forward the common operations (new, borrow, borrow_mut, with, with_mut, downgrade, upgrade). They are #[repr(transparent)] and share the memory layout of the wrapped Rc / Weak.

§Choosing between SharedCell and WeakCell

  • Use SharedCell<T> when the holder owns or co-owns the value, like a plain Rc<RefCell<T>>.
  • Use WeakCell<T> for back-references that would otherwise form a cycle. The back-pointer does not keep the value alive; every access must first upgrade() to a strong SharedCell. This pattern breaks circular ownership: for an Exchange that owns an ExecutionClient which references the exchange, the exchange holds a SharedCell to the client and the client holds a WeakCell back to the exchange.

Structs§

SharedCell
Strong, shared ownership of T with interior mutability.
WeakCell
Weak counterpart to SharedCell.