Struct OptionLock

Source
pub struct OptionLock<T> { /* private fields */ }
Expand description

A read/write lock around an Option value.

Implementations§

Source§

impl<T> OptionLock<T>

Source

pub const fn empty() -> Self

Create a new instance with no stored value.

Examples found in repository?
examples/atomic-wake.rs (line 16)
14    pub const fn new() -> Self {
15        Self {
16            state: OptionLock::empty(),
17        }
18    }
Source

pub const fn new(value: T) -> Self

Create a new populated instance.

Source

pub fn is_none_unlocked(&self) -> bool

Check if there is no stored value and no guard held.

Source

pub fn is_some_unlocked(&self) -> bool

Check if there is a stored value and no guard held.

Source

pub fn is_locked(&self) -> bool

Check if a guard is held.

Source

pub fn get_mut(&mut self) -> Option<&mut T>

Get a mutable reference to the contained value, if any.

Source

pub fn into_inner(self) -> Option<T>

Unwrap an owned lock instance.

Source

pub fn spin_get(&self) -> MutexGuard<'_, T>

In a spin loop, wait to get an exclusive lock on the contained value.

Source

pub fn spin_lock(&self) -> OptionGuard<'_, T>

In a spin loop, wait to acquire the lock.

Source

pub fn spin_lock_none(&self) -> OptionGuard<'_, T>

In a spin loop, wait to acquire the lock with an empty slot.

Source

pub fn spin_take(&self) -> T

In a spin loop, wait to take a value from the lock.

Source

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.

Source

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.

Source

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.

Examples found in repository?
examples/sync-results.rs (line 35)
34    pub fn return_result(&self, index: usize, value: T) {
35        if let Ok(()) = self.data[index].try_fill(value) {
36            self.completed.fetch_add(1, Ordering::Release);
37        } else {
38            panic!("Update conflict");
39        }
40    }
Source

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.

Source

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?
examples/atomic-wake.rs (line 21)
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    }
Source

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.

Source

pub fn try_lock_none(&self) -> Result<OptionGuard<'_, T>, OptionLockError>

Try to acquire an exclusive lock when there is no value currently stored.

Source

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.

Source

pub fn try_take(&self) -> Result<T, OptionLockError>

Try to take a stored value from the lock.

Source

pub fn replace(&mut self, value: T) -> Option<T>

Replace the value in an owned OptionLock.

Source

pub fn take(&mut self) -> Option<T>

Take the value (if any) from an owned OptionLock.

Examples found in repository?
examples/sync-results.rs (line 50)
46    fn next(&mut self) -> Option<Self::Item> {
47        let idx = self.iter_index;
48        if idx < self.data.len() {
49            self.iter_index += 1;
50            Some(self.data[idx].take().unwrap())
51        } else {
52            None
53        }
54    }
Source§

impl<T: Clone> OptionLock<T>

Source

pub fn try_clone(&self) -> Result<T, OptionLockError>

Try to clone the contained resource.

Source§

impl<T: Copy> OptionLock<T>

Source

pub fn try_copy(&self) -> Result<T, OptionLockError>

Try to copy the contained resource.

Trait Implementations§

Source§

impl<T> Debug for OptionLock<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Default for OptionLock<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T> Drop for OptionLock<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T> From<Option<T>> for OptionLock<T>

Source§

fn from(data: Option<T>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<OptionLock<T>> for OnceCell<T>

Source§

fn from(lock: OptionLock<T>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<T> for OptionLock<T>

Source§

fn from(data: T) -> Self

Converts to this type from the input type.
Source§

impl<T> Into<Option<T>> for OptionLock<T>

Source§

fn into(self) -> Option<T>

Converts this type into the (usually inferred) input type.
Source§

impl<T: Send> Send for OptionLock<T>

Source§

impl<T: Send> Sync for OptionLock<T>

Auto Trait Implementations§

§

impl<T> !Freeze for OptionLock<T>

§

impl<T> !RefUnwindSafe for OptionLock<T>

§

impl<T> Unpin for OptionLock<T>
where T: Unpin,

§

impl<T> UnwindSafe for OptionLock<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.