Skip to main content

rust_css_parser/ast/
column.rs

1//! Interior mutable column of the arena.
2//!
3//! The arena follows the shared mutable model of the typescript implementation: the columns are read and written
4//! through `&self` (visitors written in javascript re-enter the arena while a transformation runs). A column is a
5//! `Vec` in an `UnsafeCell`. Soundness: no reference into the vector ever escapes these methods (values are copied in
6//! and out), and the arena is `!Sync` (single threaded), so a growth never invalidates a live borrow.
7
8use std::cell::UnsafeCell;
9
10pub struct Column<T: Copy + Default> {
11    data: UnsafeCell<Vec<T>>,
12    /// the buffers replaced by a growth: the javascript typed array views of a column keep reading valid memory until
13    /// they are refreshed (like a typed array replaced by a larger one). dropped with the column
14    retired: UnsafeCell<Vec<Vec<T>>>,
15}
16
17impl<T: Copy + Default> Column<T> {
18    pub fn new(size: usize) -> Self {
19        Self { data: UnsafeCell::new(vec![T::default(); size]), retired: UnsafeCell::new(Vec::new()) }
20    }
21
22    pub fn from_vec(data: Vec<T>) -> Self {
23        Self { data: UnsafeCell::new(data), retired: UnsafeCell::new(Vec::new()) }
24    }
25
26    #[inline(always)]
27    fn vec(&self) -> &Vec<T> {
28        // SAFETY: see the module documentation
29        unsafe { &*self.data.get() }
30    }
31
32    #[inline(always)]
33    #[allow(clippy::mut_from_ref)]
34    fn vec_mut(&self) -> &mut Vec<T> {
35        // SAFETY: see the module documentation
36        unsafe { &mut *self.data.get() }
37    }
38
39    /// value of a slot (`column[id]`). out of range reads return the default value (`undefined` -> 0)
40    #[inline(always)]
41    pub fn get(&self, id: u32) -> T {
42        self.vec().get(id as usize).copied().unwrap_or_default()
43    }
44
45    /// write a slot (`column[id] = value`). out of range writes are ignored, like typed arrays
46    #[inline(always)]
47    pub fn set(&self, id: u32, value: T) {
48        if let Some(slot) = self.vec_mut().get_mut(id as usize) {
49            *slot = value;
50        }
51    }
52
53    #[inline(always)]
54    pub fn len(&self) -> usize {
55        self.vec().len()
56    }
57
58    #[inline(always)]
59    pub fn is_empty(&self) -> bool {
60        self.vec().is_empty()
61    }
62
63    /// grow the column, the new slots are zeroed. `retire`: javascript may hold views of the column, the previous
64    /// buffer is kept until the column is dropped. otherwise it is freed
65    pub fn resize(&self, size: usize, retire: bool) {
66        let vec = self.vec_mut();
67        if size <= vec.capacity() {
68            vec.resize(size, T::default());
69            return;
70        }
71        // a zeroed allocation (the default values are zeros): the pages that are not written are not resident. the
72        // values are copied, the free slots are not written
73        let mut grown = vec![T::default(); size];
74        grown[..vec.len()].copy_from_slice(vec);
75        let previous = std::mem::replace(vec, grown);
76        if retire {
77            // the javascript views may still point to the previous buffer
78            // SAFETY: see the module documentation
79            unsafe { (*self.retired.get()).push(previous) };
80        }
81    }
82
83    /// replace the data
84    pub fn replace(&self, data: Vec<T>) -> Vec<T> {
85        std::mem::replace(self.vec_mut(), data)
86    }
87
88    /// copy of the data
89    pub fn to_vec(&self) -> Vec<T> {
90        self.vec().clone()
91    }
92
93    /// pointer to the data, for the javascript typed array views. valid until the next resize / replace
94    pub fn as_ptr(&self) -> *const T {
95        self.vec().as_ptr()
96    }
97
98    /// mutable pointer to the data, for the javascript typed array views
99    pub fn as_mut_ptr(&self) -> *mut T {
100        self.vec_mut().as_mut_ptr()
101    }
102}