Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "impl bridges" to Component derive macro #669

Closed
wants to merge 3 commits into from
Closed

Add "impl bridges" to Component derive macro #669

wants to merge 3 commits into from

Conversation

jarrodldavis
Copy link
Contributor

Fixes #332.

This adds support for additional meta items to the #[component] attribute to indicate that the Component derive macro should bridge method invocations from the trait impl to an implementing type's own inherent impl. I updated core/tests/component to demonstrate and exercise an example of this:

/// Example component #2
#[derive(Component, Debug, Default)]
#[component(after_config, before_shutdown)]
pub struct BazComponent {
pub after_config_run: bool,
pub before_shutdown_run: AtomicBool,
}
impl BazComponent {
fn after_config<A: Application>(&mut self, _config: &A::Cfg) -> Result<(), FrameworkError> {
self.after_config_run = true;
Ok(())
}
fn before_shutdown(&self, _kind: Shutdown) -> Result<(), FrameworkError> {
self.before_shutdown_run.store(true, Ordering::Relaxed);
Ok(())
}
}

As demonstrated above, after_config and before_shutdown in #[component] each enable bridging from their respectively named methods on Component to equivalently-named methods in BazComponent's inherent impl.

@jarrodldavis
Copy link
Contributor Author

The after_config impl bridge is a bit clunky to use when the Component impl is for any Application, so I've also added support for deriving impl Component<MyApp> for MyComponent so you can use your application's config type:

/// Example component #2
#[derive(Component, Debug, Default)]
#[component(application = "ExampleApp", after_config, before_shutdown)]
pub struct BazComponent {
pub after_config_run: bool,
pub before_shutdown_run: AtomicBool,
}
impl BazComponent {
fn after_config(&mut self, _config: &ExampleConfig) -> Result<(), FrameworkError> {
self.after_config_run = true;
Ok(())
}
fn before_shutdown(&self, _kind: Shutdown) -> Result<(), FrameworkError> {
self.before_shutdown_run.store(true, Ordering::Relaxed);
Ok(())
}
}

- support paths, not just idents
- update `register_dependency` with correct generic argument
@jarrodldavis jarrodldavis deleted the component-derive-impl-bridge branch July 29, 2022 00:49
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.

Better support for overriding parts of a derived Component implementation.
1 participant