pub struct FillOnceAtomicOption<T>(_);
Expand description
Atomic abstraction of a Option<Box<T>>
that can provide access to a Option<&T>
This is ideal for a iterator or some consumer that doesn’t actually consume the data
Implementations
sourceimpl<T> FillOnceAtomicOption<T>
impl<T> FillOnceAtomicOption<T>
sourcepub fn new(data: Option<Box<T>>) -> Self
pub fn new(data: Option<Box<T>>) -> Self
Creates new FillOnceAtomicOption
let empty = FillOnceAtomicOption::<()>::default();
assert_eq!(empty.get_ref(Ordering::SeqCst), None);
let filled = FillOnceAtomicOption::new(Some(10.into()));
assert_eq!(filled.get_ref(Ordering::SeqCst), Some(&10));
sourcepub fn try_store(&self, data: Box<T>, order: Ordering) -> Result<(), NotEmpty>
pub fn try_store(&self, data: Box<T>, order: Ordering) -> Result<(), NotEmpty>
Stores new value if FillOnceAtomicOption
was not initialized (contains a None
)
This operation is implemented as a single atomic compare_and_swap
.
let option = FillOnceAtomicOption::default();
let old = option.try_store(5.into(), Ordering::SeqCst);
assert!(old.is_ok());
assert_eq!(option.get_ref(Ordering::SeqCst), Some(&5));
let failed_to_store = option.try_store(10.into(), Ordering::SeqCst);
assert!(failed_to_store.is_err());
assert_eq!(option.get_ref(Ordering::SeqCst), Some(&5));
sourceimpl<T: Copy> FillOnceAtomicOption<T>
impl<T: Copy> FillOnceAtomicOption<T>
sourceimpl<T> FillOnceAtomicOption<T>
impl<T> FillOnceAtomicOption<T>
sourcepub fn get_ref(&self, order: Ordering) -> Option<&T>
pub fn get_ref(&self, order: Ordering) -> Option<&T>
Atomically extracts a reference to the element stored
let empty = FillOnceAtomicOption::<()>::new(None);
assert_eq!(empty.get_ref(Ordering::SeqCst), None);
let filled = FillOnceAtomicOption::from(10);
assert_eq!(filled.get_ref(Ordering::SeqCst), Some(&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 = FillOnceAtomicOption::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 FillOnceAtomicOption
based on a 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 { FillOnceAtomicOption::<()>::from_raw(null_mut()) };
assert_eq!(empty.get_ref(Ordering::SeqCst), None);
let filled = unsafe { FillOnceAtomicOption::from_raw(Box::into_raw(Box::new(10))) };
assert_eq!(filled.get_ref(Ordering::SeqCst), Some(&10));
sourcepub fn get_raw(&self, order: Ordering) -> *mut T
pub fn get_raw(&self, order: Ordering) -> *mut T
Atomically extracts the stored pointer
If pointer returned is not null it’s safe to deref as long as you don’t drop the FillOnceAtomicOption
Safety
To deref it you must ensure that it’s not null
, the FillOnceAtomicOption
wasn’t dropped
Returns null
if FillOnceAtomicOption
is empty (was not initialized or dropped)
let empty = FillOnceAtomicOption::<()>::new(None);
assert_eq!(empty.get_raw(Ordering::SeqCst), null_mut());
let filled = FillOnceAtomicOption::from(10);
assert_eq!(unsafe { (&*filled.get_raw(Ordering::SeqCst)).deref() }, &10);
sourcepub unsafe fn dangle(&mut self) -> Option<Box<T>>
pub unsafe fn dangle(&mut self) -> Option<Box<T>>
Empties FillOnceAtomicOption
, this function should probably never be called
You should probably use into_inner
Safety
This is extremely unsafe, you don’t want to call this unless you are implementing Drop
for a chained T
All reference will endup invalidated and any function call other than try_store
(or dropping) will cause UB
In a multi-thread environment it’s very hard to ensure that this won’t happen
This is useful to obtain ownership of the inner value and implement a custom drop
(like a linked list iteratively dropped - VS
)