ink_primitives/reflect/trait_def/
registry.rs

1// Copyright (C) Use Ink (UK) Ltd.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::{
16    contract::ContractEnv,
17    types::Environment,
18};
19use core::marker::PhantomData;
20
21/// Type that is guaranteed by ink! to implement all ink! trait definitions.
22///
23/// This guarantee is used by ink! itself and can be used by ink! smart contract
24/// authors to query static information about known ink! trait definitions.
25///
26/// # Codegen
27///
28/// - The `#[ink::trait_definition]` procedural macro generates an associated type called
29///   `__ink_TraitInfo` for each ink! trait definition.
30/// - Furthermore the ink! codegen implements the ink! trait definition for the
31///   `TraitDefinitionRegistry` with stub implementations for all methods that guarantee
32///   that they are never called.
33/// - For every implemented ink! trait definition an ink! trait info object type is
34///   generated that is linked to the global `TraitDefinitionRegistry` through the
35///   aforementioned `__ink_TraitInfo` associated type.
36/// - This trait info object type itself implements various traits each providing useful
37///   static reflection information to the rest of the codegen about the ink! trait
38///   definition.
39///
40/// # Usage
41///
42/// ```
43/// # use ink::reflect::TraitDefinitionRegistry;
44/// use ink_env::DefaultEnvironment;
45///
46/// #[ink::trait_definition]
47/// pub trait TraitDefinition {
48///     #[ink(message)]
49///     fn message(&self);
50/// }
51///
52/// /// Access the generated ink! trait info object type like this:
53/// type TraitInfo =
54///     <TraitDefinitionRegistry<DefaultEnvironment> as TraitDefinition>::__ink_TraitInfo;
55/// ```
56pub struct TraitDefinitionRegistry<E> {
57    marker: PhantomData<fn() -> E>,
58}
59
60impl<E> ContractEnv for TraitDefinitionRegistry<E>
61where
62    E: Environment,
63{
64    type Env = E;
65}