pub trait AtomLogic: Atom where
    Self::Repr: PrimitiveAtomLogic
{ }
Expand description

Atoms 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 is u64 and the pack/unpack methods are implemented as you would expect. In this case, it makes sense to implement AtomLogic for Set: performing bit-wise logical operations on the u64 representation makes sense.
  • Imagine you have enum TriBool { Yes, Maybe, No } which you represent by u8. The pack/unpack methods use No = 0, Maybe = 1 and Yes = 2 (or some other assignment). You also implement the logical operators from std::ops for TriBool. In this case, it is probably very wrong to implement AtomLogic for TriBool: the logical operations are performed bit-wise on the u8 which will result in very strange results (maybe even in the value 3, which is not even valid). They will not use your std::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.

Implementations on Foreign Types

Implementors