Skip to content

Implement delegate_and_check_components!#98

Merged
soareschen merged 2 commits intomainfrom
delegate-and-check
May 8, 2025
Merged

Implement delegate_and_check_components!#98
soareschen merged 2 commits intomainfrom
delegate-and-check

Conversation

@soareschen
Copy link
Collaborator

@soareschen soareschen commented May 8, 2025

Summary

This PR introduces the delegate_and_check_components! macro, which combines the use of both delegate_components! and check_components! into one macro call. The macro simplifies the steps required to ensure that the CGP wiring is correct, and shows error as soon as there is unsatisfied constraint in one of the delegation.

Syntax

The syntax format for the macro is as follows:

delegate_and_check_components! {
    CheckTraitName for ContextType;
    ContextProviderType {
        DelegateKeys: DelegateValue,
        ...
    }
}

Since delegate_and_check_components! combines two separate macros, the macro requires the argument to check_components! to be passed first, separated by a semicolon, then followed by the delegate entries.

Example

Following is an example of using delegate_and_check_components! to check the implementation of a name getter:

#[cgp_type]
pub trait HasNameType {
    type Name;
}

#[cgp_getter]
pub trait HasName: HasNameType {
    fn name(&self) -> &Self::Name;
}

#[cgp_context]
#[derive(HasField)]
pub struct MyContext {
    pub name: String,
}

delegate_and_check_components! {
    CanUseMyContext for MyContext;
    MyContextComponents {
        NameTypeProviderComponent: UseType<String>,
        NameGetterComponent: UseField<symbol!("name")>,
    }
}

Limitations

delegate_and_check_components! can only work in the simple cases, where the involved CGP traits do not contain additional generic parameters. Otherwise, it is necessary to separate the delegation and checks, since the library cannot know in advance which generic parameters to check the implementations against.

The macro also can't be used to check the inherited implementation of presets, since it is specified externally in #[cgp_context]. Although it may be possible to also implement automated checks there, we will defer that to future work.

@soareschen soareschen merged commit 0114198 into main May 8, 2025
5 checks passed
@soareschen soareschen deleted the delegate-and-check branch May 8, 2025 10:30
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