pub unsafe trait AtomicType: Sized + Send + Copy {
    type Repr: AtomicImpl;
}
Expand description

Types that support basic atomic operations.

§Round-trip transmutability

T is round-trip transmutable to U if and only if both of these properties hold:

  • Any valid bit pattern for T is also a valid bit pattern for U.
  • Transmuting (e.g. using transmute()) a value of type T to U and then to T again yields a value that is in all aspects equivalent to the original value.

§Safety

Note that this is more relaxed than requiring the bi-directional transmutability (i.e. transmute() is always sound between U and T) because of the support for atomic variables over unit-only enums, see Examples.

§Limitations

Because C primitives are used to implement the atomic operations, and a C function requires a valid object of a type to operate on (i.e. no MaybeUninit<_>), hence at the Rust <-> C surface, only types with all the bits initialized can be passed. As a result, types like (u8, u16) (padding bytes are uninitialized) are currently not supported.

§Examples

A unit-only enum that implements AtomicType:

use kernel::sync::atomic::{AtomicType, Atomic, Relaxed};

#[derive(Clone, Copy, PartialEq, Eq)]
#[repr(i32)]
enum State {
    Uninit = 0,
    Working = 1,
    Done = 2,
};

// SAFETY: `State` and `i32` has the same size and alignment, and it's round-trip
// transmutable to `i32`.
unsafe impl AtomicType for State {
    type Repr = i32;
}

let s = Atomic::new(State::Uninit);

assert_eq!(State::Uninit, s.load(Relaxed));

Required Associated Types§

source

type Repr: AtomicImpl

The backing atomic implementation type.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl AtomicType for i32

§

type Repr = i32

source§

impl AtomicType for i64

§

type Repr = i64

source§

impl AtomicType for isize

§

type Repr = i64

source§

impl AtomicType for u32

§

type Repr = i32

source§

impl AtomicType for u64

§

type Repr = i64

source§

impl AtomicType for usize

§

type Repr = i64

Implementors§