Macro libbpf_rs::btf_type_match

source ·
macro_rules! btf_type_match {
    (
        match $ty:ident {
            $($pattern:tt)+
        }
    ) => { ... };
}
Expand description

A macro that allows matching on the type of a BtfType as if it was an enum.

Each pattern can be of two types.

use libbpf_rs::btf::BtfType;
use libbpf_rs::btf_type_match;

let ty: BtfType;
btf_type_match!(match ty {
    BtfKind::Int(i) => do_something_with_an_int(i),
    BtfKind::Struct => "it's a struct",
    BtfKind::Union => {
        "it's a union"
    },
    _ => "default",
});

Variable Binding.

    BtfKind::Int(i) => {
        // we can use i here and it will be an `Int`
    }

NonBinding.

    BtfKind::Int => {
        // we don't have access to the variable, but we know the scrutinee is an Int
    }

Multiple Variants

    BtfKind::Struct | BtfKind::Union => {
        // we don't have access to the variable,
        // but we know the scrutinee is either a Struct or a Union
    }

Special case for Struct and Union: Composite

    BtfKind::Composite(c) => {
        // we can use `c` as an instance of `Composite`.
        // this branch will match if the type is either a Struct or a Union.
    }