Skip to content

Create variant macro #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Dec 14, 2023
Merged

Create variant macro #2

merged 7 commits into from
Dec 14, 2023

Conversation

tmandry
Copy link
Member

@tmandry tmandry commented Dec 13, 2023

This takes an input like this:

#[variant(SendIntFactory: Send)]
trait IntFactory {
    async fn make(&self, x: u32, y: &str) -> i32;
    fn stream(&self) -> impl Iterator<Item = i32>;
    fn call(&self) -> u32;
}

And creates an additional trait with the specified bounds:

trait SendIntFactory: Send {
    fn make(&self, x: u32, y: &str) -> impl ::core::future::Future<Output = i32> + Send;
    fn stream(&self) -> impl Iterator<Item = i32> + Send;
    fn call(&self) -> u32;
}

And a blanket impl, so the implementer only has to implement one trait:

impl<T> IntFactory for T
where
    T: SendIntFactory,
{
    async fn make(&self, x: u32, y: &str) -> i32 {
        <Self as SendIntFactory>::make(self, x, y).await
    }
    fn stream(&self) -> impl Iterator<Item = i32> {
        <Self as SendIntFactory>::stream(self)
    }
    fn call(&self) -> u32 {
        <Self as SendIntFactory>::call(self)
    }
}

After brainstorming a handful of options I settled on variant as the most self-explanatory name for what this macro does. It does unfortunately conflict with enum variants, but I don't think that will actually be confusing in context.

My hope is that this will be forward-compatible with both RTN and implementable trait aliases, as described here.

@tmandry tmandry force-pushed the variant branch 2 times, most recently from 99b016d to b7f8ae4 Compare December 14, 2023 00:36
tmandry and others added 5 commits December 14, 2023 13:18
Tokens in `syn` are `Copy`, so we don't need to clone them.  Let's
remove the calls to `clone`.
@tmandry tmandry merged commit 2bf8bfa into main Dec 14, 2023
@tmandry tmandry deleted the variant branch December 14, 2023 21:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants