Module shared

Module shared 

Source
Expand description

Efficient and ergonomic wrappers around frequently-used Rc<RefCell<T>> / Weak<RefCell<T>> pairs.

The NautilusTrader codebase heavily relies on shared, interior-mutable ownership for many engine components (Rc<RefCell<T>>). Repeating that verbose type across many APIs—alongside its weak counterpart—clutters code and increases the likelihood of accidentally storing a strong reference where only a weak reference is required (leading to reference cycles).

SharedCell<T> and WeakCell<T> are zero-cost new-types that make the intent explicit and offer convenience helpers (downgrade, upgrade, borrow, borrow_mut). Because the wrappers are #[repr(transparent)], they have the exact same memory layout as the wrapped Rc / Weak and introduce no runtime overhead.

§Choosing between SharedCell and WeakCell

  • Use SharedCell<T> when the current owner genuinely owns (or co-owns) the value – just as you would normally store an Rc<RefCell<T>>.
  • Use WeakCell<T> for back-references that could otherwise form a reference cycle. The back-pointer does not keep the value alive, and every access must first upgrade() to a strong SharedCell. This pattern is how we break circular ownership such as Exchange ↔ ExecutionClient: the exchange keeps a SharedCell to the client, while the client holds only a WeakCell back to the exchange.

Structs§

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