Add an ArcRefCell<T> type

This commit is contained in:
Patrick Walton 2020-03-13 19:45:54 -07:00
parent c3932185ec
commit 2ff776b241
2 changed files with 59 additions and 0 deletions

View file

@ -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)
}
}

View file

@ -11,6 +11,7 @@ extern crate log;
#[macro_use]
extern crate serde;
mod cell;
pub mod context;
pub mod data;
pub mod display_list;