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
self
and 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 self
and 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 ⋄= &B
in terms of&A ⋄ &B
using assign_via_binop_ref_lhs. - Implement
A ⋄= B
in terms ofA ⋄= &B
using assign_via_assign_ref. - Implement
A ⋄ &B
in terms ofA ⋄= &B
using binop_via_assign. - Implement
&A ⋄ B
in terms of&A ⋄ &B
using binop_via_binop_ref_rhs. - Implement
A ⋄ B
in terms ofA ⋄= B
using binop_via_assign.
- Implement
- Otherwise:
- Implement
A ⋄ &B
in terms of&A ⋄ &B
using binop_via_binop_ref_lhs. - Implement
&A ⋄ B
in terms of&A ⋄ &B
using binop_via_binop_ref_rhs. - Implement
A ⋄ B
in one of two ways:- in terms of
&A ⋄ B
using binop_via_binop_ref_lhs, or - in terms of
A ⋄ &B
using binop_via_binop_ref_rhs (preferred whenA ⋄ &B
is an optimized implementation).
- in terms of
- Implement
Attribute Macros§
- assign_
via_ assign_ ref - Implements
A ⋄= B
in terms ofA ⋄= &B
viaself ⋄= &y
. - assign_
via_ binop_ ref_ lhs - Implements
A ⋄= B
in terms of&A ⋄ &B
via*self = &*self ⋄ y
. - binop_
via_ assign - Implements
A ⋄ B
in terms ofA ⋄= B
viax ⋄= y; x
. - binop_
via_ binop_ ref_ lhs - Implements
A ⋄ B
in terms of&A ⋄ B
via&x ⋄ y
. - binop_
via_ binop_ ref_ rhs - Implements
A ⋄ B
in terms ofA ⋄ &B
viax ⋄ &y
. - partial_
ord_ via_ ord - Implements PartialOrd in terms of Ord.