@@ -49,8 +49,9 @@ The constructor is simple - it just initializes the fields.
49
49
50
50
The next step is to set up an accessor. Hold on tight! This will be a ride.
51
51
52
- ``` rust template="storage" {1, 9-11, 20-22, 25-56}
53
- use storey :: containers :: {NonTerminal , Storable };
52
+ ``` rust template="storage" {1-2, 10-11, 20-26, 29-66}
53
+ use storey :: containers :: {IterableStorable , NonTerminal , Storable };
54
+ use storey :: storage :: {IntoStorage , StorageBranch };
54
55
55
56
struct MyMap <V > {
56
57
prefix : u8 ,
@@ -60,7 +61,6 @@ struct MyMap<V> {
60
61
impl <V > MyMap <V >
61
62
where
62
63
V : Storable ,
63
- <V as Storable >:: KeyDecodeError : std :: fmt :: Display ,
64
64
{
65
65
pub const fn new (prefix : u8 ) -> Self {
66
66
Self {
69
69
}
70
70
}
71
71
72
- pub fn access <S >(& self , storage : S ) -> MyMapAccess <V , StorageBranch <S >> {
72
+ pub fn access <F , S >(& self , storage : F ) -> MyMapAccess <V , StorageBranch <S >>
73
+ where
74
+ (F ,): IntoStorage <S >,
75
+ {
76
+ let storage = (storage ,). into_storage ();
73
77
Self :: access_impl (StorageBranch :: new (storage , vec! [self . prefix]))
74
78
}
75
79
}
@@ -82,21 +86,27 @@ pub struct MyMapAccess<V, S> {
82
86
impl <V > Storable for MyMap <V >
83
87
where
84
88
V : Storable ,
85
- <V as Storable >:: KeyDecodeError : std :: fmt :: Display ,
86
89
{
87
90
type Kind = NonTerminal ;
88
91
type Accessor <S > = MyMapAccess <V , S >;
89
- type Key = (u32 , V :: Key );
90
- type KeyDecodeError = ();
91
- type Value = V :: Value ;
92
- type ValueDecodeError = V :: ValueDecodeError ;
93
92
94
93
fn access_impl <S >(storage : S ) -> MyMapAccess <V , S > {
95
94
MyMapAccess {
96
95
storage ,
97
96
phantom : std :: marker :: PhantomData ,
98
97
}
99
98
}
99
+ }
100
+
101
+ impl <V > IterableStorable for MyMap <V >
102
+ where
103
+ V : IterableStorable ,
104
+ <V as IterableStorable >:: KeyDecodeError : std :: fmt :: Display ,
105
+ {
106
+ type Key = (u32 , V :: Key );
107
+ type KeyDecodeError = ();
108
+ type Value = V :: Value ;
109
+ type ValueDecodeError = V :: ValueDecodeError ;
100
110
101
111
fn decode_key (key : & [u8 ]) -> Result <Self :: Key , ()> {
102
112
todo! ()
@@ -114,6 +124,8 @@ The `MyMapAccess` struct is our accessor. It's a facade that's used to actually
114
124
the collection given a ` Storage ` instance - this is usually a subspace of the "root" storage
115
125
backend.
116
126
127
+ TODO: rework this
128
+
117
129
The [ ` Storable ` ] trait is the main trait a container must implement. It's currently a mix of things.
118
130
The associated types tell the framework: | Associated type | Details |
119
131
| -------------------| -------------------------------------------------------------------------| |
@@ -138,8 +150,8 @@ container.
138
150
There's one thing we're missing for this to actually by useful. We need some methods for the
139
151
accessor.
140
152
141
- ``` rust template="storage" {2, 59-78 }
142
- use storey :: containers :: {NonTerminal , Storable };
153
+ ``` rust template="storage" {68-83 }
154
+ use storey :: containers :: {IterableStorable , NonTerminal , Storable };
143
155
use storey :: storage :: StorageBranch ;
144
156
145
157
struct MyMap <V > {
@@ -150,7 +162,6 @@ struct MyMap<V> {
150
162
impl <V > MyMap <V >
151
163
where
152
164
V : Storable ,
153
- <V as Storable >:: KeyDecodeError : std :: fmt :: Display ,
154
165
{
155
166
pub const fn new (prefix : u8 ) -> Self {
156
167
Self {
@@ -159,7 +170,11 @@ where
159
170
}
160
171
}
161
172
162
- pub fn access <S >(& self , storage : S ) -> MyMapAccess <V , StorageBranch <S >> {
173
+ pub fn access <F , S >(& self , storage : F ) -> MyMapAccess <V , StorageBranch <S >>
174
+ where
175
+ (F ,): IntoStorage <S >,
176
+ {
177
+ let storage = (storage ,). into_storage ();
163
178
Self :: access_impl (StorageBranch :: new (storage , vec! [self . prefix]))
164
179
}
165
180
}
@@ -172,21 +187,27 @@ pub struct MyMapAccess<V, S> {
172
187
impl <V > Storable for MyMap <V >
173
188
where
174
189
V : Storable ,
175
- <V as Storable >:: KeyDecodeError : std :: fmt :: Display ,
176
190
{
177
191
type Kind = NonTerminal ;
178
192
type Accessor <S > = MyMapAccess <V , S >;
179
- type Key = (u32 , V :: Key );
180
- type KeyDecodeError = ();
181
- type Value = V :: Value ;
182
- type ValueDecodeError = V :: ValueDecodeError ;
183
193
184
194
fn access_impl <S >(storage : S ) -> MyMapAccess <V , S > {
185
195
MyMapAccess {
186
196
storage ,
187
197
phantom : std :: marker :: PhantomData ,
188
198
}
189
199
}
200
+ }
201
+
202
+ impl <V > IterableStorable for MyMap <V >
203
+ where
204
+ V : IterableStorable ,
205
+ <V as IterableStorable >:: KeyDecodeError : std :: fmt :: Display ,
206
+ {
207
+ type Key = (u32 , V :: Key );
208
+ type KeyDecodeError = ();
209
+ type Value = V :: Value ;
210
+ type ValueDecodeError = V :: ValueDecodeError ;
190
211
191
212
fn decode_key (key : & [u8 ]) -> Result <Self :: Key , ()> {
192
213
todo! ()
@@ -223,15 +244,13 @@ Time to see this in action.
223
244
224
245
``` rust template="storey-container-impl"
225
246
use cw_storey :: containers :: {Item };
226
- use cw_storey :: CwStorage ;
227
247
228
248
use storey :: containers :: IterableAccessor as _;
229
249
230
250
const MAP_IX : u8 = 0 ;
231
251
232
252
let my_map : MyMap <Item <u32 >> = MyMap :: new (MAP_IX );
233
- let mut cw_storage = CwStorage (& mut storage );
234
- let mut access = my_map . access (& mut cw_storage );
253
+ let mut access = my_map . access (& mut storage );
235
254
236
255
access . entry_mut (1 ). set (& 100 ). unwrap ();
237
256
access . entry_mut (2 ). set (& 200 ). unwrap ();
0 commit comments