Macro gen_ops_ex

Source
macro_rules! gen_ops_ex {
    ($(<$($($lt:lifetime),+)? $(,)? $($($gen:ident),+)? $(| $(const $C:ident : $Ct:ty),+)?>;)? types $($rest:tt)+) => { ... };
    (@step1 ($($gen:tt)*); types $lhs:ty, $($rest:tt)+) => { ... };
    (@step1 ($($gen:tt)*); types ref $lhs:ty, $($rest:tt)+) => { ... };
    (@step1 ($($gen:tt)*); types mut $lhs:ty, $($rest:tt)+) => { ... };
    (@step1 ($($gen:tt)*); types mut $lhs:ty, $($rest:tt)+) => { ... };
    (@step2 ($($gen:tt)*); types $lref:ident: $lhs:ty, $rhs:ty => $out:ty; $($rest:tt)+) => { ... };
    (@step2 ($($gen:tt)*); types $lref:ident: $lhs:ty, ref $rhs:ty => $out:ty; $($rest:tt)+) => { ... };
    (@step2 ($($gen:tt)*); types $lref:ident: $lhs:ty, mut $rhs:ty => $out:ty; $($rest:tt)+) => { ... };
    (@step2 ($($gen:tt)*); types own: $lhs:ty, $rhs:ty; $($rest:tt)+) => { ... };
    (@step2 ($($gen:tt)*); types own: $lhs:ty, ref $rhs:ty; $($rest:tt)+) => { ... };
    (@step2 ($($gen:tt)*); types own: $lhs:ty, mut $rhs:ty; $($rest:tt)+) => { ... };
    (@step2 ($($gen:tt)*); types mut_: $lhs:ty, $rhs:ty; $($rest:tt)+) => { ... };
    (@step2 ($($gen:tt)*); types mut_: $lhs:ty, ref $rhs:ty; $($rest:tt)+) => { ... };
    (@step2 ($($gen:tt)*); types mut_: $lhs:ty, mut $rhs:ty; $($rest:tt)+) => { ... };
    (@step2 ($($gen:tt)*); types ref_: $lhs:ty, mut $rhs:ty; $($rest:tt)+) => { ... };
    (@step2 ($($gen:tt)*); types ref_: $lhs:ty, ref $rhs:ty; $($rest:tt)+) => { ... };
    (@step2 ($($gen:tt)*); types ref_: $lhs:ty, $rhs:ty; $($rest:tt)+) => { ... };
    (@step1 ($($gen:tt)*); types $lhs:ty => $out:ty; $($rest:tt)+) => { ... };
    (@step1 ($($gen:tt)*); types ref $lhs:ty => $out:ty; $($rest:tt)+) => { ... };
    (@step1 ($($gen:tt)*); types mut $lhs:ty => $out:ty; $($rest:tt)+) => { ... };
}
Expand description

Implements trait for borrowed types.

§Example

#[derive(Debug, Copy, Clone, PartialEq)]
struct Pair<T>(pub T, pub T);
 
gen_ops_ex!(
    <T>;
    types mut Pair<T>, T => Pair<T>;
    for * call |a: &Pair<T>, b:&T| Pair(a.0 * *b, a.1 * *b);
    where T: Mul<Output=T> + Copy
);
 
let mut a = Pair(12, 3);
{
    let mut b = &mut a;
    println!("&mut a * 2 = {:?}", b * 2);// &mut a * 2 = Pair(24, 6)
}
println!("&a * 2 = {:?}", &a * 2);// &a * 2 = Pair(24, 6)
println!("a * 2 = {:?}", a * 2);// a * 2 = Pair(24, 6)

§Note

gen_ops_ex! uses extended type signature.

Type signatures supported for gen_ops_ex! are

  • types Lhs, Rhs => Out; for binary
  • types Lhs, Rhs; for assignment
  • types Lhs => Out for unary

Lhs and Rhs must be owned types.

For unary and binary operators, the callable expressions must take immutable borrowed types as argument(s) and return the result of the Out type. And for assignment operators the callable expressions must take mutable borrowed type as first argument, take immutable borrowed type as second argument and return nothing;