aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPatrick Walton <pcwalton@mimiga.net>2020-03-13 19:45:54 -0700
committerPatrick Walton <pcwalton@mimiga.net>2020-03-17 11:15:17 -0700
commit2ff776b241f847bff9053e08a8004b6c7edc48b0 (patch)
tree946ffc1ca5d40d0c72574adaa89ac78c3c5e4e47
parentc3932185ec649c3063096ca0c545b62c3ddbfd5b (diff)
downloadservo-2ff776b241f847bff9053e08a8004b6c7edc48b0.tar.gz
servo-2ff776b241f847bff9053e08a8004b6c7edc48b0.zip
Add an `ArcRefCell<T>` type
-rw-r--r--components/layout_2020/cell.rs58
-rw-r--r--components/layout_2020/lib.rs1
2 files changed, 59 insertions, 0 deletions
diff --git a/components/layout_2020/cell.rs b/components/layout_2020/cell.rs
new file mode 100644
index 00000000000..e12201e4b28
--- /dev/null
+++ b/components/layout_2020/cell.rs
@@ -0,0 +1,58 @@
+/* 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/. */
+
+use atomic_refcell::AtomicRefCell;
+use serde::{Serialize, Serializer};
+use servo_arc::Arc;
+use std::fmt;
+use std::ops::Deref;
+
+pub(crate) struct ArcRefCell<T> {
+ value: Arc<AtomicRefCell<T>>,
+}
+
+impl<T> ArcRefCell<T> {
+ pub fn new(value: T) -> Self {
+ Self {
+ value: Arc::new(AtomicRefCell::new(value)),
+ }
+ }
+}
+
+impl<T> Clone for ArcRefCell<T> {
+ fn clone(&self) -> Self {
+ Self {
+ value: self.value.clone(),
+ }
+ }
+}
+
+impl<T> Deref for ArcRefCell<T> {
+ type Target = AtomicRefCell<T>;
+
+ fn deref(&self) -> &Self::Target {
+ &self.value
+ }
+}
+
+impl<T> fmt::Debug for ArcRefCell<T>
+where
+ T: fmt::Debug,
+{
+ fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ self.value.fmt(formatter)
+ }
+}
+
+impl<T> Serialize for ArcRefCell<T>
+where
+ T: Serialize,
+{
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ self.borrow().serialize(serializer)
+ }
+}
diff --git a/components/layout_2020/lib.rs b/components/layout_2020/lib.rs
index c7d260f0f89..3fc093399ee 100644
--- a/components/layout_2020/lib.rs
+++ b/components/layout_2020/lib.rs
@@ -11,6 +11,7 @@ extern crate log;
#[macro_use]
extern crate serde;
+mod cell;
pub mod context;
pub mod data;
pub mod display_list;