Crate trait_tactics

Source
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, returning Self::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.

Attribute Macros§

assign_via_assign_ref
Implements A ⋄= B in terms of A ⋄= &B via self ⋄= &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 of A ⋄= B via x ⋄= 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 of A ⋄ &B via x ⋄ &y.
partial_ord_via_ord
Implements PartialOrd in terms of Ord.