[][src]Struct voluntary_servitude::atomics::Atomic

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

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

Methods

impl<T> Atomic<T>[src]

pub fn new<V>(data: V) -> Self where
    V: Into<Box<T>>, 
[src]

Creates new Atomic

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

pub fn store<V>(&self, new: V, order: Ordering) where
    V: Into<Box<T>>, 
[src]

Stores value into Atomic and drops old one

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

pub fn swap<V>(&self, new: V, order: Ordering) -> Box<T> where
    V: Into<Box<T>>, 
[src]

Stores value into Atomic returning old value

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

pub fn into_inner(self) -> Box<T>[src]

Converts itself into a Box<T>

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

pub unsafe fn from_raw(ptr: *mut T) -> Option<Self>[src]

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

pub unsafe fn from_raw_unchecked(ptr: *mut T) -> Self[src]

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

pub fn get_raw(&self, order: Ordering) -> *mut T[src]

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::Relaxed)), Some(ptr));

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

Trait Implementations

impl<T> From<T> for Atomic<T>[src]

impl<T> From<Box<T>> for Atomic<T>[src]

impl<T> From<Atomic<T>> for AtomicOption<T>[src]

impl<T> From<Atomic<T>> for FillOnceAtomicOption<T>[src]

impl<T> Drop for Atomic<T>[src]

impl<T: Debug> Debug for Atomic<T>[src]

impl<T> Pointer for Atomic<T>[src]

Auto Trait Implementations

impl<T> Send for Atomic<T> where
    T: Send

impl<T> Sync for Atomic<T> where
    T: Sync

Blanket Implementations

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T> From for T[src]

impl<T, U> TryFrom for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Erased for T