AtomicSwap

Struct AtomicSwap 

Source
pub struct AtomicSwap<T> { /* private fields */ }
Expand description

Allows shared access to T by only swap related operations. Acts like it stores an Option<T>

use atomic_swapping::AtomicSwap;

let swap = AtomicSwap::new(100usize);
assert_eq!(swap.clone_inner(), 100usize);
assert_eq!(swap.swap(300usize), 100usize);
assert_eq!(swap.clone_inner(), 300usize);

Implementations§

Source§

impl<T> AtomicSwap<T>

Source

pub const fn new(value: T) -> Self

Creates a new AtomicSwap from a value.

let some_swap = AtomicSwap::new(100usize);
assert_eq!(some_swap.swap(200usize), 100usize);
Source

pub fn swap(&self, value: T) -> T

Swaps the current value in the swap with value, returning the currently contained value. Same as AtomicSwap::swap_hint with spin_loop as spin_loop_hint.

let swap = AtomicSwap::new(100usize);
assert_eq!(swap.swap(200usize), 100usize);
assert_eq!(swap.swap(300usize), 200usize);
Source

pub fn swap_hint(&self, value: T, spin_loop_hint: impl FnMut()) -> T

Swaps the current value in the swap with value, returning the currently contained value. Same as AtomicSwap::swap but with a custom spin loop hint function.

let swap = AtomicSwap::new(100usize);
let spin_hint = ||println!("I'm spinning! Probably should yield here.");
assert_eq!(swap.swap_hint(200usize, spin_hint), 100usize);
assert_eq!(swap.swap_hint(300usize, spin_hint), 200usize);
Source

pub fn clone_inner(&self) -> T
where T: Clone + Sync,

Clones the contained value if Some and returns it. T must be Clone and Sync because multiple threads could clone this simultaneously. Same as AtomicSwap::clone_inner_hint with spin_loop as spin_loop_hint.

let swap = AtomicSwap::new(100usize);
assert_eq!(swap.clone_inner(), 100usize);
assert_eq!(swap.swap(200usize), 100usize);
assert_eq!(swap.clone_inner(), 200usize);
Source

pub fn clone_inner_hint(&self, spin_loop_hint: impl FnMut()) -> T
where T: Clone + Sync,

Clones the contained value if Some and returns it. T must be Clone and Sync because multiple threads could clone this simultaneously. Same as AtomicSwap::clone_inner but with a custom spin loop hint function.

let swap = AtomicSwap::new(100usize);
let spin_hint = ||println!("I'm spinning! Probably should yield here.");
assert_eq!(swap.clone_inner_hint(spin_hint), 100usize);
assert_eq!(swap.swap_hint(200usize, spin_hint), 100usize);
assert_eq!(swap.clone_inner_hint(spin_hint), 200usize);
Source

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

Gets the internal value exclusively.

let mut swap = AtomicSwap::new(100);
assert_eq!(swap.get_mut(), &mut 100usize);
*swap.get_mut() = 200usize;
assert_eq!(swap.swap(300usize), 200usize);
assert_eq!(swap.get_mut(), &mut 300usize);

Trait Implementations§

Source§

impl<T: Debug> Debug for AtomicSwap<T>

Source§

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

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

impl<T> Default for AtomicSwap<T>
where T: Default,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T> Drop for AtomicSwap<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T> Sync for AtomicSwap<T>
where T: Send,

Auto Trait Implementations§

§

impl<T> !Freeze for AtomicSwap<T>

§

impl<T> !RefUnwindSafe for AtomicSwap<T>

§

impl<T> Send for AtomicSwap<T>
where T: Send,

§

impl<T> Unpin for AtomicSwap<T>
where T: Unpin,

§

impl<T> UnwindSafe for AtomicSwap<T>
where T: UnwindSafe,

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, 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.