1 unstable release
0.1.0 | Apr 7, 2024 |
---|
#2636 in Rust patterns
Used in flexint
11KB
Macros that provide common patterns for implementing traits in terms of other traits.
lib.rs
:
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
Dependencies
~0.7–1.2MB
~26K SLoC