From a4ba74ed729dca14dcaa90005265c8fd51f63849 Mon Sep 17 00:00:00 2001 From: David Date: Tue, 7 Jul 2020 09:20:58 +0200 Subject: [PATCH] Document the `Call` derive macro (#137) * Document the `Call` derive macro * Obey the fmt * Update proc-macro/src/lib.rs Co-authored-by: Andrew Jones * Review feedback Co-authored-by: Andrew Jones --- examples/submit_and_watch.rs | 5 +++- proc-macro/src/lib.rs | 48 +++++++++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/examples/submit_and_watch.rs b/examples/submit_and_watch.rs index 81701f7383..c75137f994 100644 --- a/examples/submit_and_watch.rs +++ b/examples/submit_and_watch.rs @@ -16,7 +16,10 @@ use sp_keyring::AccountKeyring; use substrate_subxt::{ - balances::*, + balances::{ + TransferCallExt, + TransferEventExt, + }, ClientBuilder, DefaultNodeRuntime, PairSigner, diff --git a/proc-macro/src/lib.rs b/proc-macro/src/lib.rs index cecdfe397f..c6daa1e68a 100644 --- a/proc-macro/src/lib.rs +++ b/proc-macro/src/lib.rs @@ -88,7 +88,53 @@ pub fn module(args: TokenStream, input: TokenStream) -> TokenStream { module::module(args.into(), input.into()).into() } -decl_derive!([Call] => #[proc_macro_error] call); +decl_derive!( + [Call] => + /// Derive macro that implements [substrate_subxt::Call](../substrate_subxt/trait.Call.html) for your struct + /// and defines&implements the calls as an extension trait. + /// + /// Use the `Call` derive macro in tandem with the [#module](../substrate_subxt/attr.module.html) macro to extend + /// your struct to enable calls to substrate and to decode events. The struct maps to the corresponding Substrate runtime call, e.g.: + /// + /// ```ignore + /// decl_module! { + /// /* … */ + /// pub fn fun_stuff(origin, something: Vec) -> DispatchResult { /* … */ } + /// /* … */ + /// } + ///``` + /// + /// Implements [substrate_subxt::Call](../substrate_subxt/trait.Call.html) and adds an extension trait that + /// provides two methods named as your struct. + /// + /// Example: + /// ```rust,ignore + /// pub struct MyRuntime; + /// + /// impl System for MyRuntime { /* … */ } + /// impl Balances for MyRuntime { /* … */ } + /// + /// #[module] + /// pub trait MyTrait: System + Balances {} + /// + /// #[derive(Call)] + /// pub struct FunStuffCall { + /// /// Runtime marker. + /// pub _runtime: PhantomData, + /// /// The argument passed to the call.. + /// pub something: Vec, + /// } + /// ``` + /// + /// When building a [Client](../substrate_subxt/struct.Client.html) parameterised to `MyRuntime`, you have access to + /// two new methods: `fun_stuff()` and `fun_stuff_and_watch()` by way of the derived `FunStuffExt` + /// trait. The `_and_watch` variant makes the call and waits for the result. The fields of the + /// input struct become arguments to the calls (ignoring the marker field). + /// + /// Under the hood the implementation calls [submit()](../substrate_subxt/struct.Client.html#method.submit) and + /// [watch()](../substrate_subxt/struct.Client.html#method.watch) respectively. + #[proc_macro_error] call +); fn call(s: Structure) -> TokenStream { call::call(s).into() }