Skip to content

Commit

Permalink
Add an ArcRefCell<T> type
Browse files Browse the repository at this point in the history
  • Loading branch information
pcwalton committed Mar 17, 2020
1 parent c393218 commit 2ff776b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
58 changes: 58 additions & 0 deletions 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)
}
}
1 change: 1 addition & 0 deletions components/layout_2020/lib.rs
Expand Up @@ -11,6 +11,7 @@ extern crate log;
#[macro_use]
extern crate serde;

mod cell;
pub mod context;
pub mod data;
pub mod display_list;
Expand Down

0 comments on commit 2ff776b

Please sign in to comment.