Skip to content

Support generics in generated export! macro in Rust #1564

@tqwewe

Description

@tqwewe

The wit_bindgen::generate! macro generates a export! macro which has the following syntax supported:

    ($ty:ident) => ...;
    ($ty:ident with_types_in $($path_to_types_root:tt)*) => ...;

Since $ty is an ident only, it means I'm unable to pass in a path or type with generics, which is quite limiting.

My use case is something like this:

#[macro_export]
macro_rules! export_projection {
    ($ty:path) => {
        $crate::runtime::projection::export!(
            $crate::runtime::projection::Projection<$ty> with_types_in $crate::runtime::projection
        );
    };
}
pub struct Projection<T>(PhantomData<T>);

pub struct ProjectionState<T> {
    inner: RefCell<T>,
}

impl<T: EventHandler + 'static> Guest for Projection<T> {
    type ProjectionState = ProjectionState<T>;
}

impl<T: EventHandler + 'static> GuestProjectionState for ProjectionState<T> {
    ...
}

But with the export! macro only accepting an ident, it becomes problematic.

I've experimented with simply changing wit_bindgen to use path instead of ident, which seems to work no problem. The only issue is with_types_in isn't supported here, so it needs to be changed to something like $ty => $path_to_types_root.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions