layout/
cell.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use std::fmt;
6use std::ops::Deref;
7
8use atomic_refcell::AtomicRefCell;
9use malloc_size_of_derive::MallocSizeOf;
10use servo_arc::Arc;
11
12#[derive(MallocSizeOf)]
13pub struct ArcRefCell<T> {
14    #[conditional_malloc_size_of]
15    value: Arc<AtomicRefCell<T>>,
16}
17
18impl<T> ArcRefCell<T> {
19    pub fn new(value: T) -> Self {
20        Self {
21            value: Arc::new(AtomicRefCell::new(value)),
22        }
23    }
24}
25
26impl<T> Clone for ArcRefCell<T> {
27    fn clone(&self) -> Self {
28        Self {
29            value: self.value.clone(),
30        }
31    }
32}
33
34impl<T> Default for ArcRefCell<T>
35where
36    T: Default,
37{
38    fn default() -> Self {
39        Self {
40            value: Arc::new(AtomicRefCell::new(Default::default())),
41        }
42    }
43}
44
45impl<T> Deref for ArcRefCell<T> {
46    type Target = AtomicRefCell<T>;
47
48    fn deref(&self) -> &Self::Target {
49        &self.value
50    }
51}
52
53impl<T> fmt::Debug for ArcRefCell<T>
54where
55    T: fmt::Debug,
56{
57    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
58        self.value.fmt(formatter)
59    }
60}