aboutsummaryrefslogtreecommitdiff
path: root/src/util/vec_deque_cell.rs
blob: b4e124c1519e5a79b1fe677437383a89f387e000 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use crate::loom::cell::UnsafeCell;

use std::collections::VecDeque;
use std::marker::PhantomData;

/// This type is like VecDeque, except that it is not Sync and can be modified
/// through immutable references.
pub(crate) struct VecDequeCell<T> {
    inner: UnsafeCell<VecDeque<T>>,
    _not_sync: PhantomData<*const ()>,
}

// This is Send for the same reasons that RefCell<VecDeque<T>> is Send.
unsafe impl<T: Send> Send for VecDequeCell<T> {}

impl<T> VecDequeCell<T> {
    pub(crate) fn with_capacity(cap: usize) -> Self {
        Self {
            inner: UnsafeCell::new(VecDeque::with_capacity(cap)),
            _not_sync: PhantomData,
        }
    }

    /// Safety: This method may not be called recursively.
    #[inline]
    unsafe fn with_inner<F, R>(&self, f: F) -> R
    where
        F: FnOnce(&mut VecDeque<T>) -> R,
    {
        // safety: This type is not Sync, so concurrent calls of this method
        // cannot happen. Furthermore, the caller guarantees that the method is
        // not called recursively. Finally, this is the only place that can
        // create mutable references to the inner VecDeque. This ensures that
        // any mutable references created here are exclusive.
        self.inner.with_mut(|ptr| f(&mut *ptr))
    }

    pub(crate) fn pop_front(&self) -> Option<T> {
        unsafe { self.with_inner(VecDeque::pop_front) }
    }

    pub(crate) fn push_back(&self, item: T) {
        unsafe {
            self.with_inner(|inner| inner.push_back(item));
        }
    }

    /// Replaces the inner VecDeque with an empty VecDeque and return the current
    /// contents.
    pub(crate) fn take(&self) -> VecDeque<T> {
        unsafe { self.with_inner(|inner| std::mem::take(inner)) }
    }
}