@@ -211,22 +211,22 @@ While we have a functional collection by now, we can't perform iteration yet. In
211
211
impl <V > IterableStorable for MyMap <V >
212
212
where
213
213
V : IterableStorable ,
214
- < V as IterableStorable > :: KeyDecodeError : std :: fmt :: Display ,
214
+ V :: KeyDecodeError : std :: fmt :: Display ,
215
215
{
216
216
type Key = (u32 , V :: Key );
217
- type KeyDecodeError = () ;
217
+ type KeyDecodeError = String ;
218
218
type Value = V :: Value ;
219
219
type ValueDecodeError = V :: ValueDecodeError ;
220
220
221
- fn decode_key (key : & [u8 ]) -> Result <Self :: Key , () > {
221
+ fn decode_key (key : & [u8 ]) -> Result <Self :: Key , String > {
222
222
if key . len () < 4 {
223
- return Err (( ));
223
+ return Err (String :: from ( " Key too short " ));
224
224
}
225
225
226
- let key_arr = key [0 .. 4 ]. try_into (). map_err (| _ | ( ))? ;
227
- let this_key = u32 :: from_le_bytes (key_arr );
226
+ let key_arr = key [0 .. 4 ]. try_into (). map_err (| e | format! ( " Invalid key: {} " , e ))? ;
227
+ let this_key = u32 :: from_be_bytes (key_arr );
228
228
229
- let rest = V :: decode_key (& key [4 .. ]). map_err (| _ | ())? ;
229
+ let rest = V :: decode_key (& key [4 .. ]). map_err (| e | e . to_string ())? ;
230
230
231
231
Ok ((this_key , rest ))
232
232
}
@@ -260,22 +260,22 @@ use storey::storage::IterableStorage;
260
260
impl <V > IterableStorable for MyMap <V >
261
261
where
262
262
V : IterableStorable ,
263
- < V as IterableStorable > :: KeyDecodeError : std :: fmt :: Display ,
263
+ V :: KeyDecodeError : std :: fmt :: Display ,
264
264
{
265
265
type Key = (u32 , V :: Key );
266
- type KeyDecodeError = () ;
266
+ type KeyDecodeError = String ;
267
267
type Value = V :: Value ;
268
268
type ValueDecodeError = V :: ValueDecodeError ;
269
269
270
- fn decode_key (key : & [u8 ]) -> Result <Self :: Key , () > {
270
+ fn decode_key (key : & [u8 ]) -> Result <Self :: Key , String > {
271
271
if key . len () < 4 {
272
- return Err (( ));
272
+ return Err (String :: from ( " Key too short " ));
273
273
}
274
274
275
- let key_arr = key [0 .. 4 ]. try_into (). map_err (| _ | ( ))? ;
276
- let this_key = u32 :: from_le_bytes (key_arr );
275
+ let key_arr = key [0 .. 4 ]. try_into (). map_err (| e | format! ( " Invalid key: {} " , e ))? ;
276
+ let this_key = u32 :: from_be_bytes (key_arr );
277
277
278
- let rest = V :: decode_key (& key [4 .. ]). map_err (| _ | ())? ;
278
+ let rest = V :: decode_key (& key [4 .. ]). map_err (| e | e . to_string ())? ;
279
279
280
280
Ok ((this_key , rest ))
281
281
}
@@ -318,6 +318,43 @@ let result: Result<Vec<_>, _> = access.values().collect();
318
318
assert_eq! (result . unwrap (), vec! [100 , 200 , 300 ]);
319
319
```
320
320
321
+ This isn't all. What we've also enabled is the ability to iterate over outer containers.
322
+
323
+ Let's create a regular ` Map ` , nest our ` MyMap ` inside it, and see what we can do!
324
+
325
+ ``` rust template="storey-container-impl-iter"
326
+ use cw_storey :: containers :: {Item , Map };
327
+ use storey :: containers :: IterableAccessor as _;
328
+
329
+ const MAP_IX : u8 = 1 ;
330
+
331
+ let map : Map <String , MyMap <Item <u32 >>> = Map :: new (MAP_IX );
332
+ let mut access = map . access (& mut storage );
333
+
334
+ access . entry_mut (" alice" ). entry_mut (1 ). set (& 100 ). unwrap ();
335
+ access . entry_mut (" alice" ). entry_mut (2 ). set (& 200 ). unwrap ();
336
+ access . entry_mut (" bob" ). entry_mut (1 ). set (& 1100 ). unwrap ();
337
+ access . entry_mut (" bob" ). entry_mut (3 ). set (& 1300 ). unwrap ();
338
+
339
+ let result : Result <Vec <_ >, _ > = access . pairs (). collect ();
340
+ assert_eq! (result . unwrap (), vec! [
341
+ ((" bob" . to_string (), (1 , ())), 1100 ),
342
+ ((" bob" . to_string (), (3 , ())), 1300 ),
343
+ ((" alice" . to_string (), (1 , ())), 100 ),
344
+ ((" alice" . to_string (), (2 , ())), 200 )
345
+ ]);
346
+ ```
347
+
348
+ We can iterate over everything. Note we didn't have to write any special logic for this nesting in our custom container's implementation.
349
+ It's well integrated with Storey simply by implementing a few traits.
350
+
351
+ <Callout >
352
+ We know the parenthesized, nested keys are ugly. We'll eventually try to make this
353
+ prettier.
354
+
355
+ For now, remember this ugliness allows unlimited depth of nested keys, which is pretty cool! Recursion rocks. Sometimes.
356
+ </Callout >
357
+
321
358
[ `Item` ] : /storey/containers/item
322
359
[ `Map` ] : /storey/containers/map
323
360
[ `Storable` ] : https://docs.rs/storey/latest/storey/containers/trait.Storable.html
0 commit comments