fn generate_call_api_at_calls(decl: &ItemTrait) -> Result<TokenStream> {
	let fns = decl.items.iter().filter_map(|i| match i {
		TraitItem::Method(ref m) => Some((&m.attrs, &m.sig)),
		_ => None,
	});

	let mut result = Vec::new();
	let crate_ = generate_crate_access(HIDDEN_INCLUDES_ID);

	// Generate a native call generator for each function of the given trait.
	for (attrs, fn_) in fns {
		let trait_name = &decl.ident;
		let trait_fn_name = prefix_function_with_trait(&trait_name, &fn_.ident);
		let fn_name = generate_call_api_at_fn_name(&fn_.ident);

		let attrs = remove_supported_attributes(&mut attrs.clone());

		if attrs.contains_key(RENAMED_ATTRIBUTE) && attrs.contains_key(CHANGED_IN_ATTRIBUTE) {
			return Err(Error::new(
				fn_.span(), format!("`{}` and `{}` are not supported at once.", RENAMED_ATTRIBUTE, CHANGED_IN_ATTRIBUTE)
			));
		}

		// We do not need to generate this function for a method that signature was changed.
		if attrs.contains_key(CHANGED_IN_ATTRIBUTE) {
			continue;
		}

		// Parse the renamed attributes.
		let mut renames = Vec::new();
		if let Some((_, a)) = attrs
			.iter()
			.find(|a| a.0 == &RENAMED_ATTRIBUTE)
		{
			let (old_name, version) = parse_renamed_attribute(a)?;
			renames.push((version, prefix_function_with_trait(&trait_name, &old_name)));
		}

		renames.sort_unstable_by(|l, r| r.cmp(l));
		let (versions, old_names) = renames.into_iter().fold(
			(Vec::new(), Vec::new()),
			|(mut versions, mut old_names), (version, old_name)| {
				versions.push(version);
				old_names.push(old_name);
				(versions, old_names)
			}
		);

		// Generate the generator function
		result.push(quote!(
			#[cfg(any(feature = "std", test))]
			pub fn #fn_name<
				R: #crate_::runtime_api::Encode + #crate_::runtime_api::Decode + PartialEq,
				NC: FnOnce() -> ::std::result::Result<R, &'static str> + ::std::panic::UnwindSafe,
				Block: #crate_::runtime_api::BlockT,
				T: #crate_::runtime_api::CallRuntimeAt<Block>,
			>(
				call_runtime_at: &T,
				at: &#crate_::runtime_api::BlockId<Block>,
				args: Vec<u8>,
				changes: &mut #crate_::runtime_api::OverlayedChanges,
				initialized_block: &mut Option<#crate_::runtime_api::BlockId<Block>>,
				native_call: Option<NC>,
				context: #crate_::runtime_api::ExecutionContext,
			) -> #crate_::error::Result<#crate_::runtime_api::NativeOrEncoded<R>> {
				let version = call_runtime_at.runtime_version_at(at)?;

				#(
					// Check if we need to call the function by an old name.
					if version.apis.iter().any(|(s, v)| {
						s == &ID && *v < #versions
					}) {
						return call_runtime_at.call_api_at::<R, fn() -> _>(
							at,
							#old_names,
							args,
							changes,
							initialized_block,
							None,
							context
						);
					}
				)*

				call_runtime_at.call_api_at(
					at,
					#trait_fn_name,
					args,
					changes,
					initialized_block,
					native_call,
					context
				)
			}
		));
	}

	Ok(quote!( #( #result )* ))
}
/// Generate the functions that call the api at a given block for a given trait method.
Generate the functions that call the api at a given block for a given trait method.
Generate the functions that call the api at a given block for a given trait method .
fn generate_call_api_at_calls(decl: &ItemTrait) -> Result<TokenStream> {
	let fns = decl.items.iter().filter_map(|i| match i {
		TraitItem::Method(ref m) => Some((&m.attrs, &m.sig)),
		_ => None,
	});
	let mut result = Vec::new();
	let crate_ = generate_crate_access(HIDDEN_INCLUDES_ID);
	// Generate a native call generator for each function of the given trait.
	for (attrs, fn_) in fns {
		let trait_name = &decl.ident;
		let trait_fn_name = prefix_function_with_trait(&trait_name, &fn_.ident);
		let fn_name = generate_call_api_at_fn_name(&fn_.ident);
		let attrs = remove_supported_attributes(&mut attrs.clone());
		if attrs.contains_key(RENAMED_ATTRIBUTE) && attrs.contains_key(CHANGED_IN_ATTRIBUTE) {
			return Err(Error::new(
				fn_.span(), format!("`{}` and `{}` are not supported at once.", RENAMED_ATTRIBUTE, CHANGED_IN_ATTRIBUTE)
			));
		}
		// We do not need to generate this function for a method that signature was changed.
		if attrs.contains_key(CHANGED_IN_ATTRIBUTE) {
			continue;
		}
		// Parse the renamed attributes.
		let mut renames = Vec::new();
		if let Some((_, a)) = attrs
			.iter()
			.find(|a| a.0 == &RENAMED_ATTRIBUTE)
		{
			let (old_name, version) = parse_renamed_attribute(a)?;
			renames.push((version, prefix_function_with_trait(&trait_name, &old_name)));
		}
		renames.sort_unstable_by(|l, r| r.cmp(l));
		let (versions, old_names) = renames.into_iter().fold(
			(Vec::new(), Vec::new()),
			|(mut versions, mut old_names), (version, old_name)| {
				versions.push(version);
				old_names.push(old_name);
				(versions, old_names)
			}
		);
		// Generate the generator function
		result.push(quote!(
			#[cfg(any(feature = "std", test))]
			pub fn #fn_name<
				R: #crate_::runtime_api::Encode + #crate_::runtime_api::Decode + PartialEq,
				NC: FnOnce() -> ::std::result::Result<R, &'static str> + ::std::panic::UnwindSafe,
				Block: #crate_::runtime_api::BlockT,
				T: #crate_::runtime_api::CallRuntimeAt<Block>,
			>(
				call_runtime_at: &T,
				at: &#crate_::runtime_api::BlockId<Block>,
				args: Vec<u8>,
				changes: &mut #crate_::runtime_api::OverlayedChanges,
				initialized_block: &mut Option<#crate_::runtime_api::BlockId<Block>>,
				native_call: Option<NC>,
				context: #crate_::runtime_api::ExecutionContext,
			) -> #crate_::error::Result<#crate_::runtime_api::NativeOrEncoded<R>> {
				let version = call_runtime_at.runtime_version_at(at)?;
				#(
					// Check if we need to call the function by an old name.
					if version.apis.iter().any(|(s, v)| {
						s == &ID && *v < #versions
					}) {
						return call_runtime_at.call_api_at::<R, fn() -> _>(
							at,
							#old_names,
							args,
							changes,
							initialized_block,
							None,
							context
						);
					}
				)*
				call_runtime_at.call_api_at(
					at,
					#trait_fn_name,
					args,
					changes,
					initialized_block,
					native_call,
					context
				)
			}
		));
	}
	Ok(quote!( #( #result )* ))
}
fn generate_call_api_at_calls ( decl : & ItemTrait ) -> Result < TokenStream > { let fns = decl . items . iter ( ) . filter_map ( | i | match i { TraitItem :: Method ( ref m ) => Some ( ( & m . attrs , & m . sig ) ) , _ => None , } ) ; let mut result = Vec :: new ( ) ; let crate_ = generate_crate_access ( HIDDEN_INCLUDES_ID ) ; // Generate a native call generator for each function of the given trait. for ( attrs , fn_ ) in fns { let trait_name = & decl . ident ; let trait_fn_name = prefix_function_with_trait ( & trait_name , & fn_ . ident ) ; let fn_name = generate_call_api_at_fn_name ( & fn_ . ident ) ; let attrs = remove_supported_attributes ( & mut attrs . clone ( ) ) ; if attrs . contains_key ( RENAMED_ATTRIBUTE ) && attrs . contains_key ( CHANGED_IN_ATTRIBUTE ) { return Err ( Error :: new ( fn_ . span ( ) , format ! ( " " RENAMED_ATTRIBUTE CHANGED_IN_ATTRIBUTE ) ) ) ; } // We do not need to generate this function for a method that signature was changed. if attrs . contains_key ( CHANGED_IN_ATTRIBUTE ) { continue ; } // Parse the renamed attributes. let mut renames = Vec :: new ( ) ; if let Some ( ( _ , a ) ) = attrs . iter ( ) . find ( | a | a . 0 == & RENAMED_ATTRIBUTE ) { let ( old_name , version ) = parse_renamed_attribute ( a ) ? ; renames . push ( ( version , prefix_function_with_trait ( & trait_name , & old_name ) ) ) ; } renames . sort_unstable_by ( | l , r | r . cmp ( l ) ) ; let ( versions , old_names ) = renames . into_iter ( ) . fold ( ( Vec :: new ( ) , Vec :: new ( ) ) , | ( mut versions , mut old_names ) , ( version , old_name ) | { versions . push ( version ) ; old_names . push ( old_name ) ; ( versions , old_names ) } ) ; // Generate the generator function result . push ( quote ! ( [ cfg ( any ( feature " " test ) ) ] pub fn fn_name R crate_ runtime_api Encode crate_ runtime_api Decode PartialEq NC FnOnce ( ) std result Result R ' static str std panic UnwindSafe Block crate_ runtime_api BlockT T crate_ runtime_api CallRuntimeAt Block ( call_runtime_at T at crate_ runtime_api BlockId Block args Vec u8 changes mut crate_ runtime_api OverlayedChanges initialized_block mut Option crate_ runtime_api BlockId Block native_call Option NC context crate_ runtime_api ExecutionContext ) crate_ error Result crate_ runtime_api NativeOrEncoded R { let version call_runtime_at runtime_version_at ( at ) ( // Check if we need to call the function by an old name. if version apis iter ( ) any ( ( s v ) { s ID v versions } ) { return call_runtime_at call_api_at R fn ( ) ( at old_names args changes initialized_block None context ) } ) call_runtime_at call_api_at ( at trait_fn_name args changes initialized_block native_call context ) } ) ) ; } Ok ( quote ! ( ( result ) ) ) }
Generate the functions that call the api at a given block for a given trait method.
Generate the functions that call the api at a given block for a given trait method .
decl
&ItemTrait
decl
&ItemTrait