Skip to content

Conversation

@soareschen
Copy link
Collaborator

@soareschen soareschen commented Oct 16, 2025

Summary

This PR introduces a new #[cgp_impl] macro that replaces the #[cgp_provider] macro and provides much better ergonomics for implementing provider traits in CGP.

In essence, #[cgp_impl] allows you to write a provider trait implementation as if it is a blanket implementation for the consumer trait. With this, the implementation of CGP providers become as intuitive as just implementing regular Rust traits.

Example

Consider the following example trait:

#[cgp_component(HttpRequestFetcher)]
pub trait CanFetchHttpRequest: HasErrorType {
    fn fetch_http_request(&self, request: Request) -> Result<Response, Self::Error>;
}

With #[cgp_impl], you can now implement a provider such as follows:

#[cgp_impl(new FetchWithHttpClient)]
impl<Context> HttpRequestFetcher for Context
where
    Self: HasHttpClient + HasErrorType,
{
    fn fetch_http_request(&self, request: Request) -> Result<Response, Self::Error> {
        ...
    }
}

Compared to before, you would have to implement the provider using #[cgp_provider] as follows:

#[cgp_new_provider]
impl<Context> HttpRequestFetcher<Context> for FetchWithHttpClient
where
    Context: HasHttpClient + HasErrorType,
{
    fn fetch_http_request(context: &Context, request: Request) -> Result<Response, Context::Error> {
        todo!()
    }
}

As we can see, the ergonomic for #[cgp_impl] is much closer to vanilla Rust trait implementation.

Behind the scene, #[cgp_impl] desugars the provider trait implementation to the same as how you would manually write it using #[cgp_provider]. So understanding of provider traits is still necessary, especially during debugging.

@soareschen soareschen merged commit d194a24 into main Oct 16, 2025
5 checks passed
@soareschen soareschen deleted the cgp-impl branch October 16, 2025 20:26
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