Skip to content

Support declarative macros inside cxx::bridge #274

Closed
@sbrocket

Description

It would be useful to be able to use declarative macros inside the bridge. In my use case, I have a large number of formulaic types and methods to declare in the bridge. (The blocks below are untested but should convey the general idea.)

macro_rules! declare_ffi_handle_type {
    ($t:ident) => {
        struct $t {
            raw: u32,
        };

        extern "C" {
            paste! {
                type [<Cxx $t>];
                type [<Unowned $t>];

                fn [<get_handle_ $t:lower>](handle: &[<Cxx $t>]>) -> u32;
                fn [<get_unowned_handle_ $t:lower>](handle: &[<Unowned $t>]) -> u32;
            }
        }
    };
}

I have 30 such blocks to define, and was actually going to use another declarative macro to invoke the first macro, e.g.:

macro_rules! invoke_for_handle_types {
    ($m:ident) => {
        invoke_for_handle_types! { $m, Handle, Process, Thread, Vmo, Channel, Event, Port, Interrupt, PciDevice, DebugLog, Socket, Resource, EventPair, Job, Vmar, Fifo, Guest, Vcpu, Timer, Iommu, Bti, Profile, Pmt, SuspendToken, Pager, Exception, Clock, Stream, MsiAllocation, MsiInterrupt };
    };
    ($m:ident, $t:ident, $($ts:ident),+) => {
        invoke_for_handle_types! { $m, $t }
        invoke_for_handle_types! { $m, $(ts),+ }
    };
    ($m:ident, $t:ident) => { $m! {$t} };

Obviously this would be/is going to be very tedious to expand manually, never mind that it will be much less maintainable that way.

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions