pub struct PendingErrorCell(/* private fields */);Expand description
Interior-mutable slot for the renderer’s pending error-scope value.
This is the euv-engine analog of euv-core’s HandlerRegistryCell
(core/src/renderer/registry/struct.rs:62): a single-element
Sync wrapper that holds an Option<JsValue> behind an
UnsafeCell.
§Why this type exists
WebGpuRenderer::pending_error needs interior mutability
because:
pop_error_synctakes&self(the WebGPU hot path cannot beasync), but the spawnedwasm_bindgen_futures::spawn_localfuture must mutate the slot to store the resolvedPromise<GPUError?>value.take_last_erroralso takes&selfand drains the slot on the next render tick.
The first implementation used Rc<RefCell<Option<JsValue>>>,
which works but pays for:
- a
RefCell::borrow_mutruntime borrow check on every write (the panic path is unreachable in practice — only the spawn_local future andtake_last_errorever touch the slot, and they never overlap because the future is a microtask drained before the next render tick). - a heap allocation for the
RefCell’s borrow state.
The newtype keeps the interior-mutability primitive (Rc),
because the spawn_local future needs its own owning handle,
but swaps the inner cell from RefCell to UnsafeCell:
- zero runtime borrow check (the WASM single-threaded scheduler makes the borrow impossible to violate).
- zero allocation (the cell is just a
*mut Option<JsValue>sitting inside theRc-managed box).
§Sync safety
PendingErrorCell is not Sync by default (UnsafeCell
explicitly opts out). We hand-implement Sync for it because
the renderer is only ever used in the WASM single-threaded
runtime; the Rc ensures the same instance is never shared
across threads (it is not Send/Sync either), and the
WASM main thread is the only place that ever touches the
slot. This matches euv-core’s pattern
(unsafe impl Sync for HandlerRegistryCell {}).
If the engine is ever compiled for a multi-threaded target
(native, wasm-bindgen-rayon), this unsafe impl Sync is
unsound and must be removed.
Implementations§
Source§impl PendingErrorCell
impl PendingErrorCell
Sourcepub fn new() -> Self
pub fn new() -> Self
Construct a new, empty pending-error slot.
The inner UnsafeCell<Option<JsValue>> starts as None; the
WebGPU pop_error_sync microtask is the only thing that ever
writes to it, and take_last_error is the only reader.
Sourcepub fn as_ptr(&self) -> *mut Option<JsValue>
pub fn as_ptr(&self) -> *mut Option<JsValue>
Hand out a raw pointer to the inner cell for the
wasm_bindgen_futures::spawn_local closure to write through.
§Safety
The returned pointer is only valid for the lifetime of &self,
and only safe to write to on the WASM main thread. The caller
must guarantee that no other code is reading the same
PendingErrorCell concurrently — this is enforced by the
single-threaded scheduler: the spawned future is drained
before the next render tick’s take_last_error runs.