[][src]Trait atomig::AtomInteger

pub trait AtomInteger: Atom where
    <Self::Repr as PrimitiveAtom>::Impl: AtomicIntegerImpl
{ }

Atoms for which integer operations on their atomic representation make sense.

Implementing this marker trait for your type makes it possible to use Atomic::fetch_add and similar methods. Note that the integer operation is performed on the atomic representation of your type and not on your type directly!

Examples:

  • The Set and TriBool examples from the AtomLogic documentation should both not implement AtomInteger, because addition on the underlying integer does not result in any meaningful value for them.
  • Imagine you have strong types for distance measurements, including Meter(u64). It makes sense to implement AtomInteger for that type, because adding the representation (u64) makes sense.
  • As another example for types that should not implement this type, consider f64. It can be represented by u64 and additions on f64 do make sense. But add adding the u64 representations of two f64 does not yield any meaningful result!

Deriving this trait

Like Atom, this trait can automatically derived if the 'derive' Cargo feature of this crate is enabled. This custom derive is simpler because this is only a marker trait.

However, this trait cannot be derived for enums, as this is almost certainly incorrect. While in C, enums basically list some constants and often, these constants are added or subtracted from one another, this is often not valid in Rust. In Rust, a C-like enum can only have one of the values listed in the definition and nothing else. Otherwise, UB is triggered. Implementing this trait incorrectly does not cause UB, but the unpack method will panic for unexpected values. If you still think you want to implement this trait for your type, you have to do it manually.

Implementations on Foreign Types

impl AtomInteger for u8[src]

impl AtomInteger for i8[src]

impl AtomInteger for u16[src]

impl AtomInteger for i16[src]

impl AtomInteger for u32[src]

impl AtomInteger for i32[src]

impl AtomInteger for u64[src]

impl AtomInteger for i64[src]

impl AtomInteger for usize[src]

impl AtomInteger for isize[src]

Loading content...

Implementors

Loading content...