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

Atomic abstractions of a Box<T>

Implementations

Inner swap, helper to swap Atomic values

Creates new Atomic

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

Stores value into Atomic and drops old one

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

Stores value into Atomic returning old value

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

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

let empty = 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)

let empty = unsafe { Atomic::<()>::from_raw_unchecked(null_mut()) };
assert_eq!(empty.get_raw(Ordering::SeqCst), 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

Empties Atomic, this function should probably never be called

You should probably use into_inner

Safety

This is extremely unsafe, you don’t want to call this unless you are implementing Drop for a chained T

All reference will endup invalidated and any function call other than try_store (or dropping) will cause UB

In a multi-thread environment it’s very hard to ensure that this won’t happen

This is useful to obtain ownership of the inner value and implement a custom drop (like a linked list iteratively dropped - VS)

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.

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.