deferred-cell
A single-assignment, weak reference wrapper for static cyclic node graphs with late initialization.
This crate provides a lightweight alternative to RefCell<Option<Weak<T>>>
when building reference graphs such as cyclic trees, linked lists, and bidirectional or circular structures. It enables you to cleanly and safely establish weak links between nodes after they are constructed.
✨ Features
- ✅ Write-once semantics using
OnceCell<Weak<T>>
- ✅ Safe and ergonomic API for deferred initialization
- ✅ Strong test coverage
- ✅ Optional helper type for setting values:
DeferredMut
- ✅ Iterator extension trait for working with collections
📦 Installation
Add to your Cargo.toml
:
[]
= "0.1"
🧠 Motivation
In Rust, it’s often tricky to build cyclic data structures due to ownership rules. A common workaround is:
However, this allows re-assignment and mutation, which is overkill in cases where the weak reference should be set only once. deferred-cell
simplifies this pattern with explicit, single-assignment behavior and no runtime borrow checking.
🚀 Example
use ;
use Rc;
🔧 API Highlights
let d: = default;
let rc: = ...;
// One-time set
from.try_set?;
// Later access
let strong: = d.try_get?;
// Optional checks
if d.is_ready
Also includes a DeferredIteratorExt
trait to streamline iteration:
let values: = list
.into_iter
.get_deferred
.map
.collect;
⚠️ Errors
Two error types are defined:
DeferredError::DuplicateInitialization
– iftry_set()
is called more than onceDeferredError::NotInitializedError
– ifget()
ortry_get()
is called before a value is set