Skip to content
This repository was archived by the owner on Dec 12, 2025. It is now read-only.

Commit 60fc3f9

Browse files
committed
(dirty) iteration
1 parent 46141a8 commit 60fc3f9

File tree

2 files changed

+27
-34
lines changed

2 files changed

+27
-34
lines changed

docs-test-gen/templates/storey-container-impl.tpl

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -112,25 +112,6 @@ where
112112
}
113113
}
114114

115-
impl<V> IterableStorable for MyMap<V>
116-
where
117-
V: IterableStorable,
118-
<V as IterableStorable>::KeyDecodeError: std::fmt::Display,
119-
{
120-
type Key = (u32, V::Key);
121-
type KeyDecodeError = ();
122-
type Value = V::Value;
123-
type ValueDecodeError = V::ValueDecodeError;
124-
125-
fn decode_key(key: &[u8]) -> Result<Self::Key, ()> {
126-
todo!()
127-
}
128-
129-
fn decode_value(value: &[u8]) -> Result<Self::Value, Self::ValueDecodeError> {
130-
V::decode_value(value)
131-
}
132-
}
133-
134115
impl<V, S> MyMapAccess<V, S>
135116
where
136117
V: Storable,

src/pages/storey/container-impl/my-map.mdx

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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"
223211
impl<V> IterableStorable for MyMap<V>
224212
where
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

Comments
 (0)