pub struct Atomic<T>(_, _);
Expand description

Atomic Box<T>

It can’t provide a reference to the current value since it may be dropped at any time

You must swap the element to access it

FillOnceAtomicOption provides a API that enables access to the reference, but only enables try_store to write to it

Implementations

Creates new Atomic

let filled = Atomic::new(10);
assert_eq!(*filled.into_inner(), 10);

Stores value into Atomic and drops old one

use std::sync::atomic::Ordering;
let filled = Atomic::from(10);
filled.store(5, Ordering::SeqCst);
assert_eq!(*filled.into_inner(), 5);

Stores value into Atomic returning old value

use std::sync::atomic::Ordering;
let option = Atomic::from(10);
assert_eq!(*option.swap(4, Ordering::SeqCst), 10);
assert_eq!(*option.into_inner(), 4);

Converts itself into a Box<T>

let ten = Atomic::from(10);
assert_eq!(*ten.into_inner(), 10);

Creates new Atomic if pointer is not null (like NonNull)

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::ptr::null_mut;
let empty: Option<Atomic<()>> = unsafe { Atomic::from_raw(null_mut()) };
assert!(empty.is_none());

let filled = unsafe { Atomic::from_raw(Box::into_raw(Box::new(10))) };
assert_eq!(filled.map(|a| *a.into_inner()), Some(10));

Creates new Atomic based on raw pointer without checking for null pointer

Safety

Unsafe because it trusts that the pointer is not null and because it can’t be sure of the origin of T (and ownership)

You must own the pointer to call this

let filled = unsafe { Atomic::from_raw_unchecked(Box::into_raw(Box::new(10))) };
assert_eq!(*filled.into_inner(), 10);

// It's UB for `ptr` to be `null_mut()`
// let empty = unsafe { Atomic::<()>::from_raw_unchecked(null_mut()) };

Atomically extracts the current stored pointer, this function should probably not be called

Safety

It’s almost never safe to deref this value, it could have been dropped from the moment you extracted it to the moment you deref/access it in any way, it will cause UB

It exists to provide a way of implementing safe wrappers (like FillOnceAtomicOption)

use std::{sync::atomic::Ordering, ptr::null_mut};

let ptr = Box::into_raw(Box::new(10u8));
let filled = unsafe { Atomic::from_raw(ptr) };
assert_eq!(filled.map(|a| a.get_raw(Ordering::SeqCst)), Some(ptr));

// You should probably never deref `ptr`
// You should probably never use this method
// UB will be everywhere, FillOnceAtomicOption is a safe an alternative

Trait Implementations

Formats the value using the given formatter. Read more
Executes the destructor for this 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.
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.