Macro impl_ops::impl_op_ex
source · [−]macro_rules! impl_op_ex {
($op:tt |$lhs_i:ident : &mut $lhs:path, $rhs_i:ident : &$rhs:path| $body:block) => { ... };
($op:tt |$lhs_i:ident : &mut $lhs:path, $rhs_i:ident : $rhs:path| $body:block) => { ... };
($op:tt |$lhs_i:ident : &$lhs:path| -> $out:path $body:block) => { ... };
($op:tt |$lhs_i:ident : &$lhs:path, $rhs_i:ident : &$rhs:path| -> $out:path $body:block) => { ... };
($op:tt |$lhs_i:ident : &$lhs:path, $rhs_i:ident : $rhs:path| -> $out:path $body:block) => { ... };
($op:tt |$lhs_i:ident : $lhs:path| -> $out:path $body:block) => { ... };
($op:tt |$lhs_i:ident : $lhs:path, $rhs_i:ident : &$rhs:path| -> $out:path $body:block) => { ... };
($op:tt |$lhs_i:ident : $lhs:path, $rhs_i:ident : $rhs:path| -> $out:path $body:block) => { ... };
}
Expand description
Overloads an operator using the given closure as its body. Generates overloads for both owned and borrowed variants where possible.
Used with the same syntax as impl_op!
(see the module level documentation for more information).
Expands any borrowed inputs into both owned and borrowed variants.
impl_op_ex!(op |a: &LHS, b: &RHS| -> OUT {...});
gets expanded to
ⓘ
impl_op!(op |a: LHS, b: RHS| -> OUT {...});
impl_op!(op |a: LHS, b: &RHS| -> OUT {...});
impl_op!(op |a: &LHS, b: RHS| -> OUT {...});
impl_op!(op |a: &LHS, b: &RHS| -> OUT {...});
and impl_op_ex!(op |a: &LHS, b: RHS| -> OUT {...});
gets expanded to
ⓘ
impl_op!(op |a: LHS, b: RHS| -> OUT {...});
impl_op!(op |a: &LHS, b: RHS| -> OUT {...});
Examples
#[macro_use] extern crate impl_ops;
use std::ops;
impl_op_ex!(+ |a: &DonkeyKong, b: &DonkeyKong| -> i32 { a.bananas + b.bananas });
fn main() {
let total_bananas = &DonkeyKong::new(2) + &DonkeyKong::new(4);
assert_eq!(6, total_bananas);
let total_bananas = &DonkeyKong::new(2) + DonkeyKong::new(4);
assert_eq!(6, total_bananas);
let total_bananas = DonkeyKong::new(2) + &DonkeyKong::new(4);
assert_eq!(6, total_bananas);
let total_bananas = DonkeyKong::new(2) + DonkeyKong::new(4);
assert_eq!(6, total_bananas);
}