AtomicI8

Struct AtomicI8 

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

An integer type which can be safely shared between threads.

This type has the same size, alignment, and bit validity as the underlying integer type, [i8].

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 [i8].

Tuple Fields§

§0: AtomicI8

Implementations§

Source§

impl AtomicI8

Source

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

Creates a new atomic integer.

§Examples
use keos::atomic::AtomicI8;

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

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

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::AtomicI8;

let mut some_var = AtomicI8::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) -> i8

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::AtomicI8;

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

pub fn load(&self) -> i8

Loads a value from the atomic integer.

§Examples
use keos::atomic::AtomicI8;

let some_var = AtomicI8::new(5);

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

pub fn store(&self, val: i8)

Stores a value into the atomic integer.

§Examples
use keos::atomic::AtomicI8;

let some_var = AtomicI8::new(5);

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

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

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

§Examples
use keos::atomic::AtomicI8;

let some_var = AtomicI8::new(5);

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

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

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::AtomicI8;

let some_var = AtomicI8::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: i8) -> i8

Adds to the current value, returning the previous value.

This operation wraps around on overflow.

§Examples
use keos::atomic::AtomicI8;

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

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

Subtracts from the current value, returning the previous value.

This operation wraps around on overflow.

§Examples
use keos::atomic::AtomicI8;

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

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

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::AtomicI8;

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

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

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::AtomicI8;

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

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

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::AtomicI8;

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

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

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::AtomicI8;

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

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

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::AtomicI8;

let x = AtomicI8::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: i8) -> i8

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::AtomicI8;

let foo = AtomicI8::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::AtomicI8;

let foo = AtomicI8::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: i8) -> i8

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::AtomicI8;

let foo = AtomicI8::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::AtomicI8;

let foo = AtomicI8::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 i8

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 i8 instead of &AtomicI8.

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::AtomicI8;

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

let atomic = AtomicI8::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 AtomicI8

Source§

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

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

impl Default for AtomicI8

Source§

fn default() -> AtomicI8

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

impl From<i8> for AtomicI8

Source§

fn from(v: i8) -> Self

Converts an i8 into an AtomicI8.

Auto Trait Implementations§

§

impl !Freeze for AtomicI8

§

impl RefUnwindSafe for AtomicI8

§

impl Send for AtomicI8

§

impl Sync for AtomicI8

§

impl Unpin for AtomicI8

§

impl UnwindSafe for AtomicI8

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.