Skip to content

Automatically derive UseContext implementation inside #[cgp_component]#88

Merged
soareschen merged 2 commits intomainfrom
derive-use-context
Apr 29, 2025
Merged

Automatically derive UseContext implementation inside #[cgp_component]#88
soareschen merged 2 commits intomainfrom
derive-use-context

Conversation

@soareschen
Copy link
Collaborator

@soareschen soareschen commented Apr 29, 2025

Summary

This PR enhances the #[cgp_component] macro families to include an auto derivation of implementation of UseContext.

UseContext is used as a provider that simply delegates the implementation back to the context. It is the reverse of consumer-to-provider delegation.

As an example, given the following:

#[cgp_component {
    provider: NameGetter,
}]
pub trait HasName {
    fn name(&self) -> &str;
}

The following UseContext implementation would be derived:

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

The use of UseContext can be useful in the advanced CGP usage of higher-order providers, where providers are passed as type parameters directly to other providers. The auto derivation of UseContext will make it simpler to make use of higher-order provider patterns, without needing to explicitly derive the implementation on all relevant CGP traits.

@soareschen soareschen marked this pull request as ready for review April 29, 2025 20:41
@soareschen soareschen changed the title Automatically derive UseContext implementation inside #[cgp_component] Automatically derive UseContext implementation inside #[cgp_component] Apr 29, 2025
@soareschen soareschen merged commit 3e24278 into main Apr 29, 2025
5 checks passed
@soareschen soareschen deleted the derive-use-context branch April 29, 2025 20:54
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.

1 participant