AtomicU16

Struct AtomicU16 

Source
#[repr(transparent)]
pub struct AtomicU16(AtomicU16);
Expand description

An integer type which can be safely shared between threads.

This type has the same size and bit validity as the underlying integer type, [u16]. However, the alignment of this type is always equal to its size, even on targets where [u16] has a lesser alignment.

For more about the differences between atomic types and non-atomic types as well as information about the portability of this type, please see the [module-level documentation].

Note: This type is only available on platforms that support atomic loads and stores of [u16].

Tuple Fields§

§0: AtomicU16

Implementations§

Source§

impl AtomicU16

Source

pub const fn new(v: u16) -> Self

Creates a new atomic integer.

§Examples
use keos::atomic::AtomicU16;

let atomic_forty_two = AtomicU16::new(42);
Source

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

Returns a mutable reference to the underlying integer.

This is safe because the mutable reference guarantees that no other threads are concurrently accessing the atomic data.

§Examples
use keos::atomic::AtomicU16;

let mut some_var = AtomicU16::new(10);
assert_eq!(*some_var.get_mut(), 10);
*some_var.get_mut() = 5;
assert_eq!(some_var.load(), 5);
Source

pub const fn into_inner(self) -> u16

Consumes the atomic and returns the contained value.

This is safe because passing self by value guarantees that no other threads are concurrently accessing the atomic data.

§Examples
use keos::atomic::AtomicU16;

let some_var = AtomicU16::new(5);
assert_eq!(some_var.into_inner(), 5);
Source

pub fn load(&self) -> u16

Loads a value from the atomic integer.

§Examples
use keos::atomic::AtomicU16;

let some_var = AtomicU16::new(5);

assert_eq!(some_var.load(d), 5);
Source

pub fn store(&self, val: u16)

Stores a value into the atomic integer.

§Examples
use keos::atomic::AtomicU16;

let some_var = AtomicU16::new(5);

some_var.store(10d);
assert_eq!(some_var.load(d), 10);
Source

pub fn swap(&self, val: u16) -> u16

Stores a value into the atomic integer, returning the previous value.

§Examples
use keos::atomic::AtomicU16;

let some_var = AtomicU16::new(5);

assert_eq!(some_var.swap(10d), 5);
Source

pub fn compare_exchange(&self, current: u16, new: u16) -> Result<u16, u16>

Stores a value into the atomic integer if the current value is the same as the current value.

The return value is a result indicating whether the new value was written and containing the previous value. On success this value is guaranteed to be equal to current.

§Examples
use keos::atomic::AtomicU16;

let some_var = AtomicU16::new(5);

assert_eq!(some_var.compare_exchange(5, 10, e), Ok(5));
assert_eq!(some_var.load(d), 10);

assert_eq!(some_var.compare_exchange(6, 12, e), Err(10));
assert_eq!(some_var.load(d), 10);
Source

pub fn fetch_add(&self, val: u16) -> u16

Adds to the current value, returning the previous value.

This operation wraps around on overflow.

§Examples
use keos::atomic::AtomicU16;

let foo = AtomicU16::new(0);
assert_eq!(foo.fetch_add(10), 0);
assert_eq!(foo.load(), 10);
Source

pub fn fetch_sub(&self, val: u16) -> u16

Subtracts from the current value, returning the previous value.

This operation wraps around on overflow.

§Examples
use keos::atomic::AtomicU16;

let foo = AtomicU16::new(20);
assert_eq!(foo.fetch_sub(10), 20);
assert_eq!(foo.load(), 10);
Source

pub fn fetch_and(&self, val: u16) -> u16

Bitwise “and” with the current value.

Performs a bitwise “and” operation on the current value and the argument val, and sets the new value to the result.

Returns the previous value.

§Examples
use keos::atomic::AtomicU16;

let foo = AtomicU16::new(0b101101);
assert_eq!(foo.fetch_and(0b110011), 0b101101);
assert_eq!(foo.load(), 0b100001);
Source

pub fn fetch_nand(&self, val: u16) -> u16

Bitwise “nand” with the current value.

Performs a bitwise “nand” operation on the current value and the argument val, and sets the new value to the result.

Returns the previous value.

§Examples
use keos::atomic::AtomicU16;

let foo = AtomicU16::new(0x13);
assert_eq!(foo.fetch_nand(0x31), 0x13);
assert_eq!(foo.load(), !(0x13 & 0x31));
Source

pub fn fetch_or(&self, val: u16) -> u16

Bitwise “or” with the current value.

Performs a bitwise “or” operation on the current value and the argument val, and sets the new value to the result.

Returns the previous value.

§Examples
use keos::atomic::AtomicU16;

let foo = AtomicU16::new(0b101101);
assert_eq!(foo.fetch_or(0b110011), 0b101101);
assert_eq!(foo.load(), 0b111111);
Source

pub fn fetch_xor(&self, val: u16) -> u16

Bitwise “xor” with the current value.

Performs a bitwise “xor” operation on the current value and the argument val, and sets the new value to the result.

Returns the previous value.

§Examples
use keos::atomic::AtomicU16;

let foo = AtomicU16::new(0b101101);
assert_eq!(foo.fetch_xor(0b110011), 0b101101);
assert_eq!(foo.load(), 0b011110);
Source

pub fn fetch_update<F>(&self, f: F) -> Result<u16, u16>
where F: FnMut(u16) -> Option<u16>,

Fetches the value, and applies a function to it that returns an optional new value. Returns a Result of Ok(previous_value) if the function returned Some(_), else Err(previous_value).

Note: This may call the function multiple times if the value has been changed from other threads in the meantime, as long as the function returns Some(_), but the function will have been applied only once to the stored value.

§Examples
use keos::atomic::AtomicU16;

let x = AtomicU16::new(7);
assert_eq!(x.fetch_update(|_| None), Err(7));
assert_eq!(x.fetch_update(|x| Some(x + 1)), Ok(7));
assert_eq!(x.fetch_update(|x| Some(x + 1)), Ok(8));
assert_eq!(x.load(), 9);
Source

pub fn fetch_max(&self, val: u16) -> u16

Maximum with the current value.

Finds the maximum of the current value and the argument val, and sets the new value to the result.

Returns the previous value.

§Examples
use keos::atomic::AtomicU16;

let foo = AtomicU16::new(23);
assert_eq!(foo.fetch_max(42), 23);
assert_eq!(foo.load(), 42);

If you want to obtain the maximum value in one step, you can use the following:

use keos::atomic::AtomicU16;

let foo = AtomicU16::new(23);
let bar = 42;
let max_foo = foo.fetch_max(bar).max(bar);
assert!(max_foo == 42);
Source

pub fn fetch_min(&self, val: u16) -> u16

Minimum with the current value.

Finds the minimum of the current value and the argument val, and sets the new value to the result.

Returns the previous value.

§Examples
use keos::atomic::AtomicU16;

let foo = AtomicU16::new(23);
assert_eq!(foo.fetch_min(42d), 23);
assert_eq!(foo.load(d), 23);
assert_eq!(foo.fetch_min(22d), 23);
assert_eq!(foo.load(d), 22);

If you want to obtain the minimum value in one step, you can use the following:

use keos::atomic::AtomicU16;

let foo = AtomicU16::new(23);
let bar = 12;
let min_foo = foo.fetch_min(bar).min(bar);
assert_eq!(min_foo, 12);
Source

pub const fn as_ptr(&self) -> *mut u16

Returns a mutable pointer to the underlying integer.

Doing non-atomic reads and writes on the resulting integer can be a data race. This method is mostly useful for FFI, where the function signature may use *mut u16 instead of &AtomicU16.

Returning an *mut pointer from a shared reference to this atomic is safe because the atomic types work with interior mutability. All modifications of an atomic change the value through a shared reference, and can do so safely as long as they use atomic operations. Any use of the returned raw pointer requires an unsafe block and still has to uphold the same restriction: operations on it must be atomic.

§Examples
use keos::atomic::AtomicU16;

extern "C" {
    fn my_atomic_op(arg: *mut u16);
}

let atomic = AtomicU16::new(1);

// SAFETY: Safe as long as `my_atomic_op` is atomic.
unsafe {
    my_atomic_op(atomic.as_ptr());
}

Trait Implementations§

Source§

impl Debug for AtomicU16

Source§

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

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

impl Default for AtomicU16

Source§

fn default() -> AtomicU16

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

impl From<u16> for AtomicU16

Source§

fn from(v: u16) -> Self

Converts an u16 into an AtomicU16.

Auto Trait Implementations§

§

impl !Freeze for AtomicU16

§

impl RefUnwindSafe for AtomicU16

§

impl Send for AtomicU16

§

impl Sync for AtomicU16

§

impl Unpin for AtomicU16

§

impl UnwindSafe for AtomicU16

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

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

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

§

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

§

type Error = Infallible

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

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

Performs the conversion.
§

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

§

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

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

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.