OptionGuard

Struct OptionGuard 

Source
pub struct OptionGuard<'a, T> { /* private fields */ }
Expand description

An exclusive guard for the value of an OptionLock

Implementations§

Source§

impl<T> OptionGuard<'_, T>

Source

pub fn as_ref(&self) -> Option<&T>

Obtain a shared reference to the contained value, if any.

Source

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

Obtain an exclusive reference to the contained value, if any.

Source

pub fn is_none(&self) -> bool

Check if the lock contains None.

Source

pub fn is_some(&self) -> bool

Check if the lock contains Some(T).

Source

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

Replace the value in the lock, returning the previous value, if any.

Examples found in repository?
examples/atomic-wake.rs (line 25)
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 take(&mut self) -> Option<T>

Take the current value from the lock, if any.

Examples found in repository?
examples/atomic-wake.rs (line 22)
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    }

Trait Implementations§

Source§

impl<T: Debug> Debug for OptionGuard<'_, T>

Source§

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

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

impl<'a, T> Drop for OptionGuard<'a, T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T: Send> Send for OptionGuard<'_, T>

Source§

impl<T: Sync> Sync for OptionGuard<'_, T>

Auto Trait Implementations§

§

impl<'a, T> Freeze for OptionGuard<'a, T>

§

impl<'a, T> !RefUnwindSafe for OptionGuard<'a, T>

§

impl<'a, T> Unpin for OptionGuard<'a, T>

§

impl<'a, T> !UnwindSafe for OptionGuard<'a, T>

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<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.