pub struct OptionLock<T> { /* private fields */ }
Expand description
A read/write lock around an Option
value.
Implementations§
Source§impl<T> OptionLock<T>
impl<T> OptionLock<T>
Sourcepub fn is_none_unlocked(&self) -> bool
pub fn is_none_unlocked(&self) -> bool
Check if there is no stored value and no guard held.
Sourcepub fn is_some_unlocked(&self) -> bool
pub fn is_some_unlocked(&self) -> bool
Check if there is a stored value and no guard held.
Sourcepub fn get_mut(&mut self) -> Option<&mut T>
pub fn get_mut(&mut self) -> Option<&mut T>
Get a mutable reference to the contained value, if any.
Sourcepub fn into_inner(self) -> Option<T>
pub fn into_inner(self) -> Option<T>
Unwrap an owned lock instance.
Sourcepub fn spin_get(&self) -> MutexGuard<'_, T>
pub fn spin_get(&self) -> MutexGuard<'_, T>
In a spin loop, wait to get an exclusive lock on the contained value.
Sourcepub fn spin_lock(&self) -> OptionGuard<'_, T>
pub fn spin_lock(&self) -> OptionGuard<'_, T>
In a spin loop, wait to acquire the lock.
Sourcepub fn spin_lock_none(&self) -> OptionGuard<'_, T>
pub fn spin_lock_none(&self) -> OptionGuard<'_, T>
In a spin loop, wait to acquire the lock with an empty slot.
Sourcepub fn try_get(&self) -> Result<MutexGuard<'_, T>, OptionLockError>
pub fn try_get(&self) -> Result<MutexGuard<'_, T>, OptionLockError>
Try to acquire an exclusive lock around a contained value.
On successful acquisition a MutexGuard<'_, T>
is returned, representing
an exclusive read/write lock.
Sourcepub fn try_get_arc(
self: &Arc<Self>,
) -> Result<MutexGuardArc<T>, OptionLockError>
pub fn try_get_arc( self: &Arc<Self>, ) -> Result<MutexGuardArc<T>, OptionLockError>
Try to acquire an exclusive lock around the value in an Arc<OptionLock>
.
On successful acquisition a MutexGuardArc<T>
is returned, representing
an exclusive read/write lock.
Sourcepub fn try_fill(&self, value: T) -> Result<(), T>
pub fn try_fill(&self, value: T) -> Result<(), T>
Try to store a value, if the slot is currently empty and a lock can be acquired.
Sourcepub fn try_fill_with(
&self,
f: impl FnOnce() -> T,
) -> Result<(), OptionLockError>
pub fn try_fill_with( &self, f: impl FnOnce() -> T, ) -> Result<(), OptionLockError>
Store the result of an initializer function if the slot is currently empty and a lock can be acquired. If a lock cannot be acquired, then the initializer is never called.
Sourcepub fn try_lock(&self) -> Result<OptionGuard<'_, T>, OptionLockError>
pub fn try_lock(&self) -> Result<OptionGuard<'_, T>, OptionLockError>
Try to acquire an exclusive lock.
On successful acquisition an OptionGuard<'_, T>
is returned, representing
an exclusive read/write lock.
Examples found in repository?
20 pub fn poll(&self, waker: Option<&Waker>) -> Option<Result<T, E>> {
21 match self.state.try_lock() {
22 Ok(mut guard) => match guard.take() {
23 Some(ResultState::Ready(result)) => Some(result),
24 Some(ResultState::Wake(_)) | None => {
25 waker.map(|waker| guard.replace(ResultState::Wake(waker.clone())));
26 None
27 }
28 },
29 _ => {
30 // result is currently being stored
31 waker.map(Waker::wake_by_ref);
32 None
33 }
34 }
35 }
36
37 pub fn fulfill(&self, result: Result<T, E>) -> Result<(), Result<T, E>> {
38 // retry method is left up to the caller (spin, yield thread, etc)
39 if let Ok(mut guard) = self.state.try_lock() {
40 let prev = guard.replace(ResultState::Ready(result));
41 drop(guard);
42 if let Some(ResultState::Wake(waker)) = prev {
43 waker.wake();
44 }
45 Ok(())
46 } else {
47 Err(result)
48 }
49 }
Sourcepub fn try_lock_arc(
self: &Arc<Self>,
) -> Result<OptionGuardArc<T>, OptionLockError>
pub fn try_lock_arc( self: &Arc<Self>, ) -> Result<OptionGuardArc<T>, OptionLockError>
Try to acquire an exclusive lock from a reference to an Arc<OptionLock>
.
On successful acquisition an OptionGuardArc<T>
is returned, representing
an exclusive read/write lock.
Sourcepub fn try_lock_none(&self) -> Result<OptionGuard<'_, T>, OptionLockError>
pub fn try_lock_none(&self) -> Result<OptionGuard<'_, T>, OptionLockError>
Try to acquire an exclusive lock when there is no value currently stored.
Sourcepub fn try_lock_empty_arc(
self: &Arc<Self>,
) -> Result<OptionGuardArc<T>, OptionLockError>
pub fn try_lock_empty_arc( self: &Arc<Self>, ) -> Result<OptionGuardArc<T>, OptionLockError>
Try to acquire an exclusive lock when there is no value currently stored.
Sourcepub fn try_take(&self) -> Result<T, OptionLockError>
pub fn try_take(&self) -> Result<T, OptionLockError>
Try to take a stored value from the lock.
Source§impl<T: Clone> OptionLock<T>
impl<T: Clone> OptionLock<T>
Sourcepub fn try_clone(&self) -> Result<T, OptionLockError>
pub fn try_clone(&self) -> Result<T, OptionLockError>
Try to clone the contained resource.
Source§impl<T: Copy> OptionLock<T>
impl<T: Copy> OptionLock<T>
Sourcepub fn try_copy(&self) -> Result<T, OptionLockError>
pub fn try_copy(&self) -> Result<T, OptionLockError>
Try to copy the contained resource.