@@ -205,21 +205,9 @@ assert_eq!(access.entry(2).get().unwrap(), Some(200));
205205
206206# Iteration
207207
208- To be continued.. .
208+ While we have a functional collection by now, we can't perform iteration yet. Instead of trying to implement iteration on our own, there are benefits to using abstractions ` storey ` provides. This means implementing ` IterableStorable ` for ` MyMap ` .
209209
210- | Associated type | Details |
211- | ------------------ | ------------------------------------------------------------------------------------------------------------------------- |
212- | ` Key ` | The key type to be returned by iterators. Here a tuple of ` u32 ` and the key type of the inner container. |
213- | ` KeyDecodeError ` | The error to be returned on invalid key data. Here we don't care, so we use ` () ` . In production, use a proper error type. |
214- | ` Value ` | The value type to be returned by iterators. Here it's delegated to the inner container. |
215- | ` ValueDecodeError ` | The error type for invalid value data. Delegated to the inner container. |
216-
217- The methods: | Method | Function |
218- | ----------------| -----------------------------------------------------------------------------| |
219- ` decode_value ` | Used for iteration. This is how the framework knows how to decode values given raw
220- data. |
221-
222- ```
210+ ``` rust template="storey-container-impl"
223211impl <V > IterableStorable for MyMap <V >
224212where
225213 V : IterableStorable ,
@@ -231,7 +219,16 @@ where
231219 type ValueDecodeError = V :: ValueDecodeError ;
232220
233221 fn decode_key (key : & [u8 ]) -> Result <Self :: Key , ()> {
234- todo!()
222+ if key . len () < 4 {
223+ return Err (());
224+ }
225+
226+ let key_arr = key [0 .. 4 ]. try_into (). map_err (| _ | ())? ;
227+ let this_key = u32 :: from_le_bytes (key_arr );
228+
229+ let rest = V :: decode_key (& key [4 .. ]). map_err (| _ | ())? ;
230+
231+ Ok ((this_key , rest ))
235232 }
236233
237234 fn decode_value (value : & [u8 ]) -> Result <Self :: Value , Self :: ValueDecodeError > {
@@ -240,6 +237,21 @@ where
240237}
241238```
242239
240+ Alright. Let's dive into the trait items!
241+
242+ | Associated type | Details |
243+ | ------------------ | ------------------------------------------------------------------------------------------------------------------------- |
244+ | ` Key ` | The key type to be returned by iterators. Here a tuple of ` u32 ` and the key type of the inner container. |
245+ | ` KeyDecodeError ` | The error to be returned on invalid key data. Here we don't care, so we use ` () ` . In production, use a proper error type. |
246+ | ` Value ` | The value type to be returned by iterators. Here it's delegated to the inner container. |
247+ | ` ValueDecodeError ` | The error type for invalid value data. Delegated to the inner container. |
248+
249+ | Method | Function |
250+ | ----------------| -----------------------------------------------------------------------------|
251+ | ` decode_key ` | This is how the framework knows how to decode keys given raw bytes. |
252+ | ` decode_value ` | This is how the framework knows how to decode values given raw bytes. In this case, we delegate to the inner collection. |
253+
254+
243255[ `Item` ] : /storey/containers/item
244256[ `Map` ] : /storey/containers/map
245257[ `Storable` ] : https://docs.rs/storey/latest/storey/containers/trait.Storable.html
0 commit comments