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

Retain specific runtime APIs #961

Merged
merged 3 commits into from
May 22, 2023

Conversation

tadeohepperle
Copy link
Contributor

fixes #933

I integrated the retaining of specific runtime APIs into the existing retain_metadata_pallets functionality, because it is more efficient to join the two operations in one place. As for the typing: I tried usind Option<FnMut(&str) -> bool> as a sensiple argument type for both pallets_filter and runtime_apis_filter but spent way too much time on lifetime issues and could not get it going well. So now I settled for just passing |_| true closures, when all elements should be retained. Not so nice, I know, but should work.

@tadeohepperle tadeohepperle requested a review from a team as a code owner May 17, 2023 21:13
@tadeohepperle tadeohepperle changed the title retain runtime apis Retain specific runtime APIs May 17, 2023
@tadeohepperle tadeohepperle self-assigned this May 18, 2023
Comment on lines 52 to 70
match (opts.pallets.as_ref(), opts.runtime_apis.as_ref()) {
(Some(pallets), Some(runtime_apis)) => retain_metadata(
&mut metadata_v15,
|pallet_name| pallets.iter().any(|p| &**p == pallet_name),
|runtime_api_name| runtime_apis.iter().any(|p| &**p == runtime_api_name),
),
(Some(pallets), None) => retain_metadata(
&mut metadata_v15,
|pallet_name| pallets.iter().any(|p| &**p == pallet_name),
|_| true,
),
(None, Some(runtime_apis)) => retain_metadata(
&mut metadata_v15,
|_| true,
|runtime_api_name| runtime_apis.iter().any(|p| &**p == runtime_api_name),
),
(None, None) => {}
}
metadata = metadata_v15.into();
Copy link
Collaborator

@jsdw jsdw May 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way to avoid the repetition would be by boxing the closures like so:

let retain_pallets_fn: Box<dyn Fn(&str) -> bool> = match opts.pallets.as_ref() {
    Some(pallets) => Box::new(|name| pallets.iter().any(|p| &**p == name)),
    None => Box::new(|_| true)
};
let retain_runtime_apis_fn: Box<dyn Fn(&str) -> bool> = match opts.runtime_apis.as_ref() {
    Some(apis) => Box::new(|name| apis.iter().any(|p| &**p == name)),
    None => Box::new(|_| true)
};
retain_metadata(&mut metadata_v15, retain_pallets_fn, retain_runtime_apis_fn);
metadata = metadata_v15.into();

A little extra overhead but not a big deal :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, nice, yeah that is definitely a solution. I still want to keep a check that at least one of the arguments is set, because if not we can avoid calling the retain_metadata function alltogether.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I wouldn't reaplce the initial check, just the highlighted lines :)

Copy link
Collaborator

@jsdw jsdw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!

Copy link
Collaborator

@lexnv lexnv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing PR! 👍

@tadeohepperle tadeohepperle merged commit e63d618 into master May 22, 2023
@tadeohepperle tadeohepperle deleted the tadeo-hepperle-retain-specific-runtime-apis branch May 22, 2023 09:02
@jsdw jsdw mentioned this pull request Jun 1, 2023
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.

CLI: Allow users to retain specific runtime APIs
3 participants