Skip to content
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

FEATURE REQUEST: some (attribute?) macro to implement our trait for Either easily #92

Open
anatawa12 opened this issue Dec 22, 2023 · 3 comments

Comments

@anatawa12
Copy link

I think it might be useful to implement our (object-safe) trait for either easily with some attribute macro

@cuviper
Copy link
Member

cuviper commented Dec 29, 2023

Do you have an example of what you're looking for?

Generally, I would expect this to be generically implemented in your own crate, like:

impl<L: YourTrait, R: YourTrait> YourTrait for Either<L, R> { ... }

Or you could write your own proc-macro #[derive] for types that contain Either, or maybe a macro_rules! implementation, but I think it's not something we can do from the either crate itself.

@anatawa12
Copy link
Author

I want some macro to generate like this

impl<L: RepoSource, R: RepoSource> RepoSource for Either<L, R> {
    fn cache_path(&self) -> &Path {
        for_both!(self, src => src.cache_path())
    }

    fn headers(&self) -> &IndexMap<String, String> {
        for_both!(self, src => src.headers())
    }

    fn url(&self) -> Option<&Url> {
        for_both!(self, src => src.url())
    }
}

from code like this

#[either::implement_for_either]
pub(crate) trait RepoSource {
    fn cache_path(&self) -> &Path;
    fn headers(&self) -> &IndexMap<String, String>;
    fn url(&self) -> Option<&Url>;
}

@cuviper
Copy link
Member

cuviper commented Jan 4, 2024

I see. Generating a blanket impl doesn't seem too bad when the arguments and return types are independent of Self or associated types, similar to trait object safety. But consider a trait like this:

trait GetParent {
    fn get_parent(&self) -> Option<&Self>;
}

A blanket impl on Either<L, R> could pass that call to the inner types, but there's no way to wrap their Option<&L> or Option<&R> to an Option<&Either<L, R>>.

That said, I did find an existing macro crate: https://crates.io/crates/either_trait_macro

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

No branches or pull requests

2 participants