-
Notifications
You must be signed in to change notification settings - Fork 595
/
Copy pathtraits.cairo
290 lines (249 loc) · 7.09 KB
/
traits.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
use core::panics::Panic;
trait Copy<T>;
trait Drop<T>;
impl SnapshotCopy<T> of Copy<@T>;
impl SnapshotDrop<T> of Drop<@T>;
// TODO(spapini): When associated types are supported, support the general trait Add<X, Y>.
trait Add<T> {
fn add(lhs: T, rhs: T) -> T;
}
trait AddEq<T> {
fn add_eq(ref self: T, other: T);
}
// TODO(spapini): When associated types are supported, support the general trait Sub<X, Y>.
trait Sub<T> {
fn sub(lhs: T, rhs: T) -> T;
}
trait SubEq<T> {
fn sub_eq(ref self: T, other: T);
}
// TODO(spapini): When associated types are supported, support the general trait Mul<X, Y>.
trait Mul<T> {
fn mul(lhs: T, rhs: T) -> T;
}
trait MulEq<T> {
fn mul_eq(ref self: T, other: T);
}
// TODO(spapini): When associated types are supported, support the general trait Div<X, Y>.
trait Div<T> {
fn div(lhs: T, rhs: T) -> T;
}
trait DivEq<T> {
fn div_eq(ref self: T, other: T);
}
// TODO(spapini): When associated types are supported, support the general trait Rem<X, Y>.
trait Rem<T> {
fn rem(lhs: T, rhs: T) -> T;
}
trait RemEq<T> {
fn rem_eq(ref self: T, other: T);
}
// TODO(spapini): When associated types are supported, support the general trait DivRem<X, Y>.
/// Division with remainder.
trait DivRem<T> {
fn div_rem(lhs: T, rhs: NonZero<T>) -> (T, T);
}
trait PartialEq<T> {
fn eq(lhs: @T, rhs: @T) -> bool;
fn ne(lhs: @T, rhs: @T) -> bool;
}
impl PartialEqSnap<T, impl TEq: PartialEq<T>> of PartialEq<@T> {
fn eq(lhs: @@T, rhs: @@T) -> bool {
TEq::eq(*lhs, *rhs)
}
fn ne(lhs: @@T, rhs: @@T) -> bool {
TEq::ne(*lhs, *rhs)
}
}
// TODO(spapini): When associated types are supported, support the general trait BitAnd<X, Y>.
trait BitAnd<T> {
fn bitand(lhs: T, rhs: T) -> T;
}
// TODO(spapini): When associated types are supported, support the general trait BitOr<X, Y>.
trait BitOr<T> {
fn bitor(lhs: T, rhs: T) -> T;
}
// TODO(spapini): When associated types are supported, support the general trait BitXor<X, Y>.
trait BitXor<T> {
fn bitxor(lhs: T, rhs: T) -> T;
}
trait BitNot<T> {
fn bitnot(a: T) -> T;
}
trait PartialOrd<T> {
fn le(lhs: T, rhs: T) -> bool;
fn ge(lhs: T, rhs: T) -> bool;
fn lt(lhs: T, rhs: T) -> bool;
fn gt(lhs: T, rhs: T) -> bool;
}
/// Trait for conversion between types.
trait Into<T, S> {
fn into(self: T) -> S;
}
impl TIntoT<T> of Into<T, T> {
fn into(self: T) -> T {
self
}
}
/// Trait for fallible conversion between types.
trait TryInto<T, S> {
fn try_into(self: T) -> Option<S>;
}
trait Neg<T> {
fn neg(a: T) -> T;
}
trait Not<T> {
fn not(a: T) -> T;
}
/// The following two traits are for implementing the [] operator. Only one should be implemented
/// for each type. Both are not consuming of self, the first gets a snapshot of the object and
/// the second gets ref.
trait IndexView<C, I, V> {
fn index(self: @C, index: I) -> V;
}
trait Index<C, I, V> {
fn index(ref self: C, index: I) -> V;
}
trait Destruct<T> {
fn destruct(self: T) nopanic;
}
// TODO(spapini): Remove this, it can lead to multiple impls and unwanted Destruct implementation.
impl DestructFromDrop<T, impl TDrop: Drop<T>> of Destruct<T> {
#[inline(always)]
fn destruct(self: T) nopanic {}
}
trait PanicDestruct<T> {
fn panic_destruct(self: T, ref panic: Panic) nopanic;
}
impl PanicDestructForDestruct<T, impl TDestruct: Destruct<T>> of PanicDestruct<T> {
#[inline(always)]
fn panic_destruct(self: T, ref panic: Panic) nopanic {
TDestruct::destruct(self);
}
}
trait Default<T> {
fn default() -> T;
}
impl SnapshotDefault<T, impl TDefault: Default<T>, impl TDrop: Drop<T>> of Default<@T> {
#[inline(always)]
fn default() -> @T {
@Default::default()
}
}
/// Trait for types allowed as values in a Felt252Dict.
trait Felt252DictValue<T> {
/// Returns the default value for this type as a value in a Felt252Dict.
/// Should be logically equivalent to 0.
fn zero_default() -> T nopanic;
}
// Tuple Copy impls.
impl TupleSize0Copy of Copy<()>;
impl TupleSize1Copy<E0, impl E0Copy: Copy<E0>> of Copy<(E0, )>;
impl TupleSize2Copy<E0, E1, impl E0Copy: Copy<E0>, impl E1Copy: Copy<E1>> of Copy<(E0, E1)>;
impl TupleSize3Copy<
E0, E1, E2, impl E0Copy: Copy<E0>, impl E1Copy: Copy<E1>, impl E2Copy: Copy<E2>
> of Copy<(E0, E1, E2)>;
impl TupleSize4Copy<
E0,
E1,
E2,
E3,
impl E0Copy: Copy<E0>,
impl E1Copy: Copy<E1>,
impl E2Copy: Copy<E2>,
impl E3Copy: Copy<E3>
> of Copy<(E0, E1, E2, E3)>;
// Tuple Drop impls.
impl TupleSize0Drop of Drop<()>;
impl TupleSize1Drop<E0, impl E0Drop: Drop<E0>> of Drop<(E0, )>;
impl TupleSize2Drop<E0, E1, impl E0Drop: Drop<E0>, impl E1Drop: Drop<E1>> of Drop<(E0, E1)>;
impl TupleSize3Drop<
E0, E1, E2, impl E0Drop: Drop<E0>, impl E1Drop: Drop<E1>, impl E2Drop: Drop<E2>
> of Drop<(E0, E1, E2)>;
impl TupleSize4Drop<
E0,
E1,
E2,
E3,
impl E0Drop: Drop<E0>,
impl E1Drop: Drop<E1>,
impl E2Drop: Drop<E2>,
impl E2Drop: Drop<E3>
> of Drop<(E0, E1, E2, E3)>;
// Tuple PartialEq impls.
impl TupleSize0PartialEq of PartialEq<()> {
#[inline(always)]
fn eq(lhs: @(), rhs: @()) -> bool {
true
}
#[inline(always)]
fn ne(lhs: @(), rhs: @()) -> bool {
false
}
}
impl TupleSize1PartialEq<E0, impl E0PartialEq: PartialEq<E0>> of PartialEq<(E0, )> {
#[inline(always)]
fn eq(lhs: @(E0, ), rhs: @(E0, )) -> bool {
let (lhs, ) = lhs;
let (rhs, ) = rhs;
lhs == rhs
}
#[inline(always)]
fn ne(lhs: @(E0, ), rhs: @(E0, )) -> bool {
!(rhs == lhs)
}
}
impl TupleSize2PartialEq<
E0, E1, impl E0PartialEq: PartialEq<E0>, impl E1PartialEq: PartialEq<E1>
> of PartialEq<(E0, E1)> {
#[inline(always)]
fn eq(lhs: @(E0, E1), rhs: @(E0, E1)) -> bool {
let (lhs0, lhs1) = lhs;
let (rhs0, rhs1) = rhs;
(lhs0 == rhs0) && (lhs1 == rhs1)
}
#[inline(always)]
fn ne(lhs: @(E0, E1), rhs: @(E0, E1)) -> bool {
!(rhs == lhs)
}
}
impl TupleSize3PartialEq<
E0,
E1,
E2,
impl E0PartialEq: PartialEq<E0>,
impl E1PartialEq: PartialEq<E1>,
impl E2PartialEq: PartialEq<E2>
> of PartialEq<(E0, E1, E2)> {
#[inline(always)]
fn eq(lhs: @(E0, E1, E2), rhs: @(E0, E1, E2)) -> bool {
let (lhs0, lhs1, lhs2) = lhs;
let (rhs0, rhs1, rhs2) = rhs;
(lhs0 == rhs0) && (lhs1 == rhs1) && (lhs2 == rhs2)
}
#[inline(always)]
fn ne(lhs: @(E0, E1, E2), rhs: @(E0, E1, E2)) -> bool {
!(rhs == lhs)
}
}
impl TupleSize4PartialEq<
E0,
E1,
E2,
E3,
impl E0PartialEq: PartialEq<E0>,
impl E1PartialEq: PartialEq<E1>,
impl E2PartialEq: PartialEq<E2>,
impl E3PartialEq: PartialEq<E3>
> of PartialEq<(E0, E1, E2, E3)> {
#[inline(always)]
fn eq(lhs: @(E0, E1, E2, E3), rhs: @(E0, E1, E2, E3)) -> bool {
let (lhs0, lhs1, lhs2, lhs3) = lhs;
let (rhs0, rhs1, rhs2, rhs3) = rhs;
(lhs0 == rhs0) && (lhs1 == rhs1) && (lhs2 == rhs2) && (lhs3 == rhs3)
}
#[inline(always)]
fn ne(lhs: @(E0, E1, E2, E3), rhs: @(E0, E1, E2, E3)) -> bool {
!(rhs == lhs)
}
}