[−][src]Trait atomig::AtomLogic
Atom
s for which logical operations on their atomic representation make
sense.
Implementing this marker trait for your type makes it possible to use
Atomic::fetch_and
and similar methods. Note that the logical
operation is performed on the atomic representation of your type and not
on your type directly!
Examples:
- Imagine you have a
Set(u64)
type which represents an integer set for integers up to 63. The atomic representation isu64
and thepack
/unpack
methods are implemented as you would expect. In this case, it makes sense to implementAtomLogic
forSet
: performing bit-wise logical operations on theu64
representation makes sense. - Imagine you have
enum TriBool { Yes, Maybe, No }
which you represent byu8
. Thepack
/unpack
methods useNo = 0
,Maybe = 1
andYes = 2
(or some other assignment). You also implement the logical operators fromstd::ops
forTriBool
. In this case, it is probably very wrong to implementAtomLogic
forTriBool
: the logical operations are performed bit-wise on theu8
which will result in very strange results (maybe even in the value 3, which is not even valid). They will not use yourstd::ops
implementations!
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 used in bitwise logical operations, 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.