pub trait ConstructorReturnType<C> {
    type Output;
    type Error: Decode;

    const IS_RESULT: bool = false;

    // Required method
    fn ok(value: C) -> Self::Output;

    // Provided method
    fn err(_err: Self::Error) -> Option<Self::Output> { ... }
}
Expand description

Represents any type that can be returned from an ink! constructor. The following contract implements the four different return type signatures implementing this trait:

  • Self
  • Result<Self, Error>
  • Contract
  • Result<Contract, Error>
#[ink::contract]
mod contract {
    #[ink(storage)]
    pub struct Contract {}

    #[derive(Debug, PartialEq, Eq, scale::Encode, scale::Decode)]
    #[cfg_attr(feature = "std", derive(::scale_info::TypeInfo))]
    pub enum Error {
        Foo,
    }

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

        #[ink(constructor)]
        pub fn new_storage_name() -> Contract {
            Contract {}
        }

        #[ink(constructor)]
        pub fn new_result_self() -> Result<Self, Error> {
            Ok(Self {})
        }

        #[ink(constructor)]
        pub fn new_result_storage_name() -> Result<Contract, Error> {
            Ok(Contract {})
        }

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

These constructor return signatures are then used by the ContractRef codegen for the CreateBuilder::returns type parameter.

Required Associated Types§

source

type Output

The actual return type of the constructor.

  • If a constructor returns Self, then Output = Self
  • If a constructor returns a Result<Self, E>, then Output = Result<Self, E>
source

type Error: Decode

The error type of the constructor return type.

Provided Associated Constants§

source

const IS_RESULT: bool = false

Is true if Self is Result<C, E>.

Required Methods§

source

fn ok(value: C) -> Self::Output

Construct a success value of the Output type.

Provided Methods§

source

fn err(_err: Self::Error) -> Option<Self::Output>

Construct an error value of the Output type.

Result implementations should return Some(Err(err)), otherwise default to None.

Implementations on Foreign Types§

source§

impl<C, E> ConstructorReturnType<C> for Result<C, E>where C: ContractEnv + FromAccountId<<C as ContractEnv>::Env>, E: Decode,

Blanket implementation for a Result<Self> return type. Self in the context of a ContractRef inherent becomes the ContractRefs type.

source§

const IS_RESULT: bool = true

§

type Output = Result<C, E>

§

type Error = E

source§

fn ok(value: C) -> Self::Output

source§

fn err(err: Self::Error) -> Option<Self::Output>

Implementors§

source§

impl<C> ConstructorReturnType<C> for Cwhere C: ContractEnv + FromAccountId<<C as ContractEnv>::Env>,

Blanket implementation for ContractRef types, generated for cross-contract calls.

In the context of a ContractRef inherent, Self from a constructor return type will become the type of the ContractRef’s type.

§

type Output = C

§

type Error = ()