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 forU
. - Transmuting (e.g. using
transmute()
) a value of typeT
toU
and then toT
again yields a value that is in all aspects equivalent to the original value.
§Safety
Self
must have the same size and alignment asSelf::Repr
.Self
must be round-trip transmutable toSelf::Repr
.
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§
Sourcetype Repr: AtomicImpl
type Repr: AtomicImpl
The backing atomic implementation type.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.