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 anRc<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 firstupgrade()
to a strongSharedCell
. This pattern is how we break circular ownership such as Exchange ↔ExecutionClient
: the exchange keeps aSharedCell
to the client, while the client holds only aWeakCell
back to the exchange.
Structs§
- Shared
Cell - Strong, shared ownership of
T
with interior mutability. - Weak
Cell - Weak counterpart to
SharedCell
.