ink_env/
contract.rs

1/// Stores the used host environment type of the ink! smart contract.
2///
3/// # Note
4///
5/// The used host environment can be altered using the `env` configuration
6/// parameter in the `#[ink::contract]` parameters. For example if the user
7/// wanted to use an environment type definition called `MyEnvironment` they
8/// issue the ink! smart contract as follows:
9///
10/// ```no_compile
11/// #[ink::contract(env = MyEnvironment)]
12/// ```
13///
14/// # Usage: Default Environment
15///
16/// ```
17/// #[ink::contract]
18/// pub mod contract {
19///     #[ink(storage)]
20///     pub struct Contract {}
21///
22///     impl Contract {
23///         #[ink(constructor)]
24///         pub fn constructor() -> Self {
25///             Self {}
26///         }
27///
28///         #[ink(message)]
29///         pub fn message(&self) {}
30///     }
31/// }
32///
33/// use contract::Contract;
34///
35/// # use ink::env::ContractEnv;
36/// # use ink::codegen::utils::IsSameType;
37///
38/// // The following line only compiles successfully if both
39/// // `ink_env::DefaultEnvironment` and `<Contract as ContractEnv>::Env`
40/// // are of the same type.
41/// const _: IsSameType<<Contract as ContractEnv>::Env> =
42///     <IsSameType<ink_env::DefaultEnvironment>>::new();
43/// ```
44///
45/// # Usage: Custom Environment
46///
47/// ```
48/// # use ink_env::{Environment, DefaultEnvironment};
49/// #[derive(Clone)]
50/// pub struct CustomEnvironment {}
51///
52/// impl Environment for CustomEnvironment {
53///     const MAX_EVENT_TOPICS: usize = 4;
54///
55///     type AccountId = <DefaultEnvironment as Environment>::AccountId;
56///     type Balance = u64;
57///     type Hash = <DefaultEnvironment as Environment>::Hash;
58///     type BlockNumber = u32;
59///     type Timestamp = u64;
60///     type ChainExtension = <DefaultEnvironment as Environment>::ChainExtension;
61/// }
62///
63/// #[ink::contract(env = super::CustomEnvironment)]
64/// pub mod contract {
65///     #[ink(storage)]
66///     pub struct Contract {}
67///
68///     impl Contract {
69///         #[ink(constructor)]
70///         pub fn constructor() -> Self {
71///             Self {}
72///         }
73///
74///         #[ink(message)]
75///         pub fn message(&self) {}
76///     }
77/// }
78///
79/// use contract::Contract;
80/// # use ink::env::ContractEnv;
81/// # use ink::codegen::utils::IsSameType;
82///
83/// // The following line only compiles successfully if both
84/// // `CustomEnvironment` and `<Contract as ContractEnv>::Env`
85/// // are of the same type.
86/// const _: IsSameType<<Contract as ContractEnv>::Env> =
87///     <IsSameType<CustomEnvironment>>::new();
88///
89/// fn main() {}
90/// ```
91pub trait ContractEnv {
92    /// The environment type.
93    type Env: crate::Environment;
94}
95
96/// Refers to the generated ink! smart contract reference type.
97///
98/// # Note
99///
100/// Given an ink! storage struct with identifier `Contract` the ink! codegen produces
101/// the ink! root type `Contract` and the ink! reference type `ContractRef`.
102///
103/// This trait exists so that users can avoid using a generated identifier to refer to
104/// the generated reference type of the ink! smart contract.
105///
106/// # Usage
107///
108/// ```
109/// #[ink::contract]
110/// pub mod contract {
111///     #[ink(storage)]
112///     pub struct Contract {}
113///
114///     impl Contract {
115///         #[ink(constructor)]
116///         pub fn constructor() -> Self {
117///             Self {}
118///         }
119///
120///         #[ink(message)]
121///         pub fn message(&self) {}
122///     }
123/// }
124///
125/// use contract::{
126///     Contract,
127///     ContractRef,
128/// };
129/// # use ink::codegen::utils::IsSameType;
130/// # use ink::env::ContractReference;
131///
132/// // The following line only compiles successfully if both
133/// // `ContractReference` and `<Contract as ContractReference>::Type`
134/// // are of the same type.
135/// const _: IsSameType<<Contract as ContractReference>::Type> =
136///     <IsSameType<ContractRef>>::new();
137/// ```
138pub trait ContractReference {
139    /// The generated contract reference type.
140    type Type;
141}