Trait atomic_traits::fetch::Update
source · pub trait Update {
type Type;
// Required method
fn fetch_update<F>(
&self,
fetch_order: Ordering,
set_order: Ordering,
f: F
) -> Result<Self::Type, Self::Type>
where F: FnMut(Self::Type) -> Option<Self::Type>;
}
Expand description
Fetches the value, and applies a function to it that returns an optional new value.
Required Associated Types§
Required Methods§
sourcefn fetch_update<F>(
&self,
fetch_order: Ordering,
set_order: Ordering,
f: F
) -> Result<Self::Type, Self::Type>
fn fetch_update<F>( &self, fetch_order: Ordering, set_order: Ordering, f: F ) -> Result<Self::Type, Self::Type>
Fetches the value, and applies a function to it that returns an optional new value.
Returns a Result
of Ok(previous_value)
if the function returned Some(_)
, else Err(previous_value)
.
§Examples
use std::sync::atomic::{AtomicU8, Ordering};
use atomic_traits::{Atomic, fetch};
let x = AtomicU8::new(7);
assert_eq!(fetch::Update::fetch_update(&x, Ordering::SeqCst, Ordering::SeqCst, |_| None), Err(7));
assert_eq!(fetch::Update::fetch_update(&x, Ordering::SeqCst, Ordering::SeqCst, |x| Some(x + 1)), Ok(7));
assert_eq!(fetch::Update::fetch_update(&x, Ordering::SeqCst, Ordering::SeqCst, |x| Some(x + 1)), Ok(8));
assert_eq!(Atomic::load(&x, Ordering::SeqCst), 9);
Object Safety§
This trait is not object safe.