script/dom/bindings/cell.rs
1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5//! A shareable mutable container for the DOM.
6
7use std::cell::{BorrowError, BorrowMutError};
8#[cfg(not(feature = "refcell_backtrace"))]
9pub(crate) use std::cell::{Ref, RefCell, RefMut};
10
11#[cfg(feature = "refcell_backtrace")]
12pub(crate) use accountable_refcell::{Ref, RefCell, RefMut};
13use malloc_size_of::{MallocConditionalSizeOf, MallocSizeOfOps};
14
15use crate::dom::bindings::root::{assert_in_layout, assert_in_script};
16
17/// A mutable field in the DOM.
18///
19/// This extends the API of `std::cell::RefCell` to allow unsafe access in
20/// certain situations, with dynamic checking in debug builds.
21#[derive(Clone, Debug, Default, MallocSizeOf, PartialEq)]
22pub(crate) struct DomRefCell<T> {
23 value: RefCell<T>,
24}
25
26impl<T: MallocConditionalSizeOf> MallocConditionalSizeOf for DomRefCell<T> {
27 fn conditional_size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
28 self.value.borrow().conditional_size_of(ops)
29 }
30}
31
32// Functionality specific to Servo's `DomRefCell` type
33// ===================================================
34
35impl<T> DomRefCell<T> {
36 /// Return a reference to the contents. For use in layout only.
37 ///
38 /// # Safety
39 ///
40 /// Unlike RefCell::borrow, this method is unsafe because it does not return a Ref, thus leaving
41 /// the borrow flag untouched. Mutably borrowing the RefCell while the reference returned by
42 /// this method is alive is undefined behaviour.
43 ///
44 /// # Panics
45 ///
46 /// Panics if this is called from anywhere other than the layout thread
47 ///
48 /// Panics if the value is currently mutably borrowed.
49 #[allow(unsafe_code)]
50 pub(crate) unsafe fn borrow_for_layout(&self) -> &T {
51 assert_in_layout();
52 unsafe {
53 self.value
54 .try_borrow_unguarded()
55 .expect("cell is mutably borrowed")
56 }
57 }
58
59 /// Borrow the contents for the purpose of script deallocation.
60 ///
61 /// # Safety
62 ///
63 /// Unlike RefCell::borrow, this method is unsafe because it does not return a Ref, thus leaving
64 /// the borrow flag untouched. Mutably borrowing the RefCell while the reference returned by
65 /// this method is alive is undefined behaviour.
66 ///
67 /// # Panics
68 ///
69 /// Panics if this is called from anywhere other than the script thread.
70 #[allow(unsafe_code, clippy::mut_from_ref)]
71 pub(crate) unsafe fn borrow_for_script_deallocation(&self) -> &mut T {
72 assert_in_script();
73 unsafe { &mut *self.value.as_ptr() }
74 }
75
76 /// Mutably borrow a cell for layout. Ideally this would use
77 /// `RefCell::try_borrow_mut_unguarded` but that doesn't exist yet.
78 ///
79 /// # Safety
80 ///
81 /// Unlike RefCell::borrow, this method is unsafe because it does not return a Ref, thus leaving
82 /// the borrow flag untouched. Mutably borrowing the RefCell while the reference returned by
83 /// this method is alive is undefined behaviour.
84 ///
85 /// # Panics
86 ///
87 /// Panics if this is called from anywhere other than the layout thread.
88 #[allow(unsafe_code, clippy::mut_from_ref)]
89 pub(crate) unsafe fn borrow_mut_for_layout(&self) -> &mut T {
90 assert_in_layout();
91 unsafe { &mut *self.value.as_ptr() }
92 }
93}
94
95// Functionality duplicated with `std::cell::RefCell`
96// ===================================================
97impl<T> DomRefCell<T> {
98 /// Create a new `DomRefCell` containing `value`.
99 pub(crate) fn new(value: T) -> DomRefCell<T> {
100 DomRefCell {
101 value: RefCell::new(value),
102 }
103 }
104
105 /// Immutably borrows the wrapped value.
106 ///
107 /// The borrow lasts until the returned `Ref` exits scope. Multiple
108 /// immutable borrows can be taken out at the same time.
109 ///
110 /// # Panics
111 ///
112 /// Panics if the value is currently mutably borrowed.
113 #[track_caller]
114 pub(crate) fn borrow(&self) -> Ref<'_, T> {
115 self.value.borrow()
116 }
117
118 /// Mutably borrows the wrapped value.
119 ///
120 /// The borrow lasts until the returned `RefMut` exits scope. The value
121 /// cannot be borrowed while this borrow is active.
122 ///
123 /// # Panics
124 ///
125 /// Panics if the value is currently borrowed.
126 #[track_caller]
127 pub(crate) fn borrow_mut(&self) -> RefMut<'_, T> {
128 self.value.borrow_mut()
129 }
130
131 /// Attempts to immutably borrow the wrapped value.
132 ///
133 /// The borrow lasts until the returned `Ref` exits scope. Multiple
134 /// immutable borrows can be taken out at the same time.
135 ///
136 /// Returns `None` if the value is currently mutably borrowed.
137 ///
138 /// # Panics
139 ///
140 /// Panics if this is called off the script thread.
141 pub(crate) fn try_borrow(&self) -> Result<Ref<'_, T>, BorrowError> {
142 assert_in_script();
143 self.value.try_borrow()
144 }
145
146 /// Mutably borrows the wrapped value.
147 ///
148 /// The borrow lasts until the returned `RefMut` exits scope. The value
149 /// cannot be borrowed while this borrow is active.
150 ///
151 /// Returns `None` if the value is currently borrowed.
152 ///
153 /// # Panics
154 ///
155 /// Panics if this is called off the script thread.
156 pub(crate) fn try_borrow_mut(&self) -> Result<RefMut<'_, T>, BorrowMutError> {
157 assert_in_script();
158 self.value.try_borrow_mut()
159 }
160}
161
162impl<T: Default> DomRefCell<T> {
163 /// Takes the wrapped value, leaving `Default::default()` in its place.
164 ///
165 /// # Panics
166 ///
167 /// Panics if the value is currently borrowed.
168 pub(crate) fn take(&self) -> T {
169 self.value.take()
170 }
171}