[−][src]Trait atomig::AtomInteger
Atom
s 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
andTriBool
examples from theAtomLogic
documentation should both not implementAtomInteger
, 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 implementAtomInteger
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 byu64
and additions onf64
do make sense. But add adding theu64
representations of twof64
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.