pub struct FillOnceAtomicArc<T>(_);
Expand description

Atomic abstraction of a Option<Arc<T>> that can provide access to a cloned Option<Arc<T>> and a Option<&T>

Implementations

Creates new FillOnceAtomicArc

use std::sync::{Arc, atomic::Ordering};
let empty: FillOnceAtomicArc<()> = FillOnceAtomicArc::new(None);
assert_eq!(empty.get_ref(Ordering::SeqCst), None);

let filled = FillOnceAtomicArc::new(Arc::new(10));
assert_eq!(filled.get_ref(Ordering::SeqCst), Some(&10));

Stores new Arc<T> if FillOnceAtomicArc currently contains a None

This operation is implemented as a single atomic compare_and_swap.

use std::sync::atomic::Ordering;
let option = FillOnceAtomicArc::default();
let old = option.try_store(5, Ordering::SeqCst);
assert!(old.is_ok());
assert_eq!(option.get_ref(Ordering::SeqCst), Some(&5));

let failed_to_store = option.try_store(10, Ordering::SeqCst);
assert!(failed_to_store.is_err());
assert_eq!(option.get_ref(Ordering::SeqCst), Some(&5));

Atomically retrieves a cloned Option<Arc<T>>

use std::sync::atomic::Ordering;
let empty: FillOnceAtomicArc<()> = FillOnceAtomicArc::new(None);
assert_eq!(empty.load(Ordering::SeqCst), None);

let filled = FillOnceAtomicArc::from(10);
assert_eq!(filled.load(Ordering::SeqCst).map(|a| *a), Some(10));

Atomically extracts a reference to the element stored

use std::sync::atomic::Ordering;
let empty: FillOnceAtomicArc<()> = FillOnceAtomicArc::new(None);
assert_eq!(empty.get_ref(Ordering::SeqCst), None);

let filled = FillOnceAtomicArc::from(10);
assert_eq!(filled.get_ref(Ordering::SeqCst), Some(&10));

Converts itself into a Option<Arc<T>>

let ten = FillOnceAtomicArc::from(10);
assert_eq!(ten.into_inner().map(|a| *a), Some(10));

Creates new FillOnceAtomicArc based on a raw pointer

Safety

Unsafe because it uses a raw pointer, so it can’t be sure of its origin (and ownership)

You must own the pointer to call this

use std::{sync::Arc, sync::atomic::Ordering, ptr::null_mut};
let empty = unsafe { FillOnceAtomicArc::<()>::from_raw(null_mut()) };
assert_eq!(empty.get_ref(Ordering::SeqCst), None);

let ptr = Box::into_raw(Box::new(Arc::new(10)));
let filled = unsafe { FillOnceAtomicArc::from_raw(ptr) };
assert_eq!(filled.get_ref(Ordering::SeqCst), Some(&10));

Atomically extracts the stored pointer

If pointer returned is not null it’s safe to deref as long as you don’t drop the FillOnceAtomicArc

Safety

To deref it you must ensure that it’s not null, the FillOnceAtomicArc wasn’t dropped

Returns null if FillOnceAtomicArc is empty (was not initialized or dropped)

use std::{sync::atomic::Ordering, ptr::null_mut, ops::Deref};
let empty: FillOnceAtomicArc<()> = FillOnceAtomicArc::new(None);
assert_eq!(empty.get_raw(Ordering::SeqCst), null_mut());

let filled = FillOnceAtomicArc::from(10);
assert_eq!(unsafe { (&*filled.get_raw(Ordering::SeqCst)).deref().deref() }, &10);

Trait Implementations

Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Formats the value using the given formatter.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Converts to this type from the input type.

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Convert self to an expression for Diesel’s query builder. Read more
Convert &self to an expression for Diesel’s query builder. Read more
The alignment of pointer.
The type for initializers.
Initializes a with the given initializer. Read more
Dereferences the given pointer. Read more
Mutably dereferences the given pointer. Read more
Drops the object pointed to by the given pointer. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.