2

I have a code in which a function name is specified, it is referenced twice later on. This function is placed right below the macros declaration:

#[subxt::subxt(runtime_metadata_path = "polkadot_metadata.scale")]
pub mod polkadot {}

This looks to me like a procedural macro, as it takes some code(referenced via word: 'polkadot') and emits the code:

let api = ClientBuilder::new()
        .set_client(client)
        .build()
        .await?
        .to_runtime_api::<polkadot::RuntimeApi<polkadot::DefaultConfig>>();

I want to understand why the line pub mod polkadot {} was placed and how can I explore what API calls are provided in polkadot?

Aviral Srivastava
  • 4,058
  • 8
  • 29
  • 81
  • 2
    `pub mod polkadot {}` declares and exports a module named `polkadot` without any items, not a function. – Joe_Jingyu Dec 19 '21 at 03:37
  • What is the use case to export polkadot? Is that how macros code expands if we want to use a particular module? – Aviral Srivastava Dec 19 '21 at 04:29
  • 2
    *"How does ... work while using macros"* - this is an attribute macro, and it is given whatever it annotates, in this case a module definition. With that module, the macro can do almost whatever it wants. In this case it appears codegen related based on the file given. I [don't see it documented anywhere](https://github.com/paritytech/subxt/tree/master) exactly what this macro is designed to do, but you are able to see what it expands to. See [How do I see the expanded macro code that's causing my compile error?](https://stackoverflow.com/q/28580386/2189130) – kmdreko Dec 19 '21 at 04:39

1 Answers1

1

if you cargo install cargo-expand that will install a tool that allows you to see the expanded code:

https://github.com/dtolnay/cargo-expand

So if you put in the module name here you can see whatever heavy lifting the macro is doing for you: cargo expand path::to::module

Squirrel
  • 1,189
  • 12
  • 15