-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathMove.cairo
130 lines (104 loc) · 3.34 KB
/
Move.cairo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#[starknet::contract]
mod ownership_move{
#[storage]
struct Storage{
}
#[derive(Drop)]
struct User {
name: felt252,
age: u8,
school: School
}
#[derive(Drop)]
struct School {
name: felt252
}
#[derive(Copy, Drop)]
struct Point {
x: u128,
y: u128,
}
#[derive(Copy)]
struct Point_Nodrop {
x: u128,
y: u128,
}
#[derive(Drop)]
struct Point_Drop {
x: u128,
y: u128,
}
#[derive(Destruct)]
struct Dict_Drop {
mapping: Felt252Dict<felt252>
}
// move variable example
fn move_variable() {
let x = ArrayTrait::<felt252>::new(); // x becomes the owner of the data
let y = x; // ownership is moved from x to y
let z = x; // this will cause a compile error
}
// move function example
fn move_function(){
let x = ArrayTrait::<felt252>::new(); // x comes into scope
takes_ownership(x); // x's value moves into the function
// ... and so is no longer valid here
// let y = x; // This would cause a compile error because x is no longer valid
} // Here, x goes out of scope, but because its value was moved, nothing happens
fn takes_ownership(some_array: Array<felt252>){ // some_array comes into scope
} // Here, some_string goes out of scope and `drop` is called, the backing memory is freed
// copy felt example, integers and felt implemented Copy trait by default
fn copy_felt(){
// uint and felt implements Copy trait by default
let x = 5; // x owns the value 5
let y = x; // a copy of x is generated and assigned to y
let z = x; // another copy of x is generated and assigned to z
}
// copy struct example, Point implemented Copy trait manually in the contract
fn copy_struct(){
// Point struct implements Copy trait
let p1 = Point { x: 5, y: 10 };
foo(p1); // a copy of p1 is generated and passed to foo()
foo(p1); // another copy of p1 is generated and passed to foo()
}
fn foo(p: Point) {
// do something with p
}
// no drop example
fn nodrop_struct(){
// let p1 = Point_Nodrop { x: 5, y: 10 };
// error: Variable not dropped.
}
// drop example
fn drop_struct(){
let p1 = Point_Drop { x: 5, y: 10 };
}
fn give_ownership(name: felt252, age: u8, school_name: felt252) -> User {
User { name: name, age: age, school: School { name: school_name } }
}
fn use_User(user: User) {}
fn school_name(school:School){}
#[external(v0)]
fn struct_move(self: @ContractState) -> u8{
let mut u = give_ownership('WTF', 3, 'WTF Academy');
//note: variable was previously used here:
school_name(u.school);
//error: Variable was previously moved.
//use_User(u);
let y = u.age;
y
}
#[external(v0)]
fn struct_move_second(self: @ContractState){
let mut u = give_ownership('WTF', 3, 'WTF Academy');
school_name(u.school);
u.school = School{
name:'new WTF'
};
use_User(u);
}
// destruct 示例
fn drop_struct(){
Dict_Drop { mapping: Default::default() };
}
}