pub struct AtomicOptionDuration(/* private fields */);Expand description
Atomic version of Option<Duration>.
Implementations§
Source§impl AtomicOptionDuration
impl AtomicOptionDuration
Sourcepub const fn new(duration: Option<Duration>) -> Self
pub const fn new(duration: Option<Duration>) -> Self
Creates a new AtomicOptionDuration with the given value.
Sourcepub fn swap(
&self,
val: Option<Duration>,
ordering: Ordering,
) -> Option<Duration>
pub fn swap( &self, val: Option<Duration>, ordering: Ordering, ) -> Option<Duration>
Stores a value into the AtomicOptionDuration, returning the old value.
swap takes an Ordering argument which describes the memory ordering
of this operation.
Sourcepub fn compare_exchange_weak(
&self,
current: Option<Duration>,
new: Option<Duration>,
success: Ordering,
failure: Ordering,
) -> Result<Option<Duration>, Option<Duration>>
pub fn compare_exchange_weak( &self, current: Option<Duration>, new: Option<Duration>, success: Ordering, failure: Ordering, ) -> Result<Option<Duration>, Option<Duration>>
Stores a value into the AtomicOptionDuration if the current value is the same as the
current value.
Unlike compare_exchange, this function is allowed to spuriously fail
even when the comparison succeeds, which can result in more efficient
code on some platforms. The return value is a result indicating whether
the new value was written and containing the previous value.
compare_exchange takes two Ordering arguments to describe the memory
ordering of this operation. The first describes the required ordering if
the operation succeeds while the second describes the required ordering
when the operation fails. The failure ordering can’t be Release or
AcqRel and must be equivalent or weaker than the success ordering.
success ordering.
Sourcepub fn compare_exchange(
&self,
current: Option<Duration>,
new: Option<Duration>,
success: Ordering,
failure: Ordering,
) -> Result<Option<Duration>, Option<Duration>>
pub fn compare_exchange( &self, current: Option<Duration>, new: Option<Duration>, success: Ordering, failure: Ordering, ) -> Result<Option<Duration>, Option<Duration>>
Stores a value into the AtomicOptionDuration if the current value is the same as the
current value.
The return value is a result indicating whether the new value was
written and containing the previous value. On success this value is
guaranteed to be equal to current.
compare_exchange takes two Ordering arguments to describe the memory
ordering of this operation. The first describes the required ordering if
the operation succeeds while the second describes the required ordering
when the operation fails. The failure ordering can’t be Release or
AcqRel and must be equivalent or weaker than the success ordering.
Sourcepub fn try_update<F>(
&self,
set_order: Ordering,
fetch_order: Ordering,
f: F,
) -> Result<Option<Duration>, Option<Duration>>
pub fn try_update<F>( &self, set_order: Ordering, fetch_order: Ordering, f: F, ) -> Result<Option<Duration>, Option<Duration>>
Fetches the value and applies a function that can choose whether to store a new value.
Returns Ok(previous_value) when f returns Some(_), and
Err(previous_value) when it returns None. The outer Option is the
update decision; returning Some(None) stores None. The closure can run
more than once when another thread changes the value concurrently, but it
is applied only once to the value that is stored.
set_order describes the ordering of the successful update and
fetch_order describes failed compare-and-exchange loads. They have the
same requirements as the success and failure orderings of
compare_exchange.
§Panics
Panics if the orderings are invalid for
compare_exchange.
§Examples
use atomic_time::AtomicOptionDuration;
use std::{sync::atomic::Ordering, time::Duration};
let x = AtomicOptionDuration::new(Some(Duration::from_secs(7)));
assert_eq!(x.try_update(Ordering::SeqCst, Ordering::SeqCst, |_| None), Err(Some(Duration::from_secs(7))));
assert_eq!(x.try_update(Ordering::SeqCst, Ordering::SeqCst, |_| Some(None)), Ok(Some(Duration::from_secs(7))));Sourcepub fn fetch_update<F>(
&self,
set_order: Ordering,
fetch_order: Ordering,
f: F,
) -> Result<Option<Duration>, Option<Duration>>
pub fn fetch_update<F>( &self, set_order: Ordering, fetch_order: Ordering, f: F, ) -> Result<Option<Duration>, Option<Duration>>
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).
Note: This may call the function multiple times if the value has been changed from other threads in
the meantime, as long as the function returns Some(_), but the function will have been applied
only once to the stored value.
fetch_update takes two Ordering arguments to describe the memory ordering of this operation.
The first describes the required ordering for when the operation finally succeeds while the second
describes the required ordering for loads. These correspond to the success and failure orderings of
compare_exchange respectively.
Using Acquire as success ordering makes the store part
of this operation Relaxed, and using Release makes the final successful load
Relaxed. The (failed) load ordering can only be SeqCst, Acquire or Relaxed
and must be equivalent to or weaker than the success ordering.
§Examples
use atomic_time::AtomicOptionDuration;
use std::{time::Duration, sync::atomic::Ordering};
let x = AtomicOptionDuration::new(Some(Duration::from_secs(7)));
assert_eq!(x.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |_| None), Err(Some(Duration::from_secs(7))));
assert_eq!(x.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |x| Some(x.map(|val| val + Duration::from_secs(1)))), Ok(Some(Duration::from_secs(7))));
assert_eq!(x.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |x| Some(x.map(|val| val + Duration::from_secs(1)))), Ok(Some(Duration::from_secs(8))));
assert_eq!(x.load(Ordering::SeqCst), Some(Duration::from_secs(9)));Sourcepub fn update<F>(
&self,
set_order: Ordering,
fetch_order: Ordering,
f: F,
) -> Option<Duration>
pub fn update<F>( &self, set_order: Ordering, fetch_order: Ordering, f: F, ) -> Option<Duration>
Fetches the value, applies a function to produce a new value, stores it, and returns the previous value.
The closure can run more than once when another thread changes the value
concurrently, but it is applied only once to the value that is stored.
set_order and fetch_order have the same requirements as the success
and failure orderings of compare_exchange.
§Panics
Panics if the orderings are invalid for
compare_exchange.
§Examples
use atomic_time::AtomicOptionDuration;
use std::{sync::atomic::Ordering, time::Duration};
let x = AtomicOptionDuration::new(Some(Duration::from_secs(7)));
assert_eq!(x.update(Ordering::SeqCst, Ordering::SeqCst, |_| None), Some(Duration::from_secs(7)));
assert_eq!(x.load(Ordering::SeqCst), None);Sourcepub fn fetch_min(
&self,
val: Option<Duration>,
order: Ordering,
) -> Option<Duration>
pub fn fetch_min( &self, val: Option<Duration>, order: Ordering, ) -> Option<Duration>
Atomically stores the smaller of the current value and val, returning
the previous value.
The encoded ordering is None < Some(duration), and Some values use
their natural Duration ordering. order describes the memory ordering
of the read-modify-write operation.
§Examples
use atomic_time::AtomicOptionDuration;
use std::{sync::atomic::Ordering, time::Duration};
let x = AtomicOptionDuration::new(Some(Duration::from_secs(7)));
assert_eq!(x.fetch_min(None, Ordering::SeqCst), Some(Duration::from_secs(7)));
assert_eq!(x.load(Ordering::SeqCst), None);Sourcepub fn fetch_max(
&self,
val: Option<Duration>,
order: Ordering,
) -> Option<Duration>
pub fn fetch_max( &self, val: Option<Duration>, order: Ordering, ) -> Option<Duration>
Atomically stores the larger of the current value and val, returning
the previous value.
The encoded ordering is None < Some(duration), and Some values use
their natural Duration ordering. order describes the memory ordering
of the read-modify-write operation.
§Examples
use atomic_time::AtomicOptionDuration;
use std::{sync::atomic::Ordering, time::Duration};
let x = AtomicOptionDuration::new(None);
assert_eq!(x.fetch_max(Some(Duration::from_secs(7)), Ordering::SeqCst), None);
assert_eq!(x.load(Ordering::SeqCst), Some(Duration::from_secs(7)));Sourcepub fn fetch_saturating_add(
&self,
val: Duration,
order: Ordering,
) -> Option<Duration>
pub fn fetch_saturating_add( &self, val: Duration, order: Ordering, ) -> Option<Duration>
Adds val to a contained duration with saturation, returning the previous
value.
Some(duration) is replaced with Some(duration.saturating_add(val)).
None remains None; it is never treated as Duration::ZERO.
§Examples
use atomic_time::AtomicOptionDuration;
use std::{sync::atomic::Ordering, time::Duration};
let x = AtomicOptionDuration::none();
assert_eq!(x.fetch_saturating_add(Duration::from_nanos(1), Ordering::SeqCst), None);
assert_eq!(x.load(Ordering::SeqCst), None);Sourcepub fn fetch_saturating_sub(
&self,
val: Duration,
order: Ordering,
) -> Option<Duration>
pub fn fetch_saturating_sub( &self, val: Duration, order: Ordering, ) -> Option<Duration>
Subtracts val from a contained duration with saturation, returning the
previous value.
Some(duration) is replaced with Some(duration.saturating_sub(val)).
None remains None; it is never treated as Duration::ZERO.
§Examples
use atomic_time::AtomicOptionDuration;
use std::{sync::atomic::Ordering, time::Duration};
let x = AtomicOptionDuration::none();
assert_eq!(x.fetch_saturating_sub(Duration::from_nanos(1), Ordering::SeqCst), None);
assert_eq!(x.load(Ordering::SeqCst), None);Sourcepub fn into_inner(self) -> Option<Duration>
pub fn into_inner(self) -> Option<Duration>
Consumes the atomic and returns the contained value.
This is safe because passing self by value guarantees that no other threads are
concurrently accessing the atomic data.
Sourcepub fn is_lock_free() -> bool
pub fn is_lock_free() -> bool
Returns true if operations on values of this type are lock-free.
If the compiler or the platform doesn’t support the necessary
atomic instructions, global locks for every potentially
concurrent atomic operation will be used.
§Examples
use atomic_time::AtomicOptionDuration;
let is_lock_free = AtomicOptionDuration::is_lock_free();Sourcepub const fn is_always_lock_free() -> bool
pub const fn is_always_lock_free() -> bool
Returns whether operations on values of this type are always lock-free.
A false result does not preclude lock-free operations selected through
runtime CPU feature detection; use is_lock_free
to query the current target at runtime.
§Examples
use atomic_time::AtomicOptionDuration;
const ALWAYS_LOCK_FREE: bool = AtomicOptionDuration::is_always_lock_free();
let _ = ALWAYS_LOCK_FREE;Trait Implementations§
Source§impl Arbitrary for AtomicOptionDuration
Available on crate feature quickcheck only.
impl Arbitrary for AtomicOptionDuration
quickcheck only.Source§impl Arbitrary for AtomicOptionDuration
Available on crate feature proptest only.
impl Arbitrary for AtomicOptionDuration
proptest only.Source§type Parameters = ()
type Parameters = ()
arbitrary_with accepts for configuration
of the generated Strategy. Parameters must implement Default.Source§type Strategy = BoxedStrategy<AtomicOptionDuration>
type Strategy = BoxedStrategy<AtomicOptionDuration>
Strategy used to generate values of type Self.Source§fn arbitrary_with(_: Self::Parameters) -> Self::Strategy
fn arbitrary_with(_: Self::Parameters) -> Self::Strategy
Source§impl<'a> Arbitrary<'a> for AtomicOptionDuration
Available on crate feature arbitrary only.
impl<'a> Arbitrary<'a> for AtomicOptionDuration
arbitrary only.Source§fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>
Self from the given unstructured data. Read moreSource§fn size_hint(depth: usize) -> (usize, Option<usize>)
fn size_hint(depth: usize) -> (usize, Option<usize>)
Unstructured this type
needs to construct itself. Read moreSource§fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
Self from the entirety of the given
unstructured data. Read moreSource§fn try_size_hint(
depth: usize,
) -> Result<(usize, Option<usize>), MaxRecursionReached>
fn try_size_hint( depth: usize, ) -> Result<(usize, Option<usize>), MaxRecursionReached>
Unstructured this type
needs to construct itself. Read moreSource§impl Clone for AtomicOptionDuration
Available on crate feature quickcheck only.
impl Clone for AtomicOptionDuration
quickcheck only.Source§fn clone(&self) -> Self
fn clone(&self) -> Self
Creates an atomic snapshot of this value with sequentially consistent ordering.
If another thread writes concurrently, the clone contains the value observed at this load’s atomic linearization point.
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for AtomicOptionDuration
impl Debug for AtomicOptionDuration
Source§impl Default for AtomicOptionDuration
impl Default for AtomicOptionDuration
Source§impl<'de> Deserialize<'de> for AtomicOptionDuration
Available on crate feature serde only.
impl<'de> Deserialize<'de> for AtomicOptionDuration
serde only.