Struct voluntary_servitude::AtomicOption
source · pub struct AtomicOption<T>(_, _);
Expand description
Atomic abstraction of a Option<Box<T>>
Implementations
sourceimpl<T> AtomicOption<T>
impl<T> AtomicOption<T>
sourcepub fn new(value: Option<Box<T>>) -> Self
pub fn new(value: Option<Box<T>>) -> Self
Creates new AtomicOption
let empty = AtomicOption::<()>::new(None);
assert!(empty.into_inner().is_none());
let filled = AtomicOption::from(10);
assert_eq!(filled.into_inner().map(|a| *a), Some(10));
sourcepub fn try_store(&self, new: Box<T>, order: Ordering) -> Result<(), NotEmpty>
pub fn try_store(&self, new: Box<T>, order: Ordering) -> Result<(), NotEmpty>
Stores new value if AtomicOption
currently contains a None
This operation is implemented as a single atomic compare_and_swap
.
let option = AtomicOption::default();
let old = option.try_store(5.into(), Ordering::SeqCst);
assert!(old.is_ok());
let failed_to_store = option.try_store(10.into(), Ordering::SeqCst);
assert!(failed_to_store.is_err());
assert_eq!(option.into_inner().map(|a| *a), Some(5));
sourcepub fn store(&self, new: Option<Box<T>>, order: Ordering)
pub fn store(&self, new: Option<Box<T>>, order: Ordering)
Stores value into AtomicOption
and drops old one
let empty = AtomicOption::<()>::new(None);
assert!(empty.into_inner().is_none());
let filled = AtomicOption::from(10);
assert_eq!(filled.into_inner().map(|a| *a), Some(10));
sourcepub fn swap(&self, new: Option<Box<T>>, order: Ordering) -> Option<Box<T>>
pub fn swap(&self, new: Option<Box<T>>, order: Ordering) -> Option<Box<T>>
Stores value into AtomicOption
returning old value
let option = AtomicOption::default();
assert_eq!(option.swap(Some(5.into()), Ordering::SeqCst), None);
assert_eq!(option.swap(None, Ordering::SeqCst), Some(Box::new(5)));
assert_eq!(option.swap(Some(3.into()), Ordering::SeqCst), None);
sourcepub fn take(&self, order: Ordering) -> Option<Box<T>>
pub fn take(&self, order: Ordering) -> Option<Box<T>>
Replaces AtomicOption
value for None
returning old value
let option = AtomicOption::from(5);
assert_eq!(option.take(Ordering::SeqCst), Some(Box::new(5)));
assert_eq!(option.take(Ordering::SeqCst), None);
assert_eq!(option.take(Ordering::SeqCst), None);
sourcepub unsafe fn atomic_ptr(&self) -> &AtomicPtr<T>
pub unsafe fn atomic_ptr(&self) -> &AtomicPtr<T>
Gives access to inner AtomicPtr
(AtomicOption
is an abstraction of it)
Safety
It’s unsafe because you are responsible for making sure T
is not dropped
nor replaced with a invalid pointer (or that will be invalidated while still stored)
let ten = AtomicOption::from(10);
assert_eq!(unsafe { &*ten.atomic_ptr().load(Ordering::SeqCst) }, &10);
sourcepub fn into_inner(self) -> Option<Box<T>>
pub fn into_inner(self) -> Option<Box<T>>
Converts itself into a Option<Box<T>>
let ten = AtomicOption::from(10);
assert_eq!(ten.into_inner().map(|a| *a), Some(10));
sourcepub unsafe fn from_raw(ptr: *mut T) -> Self
pub unsafe fn from_raw(ptr: *mut T) -> Self
Creates new AtomicOption
based on raw pointer
Safety
Unsafe because it uses a raw pointer, so it can’t be sure of its origin (and ownership)
You must own the pointer to call this
let empty = unsafe { AtomicOption::<()>::from_raw(null_mut()) };
assert!(empty.into_inner().is_none());
let filled = unsafe { AtomicOption::from_raw(Box::into_raw(10.into())) };
assert_eq!(filled.into_inner().map(|a| *a), Some(10));
sourcepub fn get_raw(&self, order: Ordering) -> *mut T
pub fn get_raw(&self, order: Ordering) -> *mut T
Atomically extracts current pointer stored, this function should probably not be called
Safety
It’s almost never safe to deref this value, it could have been dropped from the moment you extracted it to the moment you deref/access it in any way, it will cause UB
It exists to provide a way of implementing safe wrappers (like FillOnceAtomicOption
)
let empty = AtomicOption::<()>::new(None);
assert_eq!(empty.get_raw(Ordering::SeqCst), null_mut());
let ptr = Box::into_raw(Box::new(10u8));
let filled = unsafe { AtomicOption::from_raw(ptr) };
assert_eq!(filled.get_raw(Ordering::SeqCst), ptr);
// You should probably never deref `ptr`
// You should probably never use this method
// UB will be everywhere, FillOnceAtomicOption is a safe an alternative