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;
pub(crate) struct VecDequeCell<T> {
inner: UnsafeCell<VecDeque<T>>,
_not_sync: PhantomData<*const ()>,
}
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,
}
}
#[inline]
unsafe fn with_inner<F, R>(&self, f: F) -> R
where
F: FnOnce(&mut VecDeque<T>) -> R,
{
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));
}
}
pub(crate) fn take(&self) -> VecDeque<T> {
unsafe { self.with_inner(|inner| std::mem::take(inner)) }
}
}