Macro impl_op1_opt

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

Defines a macro to implement the Op1 trait for a given type, focusing on optional transformations.

This macro, impl_op1_opt, takes a type identifier and a series of transformation patterns, generating an implementation of the Op1 trait for the specified type. The trait requires two key methods: cost, which returns the associated cost from the tuple stored within the type, and try_eval, which attempts to evaluate a unary operation on the provided Value. In try_eval, the macro iterates over values of specific types, applying a transformation function that returns an optional result. It collects results, tracking whether all transformations succeeded, and constructs a new Value of the result type, falling back to a default value if necessary. If the transformation can’t be applied to the input type, it returns a flag of false with a Null value. This macro streamlines the implementation of the Op1 trait for operations that may or may not succeed based on optional transformations.