Struct AtomicCell

Source
pub struct AtomicCell<T, A: Allocator = Global> { /* private fields */ }
Available on crate feature alloc only.
Expand description

An atomic cell that can be safely shared between threads and can contain an optional value.

AtomicCell provides methods to store, replace, and take values atomically, ensuring safe access and modification across multiple threads.

§Example

use utils_atomics::AtomicCell;

let mut atomic_cell = AtomicCell::new(Some(42));

std::thread::scope(|s| {
    // Spawn a thread that replaces the value inside the AtomicCell
    s.spawn(|| {
        let prev_value = atomic_cell.replace(Some(24));
        assert_eq!(prev_value, Some(42));
    });
});

// Check that the value was replaced
assert_eq!(atomic_cell.get_mut().copied(), Some(24));

Implementations§

Source§

impl<T, A: Allocator> AtomicCell<T, A>

Source

pub fn new_in(t: impl Into<Option<T>>, alloc: A) -> Self

Available on crate feature alloc_api only.

Constructs a new AtomicCell containing an optional value t and an allocator alloc.

If the value is Some(x), it is boxed using the allocator. If the value is None, an empty AtomicCell is created with the allocator.

§Example
#![feature(allocator_api)]

use utils_atomics::AtomicCell;
use std::alloc::System;

let atomic_cell = AtomicCell::<i32, _>::new_in(Some(42), System);
Source

pub fn new_boxed_in(t: Result<Box<T, A>, A>) -> Self

Available on crate feature alloc_api only.

Constructs a new AtomicCell from a boxed value or an allocator.

If the input is Ok(t), the AtomicCell contains the boxed value, and the allocator is extracted from the box. If the input is Err(alloc), an empty AtomicCell is created with the allocator.

§Example
#![feature(allocator_api)]
extern crate alloc;

use utils_atomics::AtomicCell;
use std::alloc::System;
use alloc::boxed::Box;
    
let atomic_cell = AtomicCell::new_boxed_in(Ok(Box::new_in(42, System)));
Source

pub fn allocator(&self) -> &A

Available on crate feature alloc_api only.

Returns a reference to the allocator associated with the AtomicCell.

§Example
#![feature(allocator_api)]

use utils_atomics::AtomicCell;
use std::alloc::System;

let atomic_cell = AtomicCell::<i32, System>::new_in(Some(42), System);
let allocator = atomic_cell.allocator();
Source

pub fn take_in(&self) -> Option<Box<T, &A>>

Available on crate feature alloc_api only.

Takes the value out of the AtomicCell, leaving it empty.

Returns an optional boxed value with a reference to the allocator. If the AtomicCell is empty, returns None.

§Example
#![feature(allocator_api)]

use utils_atomics::AtomicCell;
use std::alloc::System;

let atomic_cell = AtomicCell::new_in(Some(42), System);
let taken_value = atomic_cell.take_in();
assert_eq!(taken_value, Some(Box::new_in(42, &System)))
Source

pub fn replace_in(&self, new: impl Into<Option<T>>) -> Option<Box<T, &A>>

Available on crate feature alloc_api only.

Replaces the value inside the AtomicCell with a new optional value.

If the new value is Some(new), it is boxed using the allocator. If the new value is None, the AtomicCell is emptied.

Returns the old value as an optional boxed value with a reference to the allocator. If the AtomicCell was empty, returns None.

§Example
#![feature(allocator_api)]

use utils_atomics::AtomicCell;
use std::alloc::System;

let atomic_cell = AtomicCell::new_in(Some(42), System);
let old_value = atomic_cell.replace_in(Some(24));
assert_eq!(old_value, Some(Box::new_in(42, atomic_cell.allocator())));
assert_eq!(atomic_cell.take(), Some(24));
Source§

impl<T> AtomicCell<T>

Source

pub fn new(t: impl Into<Option<T>>) -> Self

Constructs a new AtomicCell containing an optional value t.

§Example
use utils_atomics::AtomicCell;

let atomic_cell = AtomicCell::<i32>::new(Some(42));
Source

pub fn new_boxed(t: impl Into<Option<Box<T>>>) -> Self

Constructs a new AtomicCell from an optional boxed value t.

§Example
extern crate alloc;

use utils_atomics::AtomicCell;
use alloc::boxed::Box;

let atomic_cell = AtomicCell::new_boxed(Some(Box::new(42)));
Source

pub fn replace(&self, new: impl Into<Option<T>>) -> Option<T>

Replaces the value inside the AtomicCell with a new optional value new. Returns the old value as an optional value. If the AtomicCell was empty, returns None.

§Example
use utils_atomics::AtomicCell;

let atomic_cell = AtomicCell::<i32>::new(Some(42));
let old_value = atomic_cell.replace(Some(24));
Source

pub fn replace_boxed(&self, new: impl Into<Option<Box<T>>>) -> Option<Box<T>>

Replaces the value inside the AtomicCell with a new optional boxed value new. Returns the old value as an optional boxed value. If the AtomicCell was empty, returns None.

§Example
extern crate alloc;

use utils_atomics::AtomicCell;
use alloc::boxed::Box;

let atomic_cell = AtomicCell::new_boxed(Some(Box::new(42)));
let old_value = atomic_cell.replace_boxed(Some(Box::new(24)));
Source

pub fn take_boxed(&self) -> Option<Box<T>>

Takes the value out of the AtomicCell, leaving it empty. Returns an optional boxed value. If the AtomicCell is empty, returns None.

§Example
use utils_atomics::AtomicCell;

let atomic_cell = AtomicCell::new_boxed(Some(Box::new(42)));
let taken_value = atomic_cell.take_boxed();
Source§

impl<T, A: Allocator> AtomicCell<T, A>

Source

pub fn take(&self) -> Option<T>

Takes the value out of the AtomicCell, leaving it empty. Returns an optional value. If the AtomicCell is empty, returns None.

§Examples
use utils_atomics::AtomicCell;

let atomic_cell = AtomicCell::new(Some(42));
assert_eq!(atomic_cell.take(), Some(42));
assert_eq!(atomic_cell.take(), None);
Source

pub fn get_mut(&mut self) -> Option<&mut T>

Returns a mutable reference to the value inside the AtomicCell, if any. If the AtomicCell is empty, returns None.

§Examples
use utils_atomics::AtomicCell;

let mut atomic_cell = AtomicCell::new(Some(42));
let value_ref = atomic_cell.get_mut().unwrap();
*value_ref = 24;
assert_eq!(*value_ref, 24);
Source

pub fn is_some(&self) -> bool

Returns true if the AtomicCell contains a value.

§Examples
use utils_atomics::AtomicCell;

let atomic_cell = AtomicCell::<i32>::new(Some(42));
assert!(atomic_cell.is_some());
Source

pub fn is_none(&self) -> bool

Returns true if the AtomicCell is empty.

§Examples
use utils_atomics::AtomicCell;

let atomic_cell = AtomicCell::<i32>::new(None);
assert!(atomic_cell.is_none());

Trait Implementations§

Source§

impl<T: Debug, A: Debug + Allocator> Debug for AtomicCell<T, A>

Source§

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

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

impl<T, A: Allocator> Drop for AtomicCell<T, A>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T: Send, A: Allocator + Send> Send for AtomicCell<T, A>

Source§

impl<T: Sync, A: Allocator + Sync> Sync for AtomicCell<T, A>

Auto Trait Implementations§

§

impl<T, A = Global> !Freeze for AtomicCell<T, A>

§

impl<T, A> RefUnwindSafe for AtomicCell<T, A>
where A: RefUnwindSafe,

§

impl<T, A> Unpin for AtomicCell<T, A>
where A: Unpin,

§

impl<T, A> UnwindSafe for AtomicCell<T, A>

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<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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.