Skip to main content

AtomicOptionDuration

Struct AtomicOptionDuration 

Source
pub struct AtomicOptionDuration(/* private fields */);
Expand description

Atomic version of Option<Duration>.

Implementations§

Source§

impl AtomicOptionDuration

Source

pub const fn none() -> Self

Creates a new AtomicOptionDuration with None.

Source

pub const fn new(duration: Option<Duration>) -> Self

Creates a new AtomicOptionDuration with the given value.

Source

pub fn load(&self, ordering: Ordering) -> Option<Duration>

Loads Option<Duration> from AtomicOptionDuration.

load takes an Ordering argument which describes the memory ordering of this operation.

§Panics

Panics if order is Release or AcqRel.

Source

pub fn store(&self, val: Option<Duration>, ordering: Ordering)

Stores a value into the AtomicOptionDuration.

store takes an Ordering argument which describes the memory ordering of this operation.

§Panics

Panics if order is Acquire or AcqRel.

Source

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.

Source

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.

Source

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.

Source

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))));
Source

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)));
Source

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);
Source

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);
Source

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)));
Source

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);
Source

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);
Source

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.

Source

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();
Source

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

fn arbitrary(g: &mut Gen) -> Self

Return an arbitrary value. Read more
Source§

fn shrink(&self) -> Box<dyn Iterator<Item = Self>>

Return an iterator of values that are smaller than itself. Read more
Source§

impl Arbitrary for AtomicOptionDuration

Available on crate feature proptest only.
Source§

type Parameters = ()

The type of parameters that arbitrary_with accepts for configuration of the generated Strategy. Parameters must implement Default.
Source§

type Strategy = BoxedStrategy<AtomicOptionDuration>

The type of Strategy used to generate values of type Self.
Source§

fn arbitrary_with(_: Self::Parameters) -> Self::Strategy

Generates a Strategy for producing arbitrary values of type the implementing type (Self). The strategy is passed the arguments given in args. Read more
Source§

fn arbitrary() -> Self::Strategy

Generates a Strategy for producing arbitrary values of type the implementing type (Self). Read more
Source§

impl<'a> Arbitrary<'a> for AtomicOptionDuration

Available on crate feature arbitrary only.
Source§

fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>

Generate an arbitrary value of Self from the given unstructured data. Read more
Source§

fn size_hint(depth: usize) -> (usize, Option<usize>)

Get a size hint for how many bytes out of an Unstructured this type needs to construct itself. Read more
Source§

fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>

Generate an arbitrary value of Self from the entirety of the given unstructured data. Read more
Source§

fn try_size_hint( depth: usize, ) -> Result<(usize, Option<usize>), MaxRecursionReached>

Get a size hint for how many bytes out of an Unstructured this type needs to construct itself. Read more
Source§

impl Clone for AtomicOptionDuration

Available on crate feature quickcheck only.
Source§

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)

Performs copy-assignment from source. Read more
Source§

impl Debug for AtomicOptionDuration

Source§

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

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

impl Default for AtomicOptionDuration

Source§

fn default() -> Self

Creates an AtomicOptionDuration initialized to None.

Source§

impl<'de> Deserialize<'de> for AtomicOptionDuration

Available on crate feature serde only.
Source§

fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>

Deserialize this value from the given Serde deserializer. Read more
Source§

impl From<Option<Duration>> for AtomicOptionDuration

Source§

fn from(duration: Option<Duration>) -> Self

Converts to this type from the input type.
Source§

impl Serialize for AtomicOptionDuration

Available on crate feature serde only.
Source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V