//! `Parent` — storage wrapper that backs the auto-injected `parent` //! field of an exported Rust type declared with //! `#[wasm_bindgen(extends = Parent)]`. //! //! Each parent method's wasm shim takes a `*const WasmRefCell`, //! while the child's `__wbg_ptr` points at a `WasmRefCell`. //! The two pointers can't alias safely, so each JS instance carries a //! separate `__wbg_ptr_` slot for every class in its inheritance //! chain, and the parent data lives in its own `Rc>` //! allocation that the wasm runtime can clone on demand. `Parent` is //! that storage — a newtype around `Rc>`. //! //! Users do **not** declare a `Parent` field themselves. Writing //! `#[wasm_bindgen(extends = Animal)] struct Dog { ... }` causes the macro //! to inject `parent: wasm_bindgen::Parent` as the first field of //! `Dog`; an explicit user-declared `Parent` field on any //! `#[wasm_bindgen]` struct is rejected at macro time. In the child's //! constructor the field is populated with `Animal::new(...).into()` (using //! the [`From`] impl below) or with [`Parent::new`]. From inside method //! bodies the parent value is reached as `self.parent.borrow()` / //! `self.parent.borrow_mut()`. use crate::__rt::alloc::rc::Rc; use crate::__rt::{Ref, RefMut, WasmRefCell}; /// Storage wrapper for the auto-injected `parent` field on a struct that /// declares `#[wasm_bindgen(extends = Parent)]`. /// /// Under the hood this is an `Rc>` so that wasm-bindgen can /// produce a separately-refcounted parent pointer for JS-side prototype /// dispatch. Use [`Parent::borrow`] / [`Parent::borrow_mut`] to access the /// inner value. You should not need to construct `Parent` directly /// outside the child's constructor; the [`From`] impl is the typical way /// to initialize the injected `parent` field. /// /// `Clone` is a cheap `Rc` clone — both copies point at the same parent /// data. `Debug` prints the wrapper plus the inner `T` (when `T: Debug`). pub struct Parent { inner: Rc>, } impl Clone for Parent { fn clone(&self) -> Self { Self { inner: Rc::clone(&self.inner), } } } impl core::fmt::Debug for Parent { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { f.debug_tuple("Parent") .field(&*self.inner.borrow()) .finish() } } impl Parent { /// Wraps a value in a new `Parent`. pub fn new(value: T) -> Self { Self { inner: Rc::new(WasmRefCell::new(value)), } } /// Immutably borrows the wrapped value. /// /// Panics (or throws on the wasm target) if the value is currently /// mutably borrowed. pub fn borrow(&self) -> Ref<'_, T> { self.inner.borrow() } /// Mutably borrows the wrapped value. /// /// Panics (or throws on the wasm target) if the value is currently /// borrowed. pub fn borrow_mut(&self) -> RefMut<'_, T> { self.inner.borrow_mut() } /// Internal accessor used by the `#[wasm_bindgen(extends = ...)]` /// codegen to clone the inner `Rc` when producing an ancestor ABI /// pointer for JS. Not part of the public API. #[doc(hidden)] pub fn __wbg_clone_rc(&self) -> Rc> { Rc::clone(&self.inner) } } impl From for Parent { fn from(value: T) -> Self { Parent::new(value) } }