rw_cell/
option.rs

1use std::sync::atomic::{AtomicPtr, Ordering};
2
3pub struct OptionCell<T> {
4    inner: AtomicPtr<Option<T>>
5}
6
7impl<T> OptionCell<T> {
8    pub fn new(val: T) -> Self {
9        Self {
10            inner: AtomicPtr::new(some_raw_ptr(val)),
11        }
12    }
13
14    pub fn replace(&self, val: T) -> Option<T> {
15        let val_ptr = Box::into_raw(Box::new(Some(val)));
16        let old_val_ptr = self.inner.swap(val_ptr, Ordering::SeqCst);
17        *unsafe { Box::from_raw(old_val_ptr) }
18    }
19
20    pub fn take(&self) -> Option<T> {
21        let curr_val = self.inner.swap(none_raw_ptr(), Ordering::SeqCst);
22        *unsafe { Box::from_raw(curr_val) }
23    }
24}
25
26impl<T> Drop for OptionCell<T> {
27    fn drop(&mut self) {
28        let _ = unsafe { Box::from_raw(self.inner.load(Ordering::SeqCst)) };
29    }
30}
31
32impl<T> Default for OptionCell<T> {
33    fn default() -> Self {
34        Self { inner: AtomicPtr::new(none_raw_ptr()) }
35    }
36}
37
38#[inline]
39pub(crate) fn none_raw_ptr<T>() -> *mut Option<T> {
40    Box::into_raw(Box::new(None))
41}
42
43#[inline]
44pub(crate) fn some_raw_ptr<T>(val: T) -> *mut Option<T> {
45    Box::into_raw(Box::new(Some(val)))
46}
47
48#[cfg(test)]
49mod tests {
50    use crate::option::OptionCell;
51
52    #[test]
53    fn test_option_cell_new() {
54        let cell = OptionCell::new("aa");
55        assert_eq!(Some("aa"), cell.take());
56        assert_eq!(None, cell.replace("bb"));
57        assert_eq!(Some("bb"), cell.take());
58    }
59    #[test]
60    fn test_option_cell_default() {
61        let cell = OptionCell::default();
62        assert_eq!(None, cell.replace("bb"));
63        assert_eq!(Some("bb"), cell.take());
64    }
65}