1
+ // Might not be necessary from Cairo 2.11
1
2
pub impl ContractAddressDefault of Default <core :: starknet :: ContractAddress > {
2
3
fn default () -> core :: starknet :: ContractAddress {
3
4
core :: starknet :: contract_address_const :: <0 >()
4
5
}
5
6
}
6
7
7
- /// Handle data (de)serialization to be stored into
8
- /// the world storage.
9
- ///
10
- /// The default implementation of this trait uses Serde.
11
- pub trait DojoStore <T , + Serde <T >> {
12
- fn serialize (
13
- self : @ T , ref serialized : Array <felt252 >,
14
- ) {
15
- Serde :: <T >:: serialize (self , ref serialized );
16
- }
17
- fn deserialize (ref values : Span <felt252 >) -> Option <T > {
18
- Serde :: <T >:: deserialize (ref values )
8
+ /// Handle data (de)serialization to be stored into the world storage.
9
+ pub trait DojoStore <T > {
10
+ fn serialize (self : @ T , ref serialized : Array <felt252 >);
11
+ fn deserialize (ref values : Span <felt252 >) -> Option <T >;
12
+ }
13
+
14
+ /// The default implementation of DojoStore uses Serde.
15
+ mod default_impl {
16
+ pub impl SerdeBasedDojoStore <T , + Serde <T >> of super :: DojoStore <T > {
17
+ fn serialize (self : @ T , ref serialized : Array <felt252 >) {
18
+ Serde :: serialize (self , ref serialized );
19
+ }
20
+ fn deserialize (ref values : Span <felt252 >) -> Option <T > {
21
+ Serde :: <T >:: deserialize (ref values )
22
+ }
19
23
}
20
24
}
21
25
22
- impl DojoStore_felt252 of DojoStore <felt252 >;
23
- impl DojoStore_bool of DojoStore <bool >;
24
- impl DojoStore_u8 of DojoStore <u8 >;
25
- impl DojoStore_u16 of DojoStore <u16 >;
26
- impl DojoStore_u32 of DojoStore <u32 >;
27
- impl DojoStore_u64 of DojoStore <u64 >;
28
- impl DojoStore_u128 of DojoStore <u128 >;
29
- impl DojoStore_u256 of DojoStore <u256 >;
30
- impl DojoStore_i8 of DojoStore <i8 >;
31
- impl DojoStore_i16 of DojoStore <i16 >;
32
- impl DojoStore_i32 of DojoStore <i32 >;
33
- impl DojoStore_i64 of DojoStore <i64 >;
34
- impl DojoStore_i128 of DojoStore <i128 >;
35
- impl DojoStore_ContractAddress of DojoStore <starknet :: ContractAddress >;
36
- impl DojoStore_ClassHash of DojoStore <starknet :: ClassHash >;
37
- impl DojoStore_EthAddress of DojoStore <starknet :: EthAddress >;
38
- impl DojoStore_ByteArray of DojoStore <ByteArray >;
26
+ pub impl DojoStore_felt252 = default_impl :: SerdeBasedDojoStore <felt252 >;
27
+ pub impl DojoStore_bool = default_impl :: SerdeBasedDojoStore <bool >;
28
+ pub impl DojoStore_u8 = default_impl :: SerdeBasedDojoStore <u8 >;
29
+ pub impl DojoStore_u16 = default_impl :: SerdeBasedDojoStore <u16 >;
30
+ pub impl DojoStore_u32 = default_impl :: SerdeBasedDojoStore <u32 >;
31
+ pub impl DojoStore_u64 = default_impl :: SerdeBasedDojoStore <u64 >;
32
+ pub impl DojoStore_u128 = default_impl :: SerdeBasedDojoStore <u128 >;
33
+ pub impl DojoStore_u256 = default_impl :: SerdeBasedDojoStore <u256 >;
34
+ pub impl DojoStore_i8 = default_impl :: SerdeBasedDojoStore <i8 >;
35
+ pub impl DojoStore_i16 = default_impl :: SerdeBasedDojoStore <i16 >;
36
+ pub impl DojoStore_i32 = default_impl :: SerdeBasedDojoStore <i32 >;
37
+ pub impl DojoStore_i64 = default_impl :: SerdeBasedDojoStore <i64 >;
38
+ pub impl DojoStore_i128 = default_impl :: SerdeBasedDojoStore <i128 >;
39
+ pub impl DojoStore_ContractAddress = default_impl :: SerdeBasedDojoStore <starknet :: ContractAddress >;
40
+ pub impl DojoStore_ClassHash = default_impl :: SerdeBasedDojoStore <starknet :: ClassHash >;
41
+ pub impl DojoStore_EthAddress = default_impl :: SerdeBasedDojoStore <starknet :: EthAddress >;
42
+ pub impl DojoStore_ByteArray = default_impl :: SerdeBasedDojoStore <ByteArray >;
39
43
40
44
/// Specific implementation of DojoStore for Option<T>.
41
- impl DojoStore_option <T , + Serde < T >, + DojoStore <T >, + Serde < Option < T > >> of DojoStore <Option <T >> {
45
+ impl DojoStore_option <T , + DojoStore <T >> of DojoStore <Option <T >> {
42
46
fn serialize (self : @ Option <T >, ref serialized : Array <felt252 >) {
43
47
match self {
44
48
Option :: Some (x ) => {
@@ -63,7 +67,7 @@ impl DojoStore_option<T, +Serde<T>, +DojoStore<T>, +Serde<Option<T>>> of DojoSto
63
67
}
64
68
}
65
69
66
- fn serialize_array_helper <T , + Serde < T >, + DojoStore <T >, + Drop <T >>(
70
+ fn serialize_array_helper <T , + DojoStore <T >, + Drop <T >>(
67
71
mut input : Span <T >, ref output : Array <felt252 >,
68
72
) {
69
73
match input . pop_front () {
@@ -75,7 +79,7 @@ fn serialize_array_helper<T, +Serde<T>, +DojoStore<T>, +Drop<T>>(
75
79
}
76
80
}
77
81
78
- fn deserialize_array_helper <T , + Serde < T >, + DojoStore <T >, + Drop <T >>(
82
+ fn deserialize_array_helper <T , + DojoStore <T >, + Drop <T >>(
79
83
ref serialized : Span <felt252 >, mut curr_output : Array <T >, remaining : felt252 ,
80
84
) -> Option <Array <T >> {
81
85
if remaining == 0 {
@@ -85,10 +89,9 @@ fn deserialize_array_helper<T, +Serde<T>, +DojoStore<T>, +Drop<T>>(
85
89
deserialize_array_helper (ref serialized , curr_output , remaining - 1 )
86
90
}
87
91
88
-
89
92
/// Specific implementation of DojoStore for Array<T>,
90
93
/// to call DojoStore for array items instead of Serde directly.
91
- impl DojoStore_array <T , + Drop <T >, + Serde < T >, + DojoStore <T >> of DojoStore <Array <T >> {
94
+ impl DojoStore_array <T , + Drop <T >, + DojoStore <T >> of DojoStore <Array <T >> {
92
95
fn serialize (self : @ Array <T >, ref serialized : Array <felt252 >) {
93
96
DojoStore :: serialize (@ self . len (), ref serialized );
94
97
serialize_array_helper (self . span (), ref serialized );
@@ -100,4 +103,3 @@ impl DojoStore_array<T, +Drop<T>, +Serde<T>, +DojoStore<T>> of DojoStore<Array<T
100
103
deserialize_array_helper (ref values , arr , length )
101
104
}
102
105
}
103
-
0 commit comments