Skip to content

Commit 725ba7c

Browse files
Refactor #[ink::trait_definition] docs (#497)
* Refactor `#[ink::trait-definition]` docs * Path rename
1 parent 0578bf3 commit 725ba7c

File tree

3 files changed

+28
-71
lines changed

3 files changed

+28
-71
lines changed

docs/basics/trait-definitions.md

Lines changed: 11 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ Through the `#[ink::trait_definition]` proc. macro it is now possible to define
1111
This allows to define shared smart contract interfaces to different concrete implementations.
1212
Note that this ink! trait definition can be defined anywhere, even in another crate!
1313

14-
See our [`ERC20-Trait example contract`](https://github.com/use-ink/ink-examples/blob/main/trait-erc20/lib.rs)
15-
for an elaborate example which uses trait definitions.
14+
## Example
1615

17-
### Example
16+
### Definition
1817

1918
Defined in the `base_erc20.rs` module.
2019

@@ -31,6 +30,8 @@ pub trait BaseErc20 {
3130
}
3231
```
3332

33+
### Implementation
34+
3435
An ink! smart contract definition can then implement this trait definition as follows:
3536

3637
```rust
@@ -66,6 +67,8 @@ mod erc20 {
6667
}
6768
```
6869

70+
### Usage
71+
6972
Calling the above `Erc20` explicitly through its trait implementation can be done just as if it was normal Rust code:
7073

7174
```rust
@@ -84,74 +87,11 @@ use base_erc20::BaseErc20;
8487
assert_eq!(erc20.total_supply(), 1000);
8588
```
8689

90+
See our [`ERC20-Trait example contract`](https://github.com/use-ink/ink-examples/blob/main/trait-erc20/lib.rs)
91+
for an elaborate example which uses trait definitions.
92+
93+
## Limitations
94+
8795
There are still many limitations to ink! trait definitions and trait implementations.
8896
For example, it is not possible to define associated constants or types or have default implemented methods.
8997
These limitations exist because of technical intricacies, however, please expect that many of those will be tackled in future ink! releases.
90-
91-
92-
93-
94-
Marks trait definitions to ink! as special ink! trait definitions.
95-
96-
There are some restrictions that apply to ink! trait definitions that
97-
this macro checks. Also ink! trait definitions are required to have specialized
98-
structure so that the main [`#[ink::contract]`](https://use-ink.github.io/ink/ink/attr.contract.html) macro can
99-
properly generate code for its implementations.
100-
101-
# Example: Definition
102-
103-
```rust
104-
type Balance = <ink::env::DefaultEnvironment as ink::env::Environment>::Balance;
105-
106-
#[ink::trait_definition]
107-
pub trait Erc20 {
108-
/// Returns the total supply of the ERC-20 smart contract.
109-
#[ink(message)]
110-
fn total_supply(&self) -> Balance;
111-
112-
// etc.
113-
}
114-
```
115-
116-
# Example: Implementation
117-
118-
Given the above trait definition you can implement it as shown below:
119-
120-
```rust
121-
#[ink::contract]
122-
mod base_erc20 {
123-
/// We somehow cannot put the trait in the doc-test crate root due to bugs.
124-
#[ink::trait_definition]
125-
pub trait Erc20 {
126-
/// Returns the total supply of the ERC-20 smart contract.
127-
#[ink(message)]
128-
fn total_supply(&self) -> Balance;
129-
}
130-
131-
#[ink(storage)]
132-
pub struct BaseErc20 {
133-
total_supply: Balance,
134-
// etc ..
135-
}
136-
137-
impl BaseErc20 {
138-
/// Constructs a new ERC-20 compliant smart contract using the initial supply.
139-
#[ink(constructor)]
140-
fn new(initial_supply: Balance) -> Self {
141-
Self { total_supply: initial_supply }
142-
}
143-
}
144-
145-
impl Erc20 for BaseErc20 {
146-
/// Returns the total supply of the ERC-20 smart contract.
147-
#[ink(message)]
148-
fn total_supply(&self) -> Balance {
149-
self.total_supply
150-
}
151-
152-
// etc ..
153-
}
154-
}
155-
```
156-
157-
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
title: "#[ink::trait_definition]"
3+
slug: /macros-attributes/trait_definition
4+
hide_title: true
5+
---
6+
7+
![Text/trait Title Picture](/img/title/text/trait.svg)
8+
9+
Marks trait definitions to ink! as special ink! trait definitions.
10+
11+
There are some restrictions that apply to ink! trait definitions that this macro checks.
12+
Also ink! trait definitions are required to have specialized structure so that
13+
the main [`#[ink::contract]`](./contract.md) macro can properly generate code
14+
for its implementations.
15+
16+
[See our section on Trait Definitions](../basics/trait-definitions.md) for a detailed description and examples.

sidebars.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ module.exports = {
8686
"macros-attributes/selector",
8787
"macros-attributes/storage",
8888
"macros-attributes/topic",
89+
"macros-attributes/trait_definition",
8990
],
9091
"Storage & Data Structures": [
9192
"datastructures/overview",

0 commit comments

Comments
 (0)