Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
wip - sort of works...
Browse files Browse the repository at this point in the history
  • Loading branch information
tdimitrov committed Jul 7, 2022
1 parent 17f5be5 commit 7603728
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
22 changes: 10 additions & 12 deletions primitives/api/proc-macro/src/impl_runtime_apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,17 +403,18 @@ fn extend_with_runtime_decl_path(mut trait_: Path) -> Path {
fn generate_dummy_trait_impl(initial: &ItemImpl, trait_api_ver: u64) -> ItemImpl {
let mut dummy_impl_ = initial.clone();
let mut dummy_trait = dummy_impl_.trait_.as_ref().unwrap().clone();
let trait_name = &dummy_trait.1.segments.last().unwrap().ident;
let dummy_ident = format_ident!("{}V{}", trait_name, trait_api_ver.to_string());
let mut trait_path_segment = dummy_trait.1.segments.last().unwrap().clone();
trait_path_segment.ident =
format_ident!("{}V{}", trait_path_segment.ident, trait_api_ver.to_string());
dummy_trait.1.segments.pop();
let pos = dummy_trait.1.segments.len();
dummy_trait.1.segments.insert(pos, dummy_ident.into());
dummy_trait.1.segments.insert(pos, trait_path_segment);
dummy_impl_.trait_ = Some(dummy_trait);

// remove method bodies from the implementation to generate less bloat
for i in &mut dummy_impl_.items {
match i {
ImplItem::Method(m) => m.block = parse_quote!( { unimplemented!() } ),
ImplItem::Method(m) => m.block = parse_quote!({ unimplemented!() }),
_ => continue,
}
}
Expand All @@ -428,25 +429,22 @@ fn generate_api_impl_for_runtime(impls: &[ItemImpl]) -> Result<TokenStream> {
// We put `runtime` before each trait to get the trait that is intended for the runtime and
// we put the `RuntimeBlock` as first argument for the trait generics.
for impl_ in impls.iter() {
let mut impl_ = impl_.clone();
let trait_ = extract_impl_trait(&impl_, RequireQualifiedTraitPath::Yes)?.clone();
let trait_ = extend_with_runtime_decl_path(trait_);
let versions = get_api_version(&impl_.attrs);
let trait_api_ver = match versions.len() {
0 => None,
1 => Some(parse_runtime_api_version(versions.get(0).unwrap())?),
_ => return Err(Error::new(impl_.span(), "Empty trait path not possible!")),
};

let mut impl_ = impl_.clone();
let trait_ = extract_impl_trait(&impl_, RequireQualifiedTraitPath::Yes)?.clone();
let trait_ = extend_with_runtime_decl_path(trait_);

impl_.trait_.as_mut().unwrap().1 = trait_;
impl_.attrs = filter_cfg_attrs(&impl_.attrs);

if trait_api_ver.is_some() {
let t = generate_dummy_trait_impl(&impl_, trait_api_ver.unwrap());
eprintln!("TOKENS: {}",quote!(#t));
impls_prepared.push(t);
impls_prepared.push(generate_dummy_trait_impl(&impl_, trait_api_ver.unwrap()));
}

impls_prepared.push(impl_);
}

Expand Down
1 change: 1 addition & 0 deletions primitives/api/test/tests/decl_and_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ impl_runtime_apis! {
#[api_version(3)]
impl self::ApiWithCustomVersion<Block> for Runtime {
fn same_name() {}
fn new_one() {}
}

impl sp_api::Core<Block> for Runtime {
Expand Down

0 comments on commit 7603728

Please sign in to comment.