Skip to content

Derive AsyncComputer when using #[cgp_computer] with async functions#137

Merged
soareschen merged 5 commits intomainfrom
async-computer-macro
Aug 12, 2025
Merged

Derive AsyncComputer when using #[cgp_computer] with async functions#137
soareschen merged 5 commits intomainfrom
async-computer-macro

Conversation

@soareschen
Copy link
Collaborator

@soareschen soareschen commented Aug 12, 2025

Summary

This is a follow up on #136.

This PR unifies the #[cgp_computer] and #[cgp_handler] macros into just the former. The #[cgp_handler] macro has been removed, and the #[cgp_computer] macro can now be used to also derive handler implementations for async functions.

When deriving async functions, the #[cgp_computer] macro will generate an AsyncComputer implementation, and then derive other traits like Handler through promotion adapters. This is different from the original #[cgp_handler], which generates Handler implementation instead.

The generated promotion delegations are now delegated via helpers like PromoteComputer, to keep the generated code cleaner. With that, the normal promotion providers like Promote are now removed from the prelude.

Example

Given the following code:

#[cgp_computer]
fn to_string_ref<Value: Display>(value: &Value) -> String {
    value.to_string()
}

The following implementation is generated:

#[cgp_new_provider]
impl<Context, Code, Value: Display> Computer<Context, Code, &Value> for ToStringRef {
    type Output = String;

    fn compute(&self, _context: &Context, _code: Code, value: &Value) -> Self::Output {
        to_string_ref(value)
    }
}

delegate_components! {
    ToStringRef {
        [
            ComputerRefComponent,
            TryComputerComponent,
            TryComputerRefComponent,
            AsyncComputerComponent,
            AsyncComputerRefComponent,
            HandlerComponent,
            HandlerRefComponent,
        ] ->
            PromoteComputerComputer<Self>,
    }
}

This allows ToStringRef to not only be used as a Computer, but also all other computation forms such as TryComputer, AsyncComputer, Handler, and their respective reference-input variants.

@soareschen soareschen merged commit b3acb01 into main Aug 12, 2025
5 checks passed
@soareschen soareschen deleted the async-computer-macro branch August 12, 2025 21:42
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