/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ //! A shareable mutable container for the DOM. use std::cell::{BorrowError, BorrowMutError, Ref, RefCell, RefMut}; use style::thread_state::{self, ThreadState}; /// 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. #[derive(Clone, Debug, Default, MallocSizeOf, PartialEq)] pub struct DomRefCell { value: RefCell, } // Functionality specific to Servo's `DomRefCell` type // =================================================== impl DomRefCell { /// Return a reference to the contents. /// /// For use in the layout thread only. #[allow(unsafe_code)] pub unsafe fn borrow_for_layout(&self) -> &T { debug_assert!(thread_state::get().is_layout()); self.value .try_borrow_unguarded() .expect("cell is mutably borrowed") } /// Borrow the contents for the purpose of script deallocation. /// #[allow(unsafe_code)] pub unsafe fn borrow_for_script_deallocation(&self) -> &mut T { debug_assert!(thread_state::get().contains(ThreadState::SCRIPT)); &mut *self.value.as_ptr() } /// Version of the above that we use during restyle while the script thread /// is blocked. pub fn borrow_mut_for_layout(&self) -> RefMut { debug_assert!(thread_state::get().is_layout()); self.value.borrow_mut() } } // Functionality duplicated with `std::cell::RefCell` // =================================================== impl DomRefCell { /// Create a new `DomRefCell` containing `value`. pub fn new(value: T) -> DomRefCell { DomRefCell { value: RefCell::new(value), } } /// 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 this is called off the script thread. /// /// Panics if the value is currently mutably borrowed. pub fn borrow(&self) -> Ref { self.try_borrow() .expect("DomRefCell already mutably borrowed") } /// 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 this is called off the script thread. /// /// Panics if the value is currently borrowed. pub fn borrow_mut(&self) -> RefMut { self.try_borrow_mut() .expect("DomRefCell already borrowed") } /// 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. pub fn try_borrow(&self) -> Result, BorrowError> { debug_assert!(thread_state::get().is_script()); self.value.try_borrow() } /// 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. pub fn try_borrow_mut(&self) -> Result, BorrowMutError> { debug_assert!(thread_state::get().is_script()); self.value.try_borrow_mut() } }