macro_rules! for_each_operator {
    ($mac:ident) => { ... };
}
Expand description

A helper macro to conveniently iterate over all opcodes recognized by this crate. This can be used to work with either the Operator enumeration or the VisitOperator trait if your use case uniformly handles all operators the same way.

This is an “iterator macro” where this macro is invoked with the name of another macro, and then that macro is invoked with the list of all operators. An example invocation of this looks like:

// These names are referred to by the types of each payload and must
// be imported into the module using the `for_each_operator!` macro.
use wasmparser::{V128, MemArg, BlockType, ValType, BrTable, Ieee32, Ieee64};

macro_rules! define_visit_operator {
    // The outer layer of repetition represents how all operators are
    // provided to the macro at the same time.
    //
    // The `$op` name is bound to the `Operator` variant name. The
    // payload of the operator is optionally specified (the `$(...)?`
    // clause) since not all instructions have payloads. Within the payload
    // each argument is named and has its type specified.
    //
    // The `$visit` name is bound to the corresponding name in the
    // `VisitOperator` trait that this corresponds to.
    ($( $op:ident $({ $($arg:ident: $argty:ty),* })? => $visit:ident)*) => {
        $(
            fn $visit(&mut self, _offset: usize $($(,$arg: $argty)*)?) {
                // do nothing for this example
            }
        )*
    }
}

pub struct VisitAndDoNothing;

impl<'a> wasmparser::VisitOperator<'a> for VisitAndDoNothing {
    type Output = ();

    wasmparser::for_each_operator!(define_visit_operator);
}