Macro impl_op2

Source
macro_rules! impl_op2 {
    ($s:ident, $name:expr, $(($t1:ident, $t2:ident) -> $rt:ident { $f:expr }),*) => { ... };
}
Expand description

This macro facilitates the implementation of the Op2 trait for a given struct, allowing it to define binary operations on values.

It enables configuration of the operation by specifying patterns for value types, corresponding result types, and a closure for computation. When invoked, the macro takes the struct name, operation name, and a series of type pattern mappings where each pair of input types maps to a result type and an associated function. For each specified type combination, the generated implementation of the try_eval method attempts to evaluate the operation by zipping and transforming the input iterables using the provided closure function. If the types match, it returns a successful evaluation with the transformed result; otherwise, it returns a failure with a null value. This macro centralizes the repetitive logic for implementing binary operations across different value types, promoting code reuse and reducing boilerplate.