Skip to content
Snippets Groups Projects
Unverified Commit d5327157 authored by Bastian Köcher's avatar Bastian Köcher Committed by GitHub
Browse files

sp-api: Support `mut` in `impl_runtime_apis!` (#7924)


This brings support for declaring variables in parameters as `mut`
inside of `impl_runtime_apis!`.

---------

Co-authored-by: default avatarcmd[bot] <41898282+github-actions[bot]@users.noreply.github.com>
parent 363bd6bd
No related merge requests found
Pipeline #519105 waiting for manual action with stages
in 24 minutes and 34 seconds
title: 'sp-api: Support `mut` in `impl_runtime_apis!`'
doc:
- audience: Runtime Dev
description: |-
This brings support for declaring variables in parameters as `mut` inside of `impl_runtime_apis!`.
crates:
- name: sp-api-proc-macro
bump: patch
......@@ -74,10 +74,8 @@ pub fn replace_wild_card_parameter_names(input: &mut Signature) {
let mut generated_pattern_counter = 0;
input.inputs.iter_mut().for_each(|arg| {
if let FnArg::Typed(arg) = arg {
arg.pat = Box::new(generate_unique_pattern(
(*arg.pat).clone(),
&mut generated_pattern_counter,
));
arg.pat =
Box::new(sanitize_pattern((*arg.pat).clone(), &mut generated_pattern_counter));
}
});
}
......@@ -101,8 +99,11 @@ pub fn fold_fn_decl_for_client_side(
};
}
/// Generate an unique pattern based on the given counter, if the given pattern is a `_`.
pub fn generate_unique_pattern(pat: Pat, counter: &mut u32) -> Pat {
/// Sanitize the given pattern.
///
/// - `_` patterns are changed to a variable based on `counter`.
/// - `mut something` removes the `mut`.
pub fn sanitize_pattern(pat: Pat, counter: &mut u32) -> Pat {
match pat {
Pat::Wild(_) => {
let generated_name =
......@@ -111,6 +112,10 @@ pub fn generate_unique_pattern(pat: Pat, counter: &mut u32) -> Pat {
parse_quote!( #generated_name )
},
Pat::Ident(mut pat) => {
pat.mutability = None;
pat.into()
},
_ => pat,
}
}
......@@ -138,8 +143,7 @@ pub fn extract_parameter_names_types_and_borrows(
t => (t.clone(), None),
};
let name =
generate_unique_pattern((*arg.pat).clone(), &mut generated_pattern_counter);
let name = sanitize_pattern((*arg.pat).clone(), &mut generated_pattern_counter);
result.push((name, ty, borrow));
},
FnArg::Receiver(_) if matches!(allow_self, AllowSelfRefInParameters::No) =>
......
......@@ -81,7 +81,8 @@ impl_runtime_apis! {
unimplemented!()
}
fn something_with_block(_: Block) -> Block {
// Ensure that we accept `mut`
fn something_with_block(mut _block: Block) -> Block {
unimplemented!()
}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment