Atom

Trait Atom 

Source
pub trait Atom {
    type Repr: PrimitiveAtom;

    // Required methods
    fn pack(self) -> Self::Repr;
    fn unpack(src: Self::Repr) -> Self;
}
Expand description

Types that can be represented by a primitive type supporting atomic operations.

This is trait is already implemented for all primitive types that support atomic operations. It is also implemented for f32, f64 and char as all of those can be represented by a primitive atomic type. In addition to this, you can implement this trait for your own types as long as they can be represented as one such primitive type.

The pack and unpack methods define the conversion to and from the atomic representation. The methods should be fairly fast, because they are called frequently by Atomic: at least once for every method of Atomic.

§Example

Imagine you have a Port type to represent a network port and use strong typing. It is simply a newtype around a u16, so it is easily possible to use atomic operations on it.

use atomig::{Atom, Atomic, Ordering};

struct Port(u16);

impl Atom for Port {
    type Repr = u16;
    fn pack(self) -> Self::Repr {
        self.0
    }
    fn unpack(src: Self::Repr) -> Self {
        Port(src)
    }
}

// Implementing `Atom` means that we can use `Atomic` with our type
let a = Atomic::new(Port(80));
a.store(Port(8080), Ordering::SeqCst);

§Deriving this trait

Instead of implementing the trait manually (like shown above), you can derive it automatically in many cases. In order to use that feature, you have to enabled the Cargo feature ‘derive’.

use atomig::{Atom, Atomic, Ordering};

#[derive(Atom)]
struct Port(u16);

let a = Atomic::new(Port(80));
a.store(Port(8080), Ordering::SeqCst);

The trait can be automatically derived for two kinds of types:

  • struct types with only one field. That field’s type has to implement Atom. Works with tuple structs or normal structs with one named field.
  • enum types that have a #[repr(_)] attribute specified and are C-like (i.e. no variant has any fields). The primitive type specified in the #[repr(_)] attribute is used as Repr type.

Example with enum:

use atomig::{Atom, Atomic, Ordering};

#[derive(Atom)]
#[repr(u8)]
enum Animal { Dog, Cat, Fox }

let a = Atomic::new(Animal::Cat);
a.store(Animal::Fox, Ordering::SeqCst);

Required Associated Types§

Source

type Repr: PrimitiveAtom

The atomic representation of this type.

In this trait’s implementations for the primitive types themselves, Repr is set to Self.

Required Methods§

Source

fn pack(self) -> Self::Repr

Converts the type to its atomic representation.

Source

fn unpack(src: Self::Repr) -> Self

Creates an instance of this type from the atomic representation.

This method is usually only called with values that were returned by pack. So in theory, you can assume that the argument is a valid representation. However, there are two exceptions.

If your type also implements AtomLogic or AtomInteger, results of those operations might get passed to unpack. Furthermore, this method can be called by anyone. So at the very least, you have to make sure that invalid input values do not lead to undefined behavior (e.g. memory unsafety)!

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.

Implementations on Foreign Types§

Source§

impl Atom for Option<NonZeroI8>

Source§

type Repr = i8

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Source§

impl Atom for Option<NonZeroI16>

Source§

type Repr = i16

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Source§

impl Atom for Option<NonZeroI32>

Source§

type Repr = i32

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Source§

impl Atom for Option<NonZeroI64>

Source§

type Repr = i64

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Source§

impl Atom for Option<NonZeroIsize>

Source§

type Repr = isize

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Source§

impl Atom for Option<NonZeroU8>

Source§

type Repr = u8

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Source§

impl Atom for Option<NonZeroU16>

Source§

type Repr = u16

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Source§

impl Atom for Option<NonZeroU32>

Source§

type Repr = u32

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Source§

impl Atom for Option<NonZeroU64>

Source§

type Repr = u64

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Source§

impl Atom for Option<NonZeroUsize>

Source§

type Repr = usize

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Source§

impl Atom for bool

Source§

type Repr = bool

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Source§

impl Atom for char

Available on target_has_atomic=32 only.
Source§

type Repr = u32

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Source§

impl Atom for f32

Available on target_has_atomic=32 only.
Source§

type Repr = u32

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Source§

impl Atom for f64

Available on target_has_atomic=64 only.
Source§

type Repr = u64

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Source§

impl Atom for i8

Source§

type Repr = i8

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Source§

impl Atom for i16

Source§

type Repr = i16

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Source§

impl Atom for i32

Source§

type Repr = i32

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Source§

impl Atom for i64

Source§

type Repr = i64

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Source§

impl Atom for isize

Source§

type Repr = isize

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Source§

impl Atom for u8

Source§

type Repr = u8

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Source§

impl Atom for u16

Source§

type Repr = u16

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Source§

impl Atom for u32

Source§

type Repr = u32

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Source§

impl Atom for u64

Source§

type Repr = u64

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Source§

impl Atom for usize

Source§

type Repr = usize

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Source§

impl Atom for NonZeroI8

Source§

type Repr = i8

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Source§

impl Atom for NonZeroI16

Source§

type Repr = i16

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Source§

impl Atom for NonZeroI32

Source§

type Repr = i32

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Source§

impl Atom for NonZeroI64

Source§

type Repr = i64

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Source§

impl Atom for NonZeroIsize

Source§

type Repr = isize

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Source§

impl Atom for NonZeroU8

Source§

type Repr = u8

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Source§

impl Atom for NonZeroU16

Source§

type Repr = u16

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Source§

impl Atom for NonZeroU32

Source§

type Repr = u32

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Source§

impl Atom for NonZeroU64

Source§

type Repr = u64

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Source§

impl Atom for NonZeroUsize

Source§

type Repr = usize

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Source§

impl Atom for [i8; 2]

Source§

type Repr = u16

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Source§

impl Atom for [i8; 4]

Source§

type Repr = u32

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Source§

impl Atom for [i8; 8]

Source§

type Repr = u64

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Source§

impl Atom for [i16; 2]

Source§

type Repr = u32

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Source§

impl Atom for [i16; 4]

Source§

type Repr = u64

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Source§

impl Atom for [i32; 2]

Source§

type Repr = u64

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Source§

impl Atom for [u8; 2]

Source§

type Repr = u16

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Source§

impl Atom for [u8; 4]

Source§

type Repr = u32

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Source§

impl Atom for [u8; 8]

Source§

type Repr = u64

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Source§

impl Atom for [u16; 2]

Source§

type Repr = u32

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Source§

impl Atom for [u16; 4]

Source§

type Repr = u64

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Source§

impl Atom for [u32; 2]

Source§

type Repr = u64

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Source§

impl<T> Atom for Option<NonNull<T>>

Available on target_has_atomic=ptr only.
Source§

type Repr = *mut T

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Source§

impl<T> Atom for *mut T

Available on target_has_atomic=ptr only.
Source§

type Repr = *mut T

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Source§

impl<T> Atom for NonNull<T>

Available on target_has_atomic=ptr only.
Source§

type Repr = *mut T

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Source§

impl<T: Atom> Atom for Wrapping<T>

Source§

type Repr = <T as Atom>::Repr

Source§

fn pack(self) -> Self::Repr

Source§

fn unpack(src: Self::Repr) -> Self

Implementors§