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

Implement derive_provider macros #30

Open
soareschen opened this issue Dec 1, 2024 · 0 comments
Open

Implement derive_provider macros #30

soareschen opened this issue Dec 1, 2024 · 0 comments

Comments

@soareschen
Copy link
Collaborator

Summary

The derive_provider macros would allow trait-generic providers to be automatically implemented following similar boilerplates. Below are some examples:

WithProvider

#[derive_provider(WithProvider)]
#[cgp_component { name: NameTypeComponent, provider: ProvideNameType }]
pub trait HasNameType {
  type Name;
}

would have the following derived:

impl<Context, Provider, Type> ProvideNameType<Context> for WithProvider<Provider>
where
  Provider: ProvideType<NameTypeComponent, Type = Name>,
{
  type Name = Name;
}

UseContext

#[derive_provider(UseContext)]
#[cgp_component { provider: NameGetter }]
pub trait HasName {
  fn get_name(&self) -> &String;
}

would have the following derived:

impl<Context> NameGetter<Context> for UseContext
where
  Context: HasName,
{
  fn get_name(context: &Context) -> &String {
    context.get_name()
  }
}

UseField

#[derive_provider(UseField)]
#[cgp_component { provider: NameGetter }]
pub trait HasName {
  fn get_name(&self) -> &String;
}

Would have the following derived:

impl<Context, Tag> NameGetter<Context> for UseField<Tag>
where
  Context: HasField<Tag, Value = String>,
{
  fn get_name(context: &Context) -> &String {
    context.get_field(PhantomData)
  }
}
@soareschen soareschen modified the milestone: v1.0 Release Dec 1, 2024
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

1 participant