pub struct DomRefCell<T> {
value: RefCell<T>,
}Expand description
A mutable field in the DOM.
This extends the API of std::cell::RefCell to allow unsafe access in
certain situations, with dynamic checking in debug builds.
Fields§
§value: RefCell<T>Implementations§
Source§impl<T> DomRefCell<T>
impl<T> DomRefCell<T>
Sourcepub unsafe fn borrow_for_layout(&self) -> &T
pub unsafe fn borrow_for_layout(&self) -> &T
Return a reference to the contents. For use in layout only.
§Safety
Unlike RefCell::borrow, this method is unsafe because it does not return a Ref, thus leaving the borrow flag untouched. Mutably borrowing the RefCell while the reference returned by this method is alive is undefined behaviour.
§Panics
Panics if this is called from anywhere other than the layout thread
Panics if the value is currently mutably borrowed.
Sourcepub unsafe fn borrow_for_script_deallocation(&self) -> &mut T
pub unsafe fn borrow_for_script_deallocation(&self) -> &mut T
Borrow the contents for the purpose of script deallocation.
§Safety
Unlike RefCell::borrow, this method is unsafe because it does not return a Ref, thus leaving the borrow flag untouched. Mutably borrowing the RefCell while the reference returned by this method is alive is undefined behaviour.
§Panics
Panics if this is called from anywhere other than the script thread.
Sourcepub unsafe fn borrow_mut_for_layout(&self) -> &mut T
pub unsafe fn borrow_mut_for_layout(&self) -> &mut T
Mutably borrow a cell for layout. Ideally this would use
RefCell::try_borrow_mut_unguarded but that doesn’t exist yet.
§Safety
Unlike RefCell::borrow, this method is unsafe because it does not return a Ref, thus leaving the borrow flag untouched. Mutably borrowing the RefCell while the reference returned by this method is alive is undefined behaviour.
§Panics
Panics if this is called from anywhere other than the layout thread.
Sourcepub fn safe_borrow_mut<'a: 'r, 'no_cx: 'r, 'r>(
&'a self,
_no_gc: &'no_cx NoGC,
) -> RefMut<'r, T>
pub fn safe_borrow_mut<'a: 'r, 'no_cx: 'r, 'r>( &'a self, _no_gc: &'no_cx NoGC, ) -> RefMut<'r, T>
Mutably borrows the wrapped value.
The borrow lasts until the returned RefMut exits scope. The value
cannot be borrowed while this borrow is active.
By passing a &NoGC we statically prevent GC from being run while the borrow is active,
to prevent panic when tracing (which calls borrow).
§Example
In simple cases one can use NoGC to statically ensure no GC can happen in the whole DOM method:
use js::context::{JSContext, NoGC};
use script_bindings::cell::DomRefCell;
fn DomMethod(no_gc: &NoGC, cell: &DomRefCell<usize>) {
let mut mutably_borrowed = cell.safe_borrow_mut(no_gc);
}But in more complex cases, method might trigger a GC, and thus require a &mut JSContext.
In that case &JSContext can be used in place of NoGC,
which will make RefMut bounded to the lifetime of the &JSContext
and thus prevent any GC from happening while it is alive.
use js::context::{JSContext, NoGC};
use script_bindings::cell::DomRefCell;
fn GC(cx: &mut JSContext) {}
fn DomMethod(cell: &DomRefCell<usize>, cx: &mut JSContext) {
{
let mut mutably_borrowed = cell.safe_borrow_mut(cx);
// do something with mutably_borrowed
// only &JSContext is available here
} // mutably_borrowed goes out of scope here
// so one can now use &mut JSContext
GC(cx);
}use js::context::{JSContext, NoGC};
use script_bindings::cell::DomRefCell;
fn GC(cx: &mut JSContext) {}
fn DomMethod(cell: &DomRefCell<usize>, cx: &mut JSContext) {
{
let mut mutably_borrowed = cell.safe_borrow_mut(cx);
// do something with mutably_borrowed
// here one cannot use anything that might trigger a GC
// as that would require &mut JSContext
// but there is already existing &JSContext bounded at RefMut
GC(cx);
} // mutably_borrowed goes out of scope here
}§Panics
Panics if the value is currently borrowed.
Sourcepub fn safe_try_borrow_mut<'a: 'r, 'no_cx: 'r, 'r>(
&'a self,
_no_gc: &'no_cx NoGC,
) -> Result<RefMut<'r, T>, BorrowMutError>
pub fn safe_try_borrow_mut<'a: 'r, 'no_cx: 'r, 'r>( &'a self, _no_gc: &'no_cx NoGC, ) -> Result<RefMut<'r, T>, BorrowMutError>
Mutably borrows the wrapped value.
The borrow lasts until the returned RefMut exits scope. The value
cannot be borrowed while this borrow is active.
By passing a &NoGC we statically prevent GC from being run while the borrow is active,
to prevent panic when tracing (which calls borrow).
Returns None if the value is currently borrowed.
Source§impl<T> DomRefCell<T>
impl<T> DomRefCell<T>
Sourcepub fn new(value: T) -> DomRefCell<T>
pub fn new(value: T) -> DomRefCell<T>
Create a new DomRefCell containing value.
Sourcepub fn borrow(&self) -> Ref<'_, T>
pub fn borrow(&self) -> Ref<'_, T>
Immutably borrows the wrapped value.
The borrow lasts until the returned Ref exits scope. Multiple
immutable borrows can be taken out at the same time.
§Panics
Panics if the value is currently mutably borrowed. Panics if this is called from anywhere other than the script thread. Use borrow_for_layout if the borrowed data might used during layout.
Sourcepub fn borrow_mut(&self) -> RefMut<'_, T>
pub fn borrow_mut(&self) -> RefMut<'_, T>
Mutably borrows the wrapped value.
The borrow lasts until the returned RefMut exits scope. The value
cannot be borrowed while this borrow is active.
§Panics
Panics if the value is currently borrowed. Panics if this is called from anywhere other than the script thread.
Sourcepub fn try_borrow(&self) -> Result<Ref<'_, T>, BorrowError>
pub fn try_borrow(&self) -> Result<Ref<'_, T>, BorrowError>
Attempts to immutably borrow the wrapped value.
The borrow lasts until the returned Ref exits scope. Multiple
immutable borrows can be taken out at the same time.
Returns None if the value is currently mutably borrowed.
§Panics
Panics if this is called off the script thread.
Sourcepub fn try_borrow_mut(&self) -> Result<RefMut<'_, T>, BorrowMutError>
pub fn try_borrow_mut(&self) -> Result<RefMut<'_, T>, BorrowMutError>
Mutably borrows the wrapped value.
The borrow lasts until the returned RefMut exits scope. The value
cannot be borrowed while this borrow is active.
Returns None if the value is currently borrowed.
§Panics
Panics if this is called off the script thread.
Trait Implementations§
Source§impl<T: Clone> Clone for DomRefCell<T>
impl<T: Clone> Clone for DomRefCell<T>
Source§fn clone(&self) -> DomRefCell<T>
fn clone(&self) -> DomRefCell<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T: CustomTraceable> CustomTraceable for DomRefCell<T>
impl<T: CustomTraceable> CustomTraceable for DomRefCell<T>
Source§impl<T: Debug> Debug for DomRefCell<T>
impl<T: Debug> Debug for DomRefCell<T>
Source§impl<T: Default> Default for DomRefCell<T>
impl<T: Default> Default for DomRefCell<T>
Source§fn default() -> DomRefCell<T>
fn default() -> DomRefCell<T>
Source§impl<T: MallocConditionalSizeOf> MallocConditionalSizeOf for DomRefCell<T>
impl<T: MallocConditionalSizeOf> MallocConditionalSizeOf for DomRefCell<T>
Source§fn conditional_size_of(&self, ops: &mut MallocSizeOfOps) -> usize
fn conditional_size_of(&self, ops: &mut MallocSizeOfOps) -> usize
Source§impl<T> MallocSizeOf for DomRefCell<T>where
T: MallocSizeOf,
impl<T> MallocSizeOf for DomRefCell<T>where
T: MallocSizeOf,
Source§fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize
Source§impl<K, V> Maplike for DomRefCell<IndexMap<K, V>>
impl<K, V> Maplike for DomRefCell<IndexMap<K, V>>
fn get_index( &self, _cx: &mut JSContext, index: u32, ) -> Option<(Self::Key, Self::Value)>
fn get(&self, _cx: &mut JSContext, key: Self::Key) -> Option<Self::Value>
fn size(&self, _cx: &mut JSContext) -> u32
fn set(&self, _cx: &mut JSContext, key: Self::Key, value: Self::Value)
fn has(&self, _cx: &mut JSContext, key: Self::Key) -> bool
fn clear(&self, _cx: &mut JSContext)
fn delete(&self, _cx: &mut JSContext, key: Self::Key) -> bool
Source§impl<T: PartialEq> PartialEq for DomRefCell<T>
impl<T: PartialEq> PartialEq for DomRefCell<T>
Source§impl<K> Setlike for DomRefCell<IndexSet<K>>
impl<K> Setlike for DomRefCell<IndexSet<K>>
fn get_index(&self, _cx: &mut JSContext, index: u32) -> Option<Self::Key>
fn size(&self, _cx: &mut JSContext) -> u32
fn add(&self, _cx: &mut JSContext, key: Self::Key)
fn has(&self, _cx: &mut JSContext, key: Self::Key) -> bool
fn clear(&self, _cx: &mut JSContext)
fn delete(&self, _cx: &mut JSContext, key: Self::Key) -> bool
Source§impl<T: Traceable> Traceable for DomRefCell<T>
impl<T: Traceable> Traceable for DomRefCell<T>
impl<T> StructuralPartialEq for DomRefCell<T>
Auto Trait Implementations§
impl<T> !Freeze for DomRefCell<T>
impl<T> !RefUnwindSafe for DomRefCell<T>
impl<T> Send for DomRefCell<T>where
T: Send,
impl<T> !Sync for DomRefCell<T>
impl<T> Unpin for DomRefCell<T>where
T: Unpin,
impl<T> UnsafeUnpin for DomRefCell<T>where
T: UnsafeUnpin,
impl<T> UnwindSafe for DomRefCell<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Filterable for T
impl<T> Filterable for T
Source§fn filterable(
self,
filter_name: &'static str,
) -> RequestFilterDataProvider<T, fn(DataRequest<'_>) -> bool>
fn filterable( self, filter_name: &'static str, ) -> RequestFilterDataProvider<T, fn(DataRequest<'_>) -> bool>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more