pub struct OptionGuard<'a, T> { /* private fields */ }
Expand description
An exclusive guard for the value of an OptionLock
Implementations§
Source§impl<T> OptionGuard<'_, T>
impl<T> OptionGuard<'_, T>
Sourcepub fn as_mut(&mut self) -> Option<&mut T>
pub fn as_mut(&mut self) -> Option<&mut T>
Obtain an exclusive reference to the contained value, if any.
Sourcepub fn replace(&mut self, value: T) -> Option<T>
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 }
Sourcepub fn take(&mut self) -> Option<T>
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>
impl<T: Debug> Debug for OptionGuard<'_, T>
Source§impl<'a, T> Drop for OptionGuard<'a, T>
impl<'a, T> Drop for OptionGuard<'a, T>
impl<T: Send> Send for OptionGuard<'_, T>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more