Support #[use_provider] attribute inside #[cgp_impl] for higher order providers#204
Merged
soareschen merged 2 commits intomainfrom Feb 23, 2026
Merged
Support #[use_provider] attribute inside #[cgp_impl] for higher order providers#204soareschen merged 2 commits intomainfrom
#[use_provider] attribute inside #[cgp_impl] for higher order providers#204soareschen merged 2 commits intomainfrom
Conversation
1c0e022 to
5199ec3
Compare
Collaborator
Author
|
Note that there is a bug at rust-lang/rust#152765 that breaks the formatting when the #[cgp_impl(new ScaledArea<Inner>)]
#[use_provider(Inner: AreaCalculator)]
impl<Inner> AreaCalculator {
fn area(&self, #[implicit] scale_factor: f64) -> f64 {
let base_area = #[use_provider(Inner)]
self.area();
base_area * scale_factor * scale_factor
}
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR introduces a new
#[use_provider]attribute that can be used to improve the ergonomics of using higher order providers, by hiding theSelfparameter at the first position of the generic parameter of the provider trait.For example, the
ScaledAreaprovider can be rewritten as:which would be desugared to:
The outer
#[use_provider]attribute automatically adds theSelfparameter to the generic parameter ofInnerCalculator, so that the user only needs to writeInnerCalculator: AreaCalculatorinstead ofInnerCalculator: AreaCalculator<Self>. The trait bound is then added to thewhereclause of the impl block.The inner
#[use_provider]attribute accepts aProvidertype and can be applied on a method call expression. It converts the expression from the formreceiver.method(args)toProvider::method(receiver, args), so that the method call is dispatched to the specified provider instead of through the context.It is strongly recommended to always use
#[use_provider]when implementing higher order providers, as it significantly reduces the boilerplate of writing higher order providers, and makes the code much more readable. Without it, the reader may be confused by the extraSelfgeneric parameter at the first position of the provider trait, which breaks the illusion that the provider trait appears