Macro wasmtime::component::flags

source ·
flags!() { /* proc-macro */ }
Available on crate features runtime and component-model only.
Expand description

A macro to generate a Rust type corresponding to WIT flags

This macro generates a type that implements the ComponentType, Lift, and Lower traits. The generated Rust type corresponds to the flags type in WIT.

Example usage of this looks like:

use wasmtime::component::flags;

flags! {
    Permissions {
        #[component(name = "read")]
        const READ;
        #[component(name = "write")]
        const WRITE;
        #[component(name = "execute")]
        const EXECUTE;
    }
}

fn validate_permissions(permissions: &mut Permissions) {
    if permissions.contains(Permissions::EXECUTE | Permissions::WRITE) {
        panic!("cannot enable both writable and executable at the same time");
    }

    if permissions.contains(Permissions::READ) {
        panic!("permissions must at least contain read");
    }
}

which corresponds to the WIT type:

flags permissions {
    read,
    write,
    execute,
}

This generates a structure which is similar to/inspired by the bitflags crate. The Permissions structure generated implements the PartialEq, Eq, Debug, BitOr, BitOrAssign, BitAnd, BitAndAssign, BitXor, BitXorAssign, and Not traits - in addition to the Wasmtime-specific component ones ComponentType, Lift, and Lower.