Commit 7b7fb119 authored by Hero Bird's avatar Hero Bird

[pdsl_model] Extend messages! macro with manual identifiers

parent 3623341e
......@@ -18,9 +18,9 @@ state! {
messages! {
/// Increases the storage value by the given amount.
Inc(by: u32);
0 => Inc(by: u32);
/// Returns the storage value.
Get() -> u32;
1 => Get() -> u32;
}
fn instantiate() -> impl Contract {
......
......@@ -26,13 +26,13 @@ state! {
messages! {
/// Returns the total supply.
TotalSupply() -> Balance;
0 => TotalSupply() -> Balance;
/// Returns the balance of the given address.
BalanceOf(owner: Address) -> Balance;
1 => BalanceOf(owner: Address) -> Balance;
/// Transfers balance from the caller to the given address.
///
/// Returns `true` if the transfer was successful.
Transfer(to: Address, amount: Balance) -> bool;
2 => Transfer(to: Address, amount: Balance) -> bool;
}
fn instantiate() -> impl Contract {
......
#![cfg_attr(not(feature = "std"), no_std)]
#![feature(
const_fn,
const_str_as_bytes,
)]
#![allow(unused)]
#[macro_use]
......
use crate::{
msg_handler::MessageHandlerSelector,
};
/// A message with an expected input type and output (result) type.
pub trait Message {
......@@ -7,6 +10,11 @@ pub trait Message {
/// The output of the message, also known as return type.
type Output: parity_codec::Encode;
/// The user provided message selector.
///
/// This identifier must be unique for every message.
const ID: MessageHandlerSelector;
/// The name of the message.
///
/// # Note
......@@ -19,7 +27,8 @@ pub trait Message {
#[macro_export]
macro_rules! messages {
(
$( #[$msg_meta:meta] )* $msg_name:ident (
$( #[$msg_meta:meta] )*
$msg_id:literal => $msg_name:ident (
$( $param_name:ident : $param_ty:ty ),*
) -> $ret_ty:ty ;
......@@ -33,19 +42,28 @@ macro_rules! messages {
type Input = ($($param_ty),*);
type Output = $ret_ty;
const ID: $crate::MessageHandlerSelector = $crate::MessageHandlerSelector::new($msg_id);
const NAME: &'static [u8] = stringify!($msg_name).as_bytes();
}
messages!($($rest)*);
};
(
$( #[$msg_meta:meta] )* $msg_name:ident (
$( #[$msg_meta:meta] )*
$msg_id:literal => $msg_name:ident (
$( $param_name:ident : $param_ty:ty ),*
) ;
$($rest:tt)*
) => {
messages!( $( #[$msg_meta] )* $msg_name ( $( $param_name : $param_ty ),* ) -> (); $($rest)* );
messages!(
$( #[$msg_meta] )*
$msg_id => $msg_name (
$( $param_name : $param_ty ),*
) -> ();
$($rest)*
);
};
() => {};
}
......@@ -70,7 +70,14 @@ impl CallData {
/// A hash to identify a called function.
#[derive(Copy, Clone, PartialEq, Eq)]
pub struct MessageHandlerSelector(pub u64);
pub struct MessageHandlerSelector(u32);
impl MessageHandlerSelector {
/// Creates a new message handler selector from the given value.
pub const fn new(raw: u32) -> Self {
Self(raw)
}
}
/// A read-only message handler.
///
......
......@@ -18,9 +18,9 @@ state! {
messages! {
/// Increases the storage value by the given amount.
Inc(by: u32);
0 => Inc(by: u32);
/// Returns the storage value.
Get() -> u32;
1 => Get() -> u32;
}
fn instantiate() {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment