aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTetsuharu OHZEKI <saneyuki.snyk@gmail.com>2014-10-15 02:34:03 +0900
committerTetsuharu OHZEKI <saneyuki.snyk@gmail.com>2014-10-15 13:41:03 +0900
commit80593d9cc5917ee5c7cb5aa5b62b92adcb3e1db7 (patch)
tree68ca36096b7143ae3035e140c12c4a51bb8edd85
parentf1c050531f3e3669275f1fc50d92724609eba59b (diff)
downloadservo-80593d9cc5917ee5c7cb5aa5b62b92adcb3e1db7.tar.gz
servo-80593d9cc5917ee5c7cb5aa5b62b92adcb3e1db7.zip
Add DOMRefCell<T> for safe borrowing in layout.
This type simply wraps `RefCell<T>` to add the special method, and introduce the method to return the pointer of the value contained in itself, for used in layout task.
-rw-r--r--components/script/dom/bindings/cell.rs68
-rw-r--r--components/script/lib.rs1
2 files changed, 69 insertions, 0 deletions
diff --git a/components/script/dom/bindings/cell.rs b/components/script/dom/bindings/cell.rs
new file mode 100644
index 00000000000..a4a15b7d5e7
--- /dev/null
+++ b/components/script/dom/bindings/cell.rs
@@ -0,0 +1,68 @@
+/* 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 http://mozilla.org/MPL/2.0/. */
+
+use dom::bindings::trace::JSTraceable;
+use js::jsapi::{JSTracer};
+
+use std::cell;
+use std::cell::RefCell;
+use std::mem;
+
+/// A mutable field in DOM for large sized value.
+/// This has a special method to return the pointer of itself
+/// for used in layout task.
+/// This simply wraps `RefCell<T>` to add the special method.
+pub struct DOMRefCell<T> {
+ base: RefCell<T>,
+}
+
+pub type Ref<'a, T> = cell::Ref<'a, T>;
+pub type RefMut<'a, T> = cell::RefMut<'a, T>;
+
+
+impl<T> DOMRefCell<T> {
+ #[inline(always)]
+ pub fn new(value: T) -> DOMRefCell<T> {
+ DOMRefCell {
+ base: RefCell::new(value),
+ }
+ }
+
+ #[inline(always)]
+ pub fn unwrap(self) -> T {
+ self.base.unwrap()
+ }
+
+ #[inline(always)]
+ pub fn try_borrow<'a>(&'a self) -> Option<Ref<'a, T>> {
+ self.base.try_borrow()
+ }
+
+ #[inline(always)]
+ pub fn borrow<'a>(&'a self) -> Ref<'a, T> {
+ self.base.borrow()
+ }
+
+ #[inline(always)]
+ pub fn try_borrow_mut<'a>(&'a self) -> Option<RefMut<'a, T>> {
+ self.base.try_borrow_mut()
+ }
+
+ #[inline(always)]
+ pub fn borrow_mut<'a>(&'a self) -> RefMut<'a, T> {
+ self.base.borrow_mut()
+ }
+
+ /// This returns the pointer which refers T in `RefCell<T>` directly.
+ pub unsafe fn borrow_for_layout<'a>(&'a self) -> &'a T {
+ let val = mem::transmute::<&RefCell<T>, &T>(&self.base);
+ val
+ }
+}
+
+impl<T: JSTraceable> JSTraceable for DOMRefCell<T> {
+ fn trace(&self, trc: *mut JSTracer) {
+ (*self).base.borrow().trace(trc)
+ }
+}
diff --git a/components/script/lib.rs b/components/script/lib.rs
index 09bea1b73bd..03662371e0c 100644
--- a/components/script/lib.rs
+++ b/components/script/lib.rs
@@ -56,6 +56,7 @@ pub mod dom {
/// The code to expose the DOM to JavaScript through IDL bindings.
pub mod bindings {
+ pub mod cell;
pub mod global;
pub mod js;
pub mod utils;