pub trait AtomLogic: Atomwhere
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 isu64and thepack/unpackmethods are implemented as you would expect. In this case, it makes sense to implementAtomLogicforSet: performing bit-wise logical operations on theu64representation makes sense. - Imagine you have
enum TriBool { Yes, Maybe, No }which you represent byu8. Thepack/unpackmethods useNo = 0,Maybe = 1andYes = 2(or some other assignment). You also implement the logical operators fromstd::opsforTriBool. In this case, it is probably very wrong to implementAtomLogicforTriBool: the logical operations are performed bit-wise on theu8which will result in very strange results (maybe even in the value 3, which is not even valid). They will not use yourstd::opsimplementations!
§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.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.