Macro cswap

Source
macro_rules! cswap {
    (   // defaults to `tmp`
        $a:expr, $b:expr) => { ... };
    (xor // deprecated(since = "0.23.0", note = "Use `xor:`")
        $a:expr, $b:expr) => { ... };
    (tmp:
        // swaps two values using a temporary variable.
        $a:expr, $b:expr) => { ... };
    (mut:
        // swaps two values by calling core's `swap` over their mut references.
        $a:expr, $b:expr) => { ... };
    (xor:
        // swaps two `T: PartialEq + BitXorAssign` values without a temporary variable.
        $a:expr, $b:expr) => { ... };
    (xor_unchecked:
        // swaps two `T: PartialEq + BitXorAssign` values without a temporary variable,
        // without making sure they are both equal, in which case they'll get corrupted.
        $a:expr, $b:expr) => { ... };
}
Expand description

Swaps two mutable variables in a compile-time friendly manner.

For that it uses either:

  1. a temporary variable.
  2. [Mem::swap] over their respective mutable references.
  3. the xor swap method, making sure the values are not the same, first.
  4. the xor swap method, unchecked. If both values are the same they will get corrupted.