1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//! Shared parent storage for generated extended Rust types.
use crate::__rt::{Ref, RefMut};
/// Storage wrapper for the auto-injected parent field on extended Rust types.
pub struct Parent<T> {
inner: alloc::rc::Rc<core::cell::RefCell<T>>,
}
impl<T> Clone for Parent<T> {
fn clone(&self) -> Self {
Self {
inner: alloc::rc::Rc::clone(&self.inner),
}
}
}
// Match upstream wasm-bindgen, whose `Rc<WasmRefCell<T>>`-backed `Parent` opts into the
// unwind-safe markers (`WasmRefCell` implements both). These are safe marker traits.
impl<T> core::panic::UnwindSafe for Parent<T> {}
impl<T> core::panic::RefUnwindSafe for Parent<T> {}
impl<T: core::fmt::Debug> core::fmt::Debug for Parent<T> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_tuple("Parent")
.field(&*self.inner.borrow())
.finish()
}
}
impl<T> Parent<T> {
pub fn new(value: T) -> Self {
Self {
inner: alloc::rc::Rc::new(core::cell::RefCell::new(value)),
}
}
pub fn borrow(&self) -> Ref<'_, T> {
Ref {
inner: self.inner.borrow(),
}
}
pub fn borrow_mut(&self) -> RefMut<'_, T> {
RefMut {
inner: self.inner.borrow_mut(),
}
}
/// Clone the shared backing cell. Used by generated upcast exports to publish
/// an ancestor view that shares the same `T` as the descendant's parent field.
#[doc(hidden)]
pub fn share_cell(&self) -> alloc::rc::Rc<core::cell::RefCell<T>> {
alloc::rc::Rc::clone(&self.inner)
}
/// Reconstruct a `Parent<T>` from a previously [`share_cell`](Self::share_cell)d
/// backing cell, so an ancestor view stored by handle resolves to the same
/// shared `T`.
#[doc(hidden)]
pub fn from_cell(inner: alloc::rc::Rc<core::cell::RefCell<T>>) -> Self {
Self { inner }
}
}
impl<T> From<T> for Parent<T> {
fn from(value: T) -> Self {
Parent::new(value)
}
}