Trait DispatchableConstructorInfo

pub trait DispatchableConstructorInfo<const ID: u32> {
    type Input;
    type Storage;
    type Output;
    type Error;

    const IS_RESULT: bool;
    const CALLABLE: fn(_: Self::Input) -> Self::Output;
    const DECODE: fn(_: &mut &[u8]) -> Result<Self::Input, DispatchError>;
    const RETURN: fn(_: ReturnFlags, _: Result<(), &Self::Error>);
    const PAYABLE: bool;
    const SELECTOR: Option<[u8; 4]>;
    const LABEL: &'static str;
    const ABI: Abi;
}
Expand description

Stores various information of the respective dispatchable ink! constructor.

§Note

This trait is implemented by ink! for every dispatchable ink! constructor of the root ink! smart contract. The ID used in the trait reflects the chosen or derived selector of the dispatchable ink! constructor.

§Usage


#[ink::contract]
pub mod contract {
    #[ink(storage)]
    pub struct Contract {}

    impl Contract {
        #[ink(constructor)]
        pub fn constructor1() -> Self {
            Contract {}
        }

        #[ink(constructor, selector = 0xC0DECAFE)]
        pub fn constructor2(input1: i32, input2: i64) -> Self {
            Contract {}
        }

        #[ink(message)]
        pub fn message(&self) {}
    }
}

use contract::Contract;

/// Asserts that the constructor with the selector `ID` has the following properties.
///
/// # Note
///
/// The `In` and `Out` generic parameters describe the input and output types.
fn assert_constructor_info<In, const ID: u32>(selector: [u8; 4], label: &str)
where
    Contract: DispatchableConstructorInfo<{ ID }, Input = In>,
{
    assert_eq!(
        <Contract as DispatchableConstructorInfo<{ ID }>>::SELECTOR,
        Some(selector),
    );
    assert_eq!(
        <Contract as DispatchableConstructorInfo<{ ID }>>::LABEL,
        label,
    );
}

fn main() {
    assert_constructor_info::<(), { selector_id!("constructor1") }>(
        selector_bytes!("constructor1"),
        "constructor1",
    );
    assert_constructor_info::<(i32, i64), 0xC0DECAFE_u32>(
        [0xC0, 0xDE, 0xCA, 0xFE],
        "constructor2",
    );
}

Required Associated Constants§

const IS_RESULT: bool

True if the constructor returns a Result.

const CALLABLE: fn(_: Self::Input) -> Self::Output

The closure that can be used to dispatch into the dispatchable ink! constructor.

const DECODE: fn(_: &mut &[u8]) -> Result<Self::Input, DispatchError>

The closure for decoding constructor input.

const RETURN: fn(_: ReturnFlags, _: Result<(), &Self::Error>)

The closure for returning return data.

const PAYABLE: bool

Yields true if the dispatchable ink! constructor is payable.

const SELECTOR: Option<[u8; 4]>

The selector (if any) of the dispatchable ink! constructor.

const LABEL: &'static str

The label of the dispatchable ink! constructor.

const ABI: Abi

The ABI spec for the decoding the constructor call.

Required Associated Types§

type Input

Reflects the input types of the dispatchable ink! constructor.

type Storage

The ink! storage struct type.

type Output

Reflects the output type of the dispatchable ink! constructor.

type Error

The type of the error returned from the constructor. Infallible constructors will have () as the error type.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§