Expand description
Macros that provide common patterns for implementing traits in terms of other traits.
§Binary operators
A binary operator trait is a trait that has:
- A single generic type argument representing the right-hand-side operand, commonly defaulted to
Self - A single associated type
Output - A single method whose two arguments are
selfand the right-hand-side operand, returningSelf::Output
A compound assignment operator trait is a trait that has:
- A single generic type argument representing the right-hand-side operand, commonly defaulted to
Self - A single method whose two arguments are
&mut selfand the right-hand-side operand, returning()
Particularly when overloading one of Rust’s built-in binary operators, it is customary to
provide implementations not only for A ⋄ B, but also for &A ⋄ B, A ⋄ &B, and &A ⋄ &B
(where ⋄ stands for the operator to be overloaded, and A and B are operand types).
Furthermore, if the binary operator has a corresponding compound assignment operator (which we
will refer to as ⋄=), it is customary to provide implements for A ⋄= B and A ⋄= &B. The
macros provided by this module assist in writing these additional trait implementations.
First, implement &A ⋄ &B. This is the most general implementation, as all other
implementations can be written in terms of it.
Then, provide the implementations suggested below, either by using the appropriate macro, or by explicitly writing an optimized implementation.
- If the binary operator has a corresponding compound assignment operator:
- Implement
A ⋄= &Bin terms of&A ⋄ &Busing assign_via_binop_ref_lhs. - Implement
A ⋄= Bin terms ofA ⋄= &Busing assign_via_assign_ref. - Implement
A ⋄ &Bin terms ofA ⋄= &Busing binop_via_assign. - Implement
&A ⋄ Bin terms of&A ⋄ &Busing binop_via_binop_ref_rhs. - Implement
A ⋄ Bin terms ofA ⋄= Busing binop_via_assign.
- Implement
- Otherwise:
- Implement
A ⋄ &Bin terms of&A ⋄ &Busing binop_via_binop_ref_lhs. - Implement
&A ⋄ Bin terms of&A ⋄ &Busing binop_via_binop_ref_rhs. - Implement
A ⋄ Bin one of two ways:- in terms of
&A ⋄ Busing binop_via_binop_ref_lhs, or - in terms of
A ⋄ &Busing binop_via_binop_ref_rhs (preferred whenA ⋄ &Bis an optimized implementation).
- in terms of
- Implement
Attribute Macros§
- assign_
via_ assign_ ref - Implements
A ⋄= Bin terms ofA ⋄= &Bviaself ⋄= &y. - assign_
via_ binop_ ref_ lhs - Implements
A ⋄= Bin terms of&A ⋄ &Bvia*self = &*self ⋄ y. - binop_
via_ assign - Implements
A ⋄ Bin terms ofA ⋄= Bviax ⋄= y; x. - binop_
via_ binop_ ref_ lhs - Implements
A ⋄ Bin terms of&A ⋄ Bvia&x ⋄ y. - binop_
via_ binop_ ref_ rhs - Implements
A ⋄ Bin terms ofA ⋄ &Bviax ⋄ &y. - partial_
ord_ via_ ord - Implements PartialOrd in terms of Ord.