mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-27 08:07:58 +00:00
Make runtime api calls native when possible (#1302)
* Add simple benchmark for the runtime api * Make the executor support native calls * Some documentation * Hide behind `feature = "std"` * Rework the native calls * Make all tests compile again * Make every parameter using the Block serialized/deserialized in the native call * Forward `UnwindSafe` requirement * Remove debug stuff * Add some documentation * Fixes warnings * Fixes errors after master rebase * Fixes compilation after master rebase * Fixes compilation after rebase
This commit is contained in:
@@ -15,7 +15,7 @@
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use proc_macro2::{TokenStream, Span};
|
||||
use syn::{Result, Ident, FnDecl, parse_quote, Type, FnArg};
|
||||
use syn::{Result, Ident, FnDecl, parse_quote, Type, Pat, spanned::Spanned, FnArg, Error};
|
||||
use quote::quote;
|
||||
use std::env;
|
||||
|
||||
@@ -64,19 +64,6 @@ pub fn fold_fn_decl_for_client_side(
|
||||
block_id: &TokenStream,
|
||||
crate_: &TokenStream
|
||||
) -> FnDecl {
|
||||
// Add `&` to all parameter types.
|
||||
input.inputs
|
||||
.iter_mut()
|
||||
.filter_map(|i| match i {
|
||||
FnArg::Captured(ref mut arg) => Some(&mut arg.ty),
|
||||
_ => None,
|
||||
})
|
||||
.filter_map(|i| match i {
|
||||
Type::Reference(_) => None,
|
||||
r => Some(r),
|
||||
})
|
||||
.for_each(|i| *i = parse_quote!( &#i ));
|
||||
|
||||
// Add `&self, at:& BlockId` as parameters to each function at the beginning.
|
||||
input.inputs.insert(0, parse_quote!( at: &#block_id ));
|
||||
input.inputs.insert(0, parse_quote!( &self ));
|
||||
@@ -95,3 +82,61 @@ pub fn fold_fn_decl_for_client_side(
|
||||
|
||||
input
|
||||
}
|
||||
|
||||
/// 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 {
|
||||
match pat {
|
||||
Pat::Wild(_) => {
|
||||
let generated_name = Ident::new(
|
||||
&format!("runtime_api_generated_name_{}", counter),
|
||||
pat.span()
|
||||
);
|
||||
*counter += 1;
|
||||
|
||||
parse_quote!( #generated_name )
|
||||
},
|
||||
_ => pat,
|
||||
}
|
||||
}
|
||||
|
||||
/// Extracts the name, the type and `&` or ``(if it is a reference or not)
|
||||
/// for each parameter in the given function declaration.
|
||||
pub fn extract_parameter_names_types_and_borrows(fn_decl: &FnDecl)
|
||||
-> Result<Vec<(Pat, Type, TokenStream)>>
|
||||
{
|
||||
let mut result = Vec::new();
|
||||
let mut generated_pattern_counter = 0;
|
||||
for input in fn_decl.inputs.iter() {
|
||||
match input {
|
||||
FnArg::Captured(arg) => {
|
||||
let (ty, borrow) = match &arg.ty {
|
||||
Type::Reference(t) => {
|
||||
let ty = &t.elem;
|
||||
(parse_quote!( #ty ), quote!( & ))
|
||||
},
|
||||
t => { (t.clone(), quote!()) },
|
||||
};
|
||||
|
||||
let name =
|
||||
generate_unique_pattern(arg.pat.clone(), &mut generated_pattern_counter);
|
||||
result.push((name, ty, borrow));
|
||||
},
|
||||
_ => {
|
||||
return Err(
|
||||
Error::new(
|
||||
input.span(),
|
||||
"Only function arguments with the following \
|
||||
pattern are accepted: `name: type`!"
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
/// Generates the name for the native call generator function.
|
||||
pub fn generate_native_call_generator_fn_name(fn_name: &Ident) -> Ident {
|
||||
Ident::new(&format!("{}_native_call_generator", fn_name.to_string()), Span::call_site())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user