From 2ff776b241f847bff9053e08a8004b6c7edc48b0 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Fri, 13 Mar 2020 19:45:54 -0700 Subject: [PATCH] Add an `ArcRefCell` type --- components/layout_2020/cell.rs | 58 ++++++++++++++++++++++++++++++++++ components/layout_2020/lib.rs | 1 + 2 files changed, 59 insertions(+) create mode 100644 components/layout_2020/cell.rs 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 { + value: Arc>, +} + +impl ArcRefCell { + pub fn new(value: T) -> Self { + Self { + value: Arc::new(AtomicRefCell::new(value)), + } + } +} + +impl Clone for ArcRefCell { + fn clone(&self) -> Self { + Self { + value: self.value.clone(), + } + } +} + +impl Deref for ArcRefCell { + type Target = AtomicRefCell; + + fn deref(&self) -> &Self::Target { + &self.value + } +} + +impl fmt::Debug for ArcRefCell +where + T: fmt::Debug, +{ + fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + self.value.fmt(formatter) + } +} + +impl Serialize for ArcRefCell +where + T: Serialize, +{ + fn serialize(&self, serializer: S) -> Result + 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;