Skip to content

Commit

Permalink
Document the Call derive macro (paritytech#137)
Browse files Browse the repository at this point in the history
* Document the `Call` derive macro

* Obey the fmt

* Update proc-macro/src/lib.rs

Co-authored-by: Andrew Jones <ascjones@gmail.com>

* Review feedback

Co-authored-by: Andrew Jones <ascjones@gmail.com>
  • Loading branch information
dvdplm and ascjones authored Jul 7, 2020
1 parent 536c54c commit a4ba74e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
5 changes: 4 additions & 1 deletion examples/submit_and_watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

use sp_keyring::AccountKeyring;
use substrate_subxt::{
balances::*,
balances::{
TransferCallExt,
TransferEventExt,
},
ClientBuilder,
DefaultNodeRuntime,
PairSigner,
Expand Down
48 changes: 47 additions & 1 deletion proc-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>) -> 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<T: MyTrait> {
/// /// Runtime marker.
/// pub _runtime: PhantomData<T>,
/// /// The argument passed to the call..
/// pub something: Vec<u8>,
/// }
/// ```
///
/// 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()
}
Expand Down

0 comments on commit a4ba74e

Please sign in to comment.