Trait atomig::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)!

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl Atom for Option<NonZeroI8>

§

type Repr = i8

source§

fn pack(self) -> Self::Repr

source§

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

source§

impl Atom for Option<NonZeroI16>

§

type Repr = i16

source§

fn pack(self) -> Self::Repr

source§

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

source§

impl Atom for Option<NonZeroI32>

§

type Repr = i32

source§

fn pack(self) -> Self::Repr

source§

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

source§

impl Atom for Option<NonZeroI64>

§

type Repr = i64

source§

fn pack(self) -> Self::Repr

source§

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

source§

impl Atom for Option<NonZeroIsize>

§

type Repr = isize

source§

fn pack(self) -> Self::Repr

source§

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

source§

impl Atom for Option<NonZeroU8>

§

type Repr = u8

source§

fn pack(self) -> Self::Repr

source§

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

source§

impl Atom for Option<NonZeroU16>

§

type Repr = u16

source§

fn pack(self) -> Self::Repr

source§

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

source§

impl Atom for Option<NonZeroU32>

§

type Repr = u32

source§

fn pack(self) -> Self::Repr

source§

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

source§

impl Atom for Option<NonZeroU64>

§

type Repr = u64

source§

fn pack(self) -> Self::Repr

source§

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

source§

impl Atom for Option<NonZeroUsize>

§

type Repr = usize

source§

fn pack(self) -> Self::Repr

source§

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

source§

impl Atom for bool

§

type Repr = bool

source§

fn pack(self) -> Self::Repr

source§

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

source§

impl Atom for char

§

type Repr = u32

source§

fn pack(self) -> Self::Repr

source§

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

source§

impl Atom for f32

§

type Repr = u32

source§

fn pack(self) -> Self::Repr

source§

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

source§

impl Atom for f64

§

type Repr = u64

source§

fn pack(self) -> Self::Repr

source§

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

source§

impl Atom for i8

§

type Repr = i8

source§

fn pack(self) -> Self::Repr

source§

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

source§

impl Atom for i16

§

type Repr = i16

source§

fn pack(self) -> Self::Repr

source§

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

source§

impl Atom for i32

§

type Repr = i32

source§

fn pack(self) -> Self::Repr

source§

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

source§

impl Atom for i64

§

type Repr = i64

source§

fn pack(self) -> Self::Repr

source§

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

source§

impl Atom for isize

§

type Repr = isize

source§

fn pack(self) -> Self::Repr

source§

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

source§

impl Atom for u8

§

type Repr = u8

source§

fn pack(self) -> Self::Repr

source§

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

source§

impl Atom for u16

§

type Repr = u16

source§

fn pack(self) -> Self::Repr

source§

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

source§

impl Atom for u32

§

type Repr = u32

source§

fn pack(self) -> Self::Repr

source§

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

source§

impl Atom for u64

§

type Repr = u64

source§

fn pack(self) -> Self::Repr

source§

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

source§

impl Atom for usize

§

type Repr = usize

source§

fn pack(self) -> Self::Repr

source§

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

source§

impl Atom for NonZeroI8

§

type Repr = i8

source§

fn pack(self) -> Self::Repr

source§

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

source§

impl Atom for NonZeroI16

§

type Repr = i16

source§

fn pack(self) -> Self::Repr

source§

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

source§

impl Atom for NonZeroI32

§

type Repr = i32

source§

fn pack(self) -> Self::Repr

source§

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

source§

impl Atom for NonZeroI64

§

type Repr = i64

source§

fn pack(self) -> Self::Repr

source§

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

source§

impl Atom for NonZeroIsize

§

type Repr = isize

source§

fn pack(self) -> Self::Repr

source§

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

source§

impl Atom for NonZeroU8

§

type Repr = u8

source§

fn pack(self) -> Self::Repr

source§

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

source§

impl Atom for NonZeroU16

§

type Repr = u16

source§

fn pack(self) -> Self::Repr

source§

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

source§

impl Atom for NonZeroU32

§

type Repr = u32

source§

fn pack(self) -> Self::Repr

source§

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

source§

impl Atom for NonZeroU64

§

type Repr = u64

source§

fn pack(self) -> Self::Repr

source§

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

source§

impl Atom for NonZeroUsize

§

type Repr = usize

source§

fn pack(self) -> Self::Repr

source§

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

source§

impl Atom for [i8; 2]

§

type Repr = u16

source§

fn pack(self) -> Self::Repr

source§

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

source§

impl Atom for [i8; 4]

§

type Repr = u32

source§

fn pack(self) -> Self::Repr

source§

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

source§

impl Atom for [i8; 8]

§

type Repr = u64

source§

fn pack(self) -> Self::Repr

source§

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

source§

impl Atom for [i16; 2]

§

type Repr = u32

source§

fn pack(self) -> Self::Repr

source§

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

source§

impl Atom for [i16; 4]

§

type Repr = u64

source§

fn pack(self) -> Self::Repr

source§

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

source§

impl Atom for [i32; 2]

§

type Repr = u64

source§

fn pack(self) -> Self::Repr

source§

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

source§

impl Atom for [u8; 2]

§

type Repr = u16

source§

fn pack(self) -> Self::Repr

source§

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

source§

impl Atom for [u8; 4]

§

type Repr = u32

source§

fn pack(self) -> Self::Repr

source§

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

source§

impl Atom for [u8; 8]

§

type Repr = u64

source§

fn pack(self) -> Self::Repr

source§

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

source§

impl Atom for [u16; 2]

§

type Repr = u32

source§

fn pack(self) -> Self::Repr

source§

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

source§

impl Atom for [u16; 4]

§

type Repr = u64

source§

fn pack(self) -> Self::Repr

source§

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

source§

impl Atom for [u32; 2]

§

type Repr = u64

source§

fn pack(self) -> Self::Repr

source§

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

source§

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

§

type Repr = *mut T

source§

fn pack(self) -> Self::Repr

source§

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

source§

impl<T> Atom for *mut T

§

type Repr = *mut T

source§

fn pack(self) -> Self::Repr

source§

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

source§

impl<T> Atom for NonNull<T>

§

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>

§

type Repr = <T as Atom>::Repr

source§

fn pack(self) -> Self::Repr

source§

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

Implementors§