ic_storage/adapter/
iteration.rs1use super::{
3 Memory, Storable,
4 bridge::{Backing, Codec},
5 error::Result,
6};
7use crate::{DefaultMemoryImpl, RuntimeMemory, engine};
8use std::{borrow::Cow, marker::PhantomData};
9
10pub struct MapEntry<K, V> {
16 key: K,
17 bytes: Vec<u8>,
18 value: PhantomData<V>,
19}
20impl<K, V: Storable> MapEntry<K, V> {
21 pub const fn key(&self) -> &K {
23 &self.key
24 }
25 pub fn value(&self) -> Result<V> {
29 Ok(<Codec<V> as engine::Storable>::from_bytes(Cow::Borrowed(&self.bytes)).0)
30 }
31 pub fn into_pair(self) -> Result<(K, V)> {
35 Ok((
36 self.key,
37 <Codec<V> as engine::Storable>::from_bytes(Cow::Owned(self.bytes)).0,
38 ))
39 }
40 pub fn into_key(self) -> K {
42 self.key
43 }
44 pub fn into_value(self) -> Result<V> {
48 Ok(<Codec<V> as engine::Storable>::from_bytes(Cow::Owned(self.bytes)).0)
49 }
50}
51pub struct MapIter<'a, K: Storable + Ord + Clone, V: Storable, M: Memory = DefaultMemoryImpl>(
57 pub(super) engine::btreemap::Iter<'a, Codec<K>, Codec<V>, Backing<RuntimeMemory<M>>>,
58);
59fn entry<K: Storable + Ord + Clone, V: Storable, M: Memory>(
60 entry: engine::btreemap::LazyEntry<'_, Codec<K>, Codec<V>, Backing<RuntimeMemory<M>>>,
61) -> MapEntry<K, V> {
62 let (key, bytes) = entry.into_raw();
63 MapEntry {
64 key: key.0,
65 bytes,
66 value: PhantomData,
67 }
68}
69impl<K: Storable + Ord + Clone, V: Storable, M: Memory> Iterator for MapIter<'_, K, V, M> {
70 type Item = Result<MapEntry<K, V>>;
71 fn next(&mut self) -> Option<Self::Item> {
72 self.0.next().map(|e| Ok(entry(e)))
73 }
74 fn nth(&mut self, n: usize) -> Option<Self::Item> {
75 self.0.nth(n).map(|e| Ok(entry(e)))
76 }
77 fn last(mut self) -> Option<Self::Item> {
78 self.next_back()
80 }
81 fn count(self) -> usize {
82 self.0.count()
83 }
84 fn size_hint(&self) -> (usize, Option<usize>) {
85 self.0.size_hint()
86 }
87}
88impl<K: Storable + Ord + Clone, V: Storable, M: Memory> DoubleEndedIterator
89 for MapIter<'_, K, V, M>
90{
91 fn next_back(&mut self) -> Option<Self::Item> {
92 self.0.next_back().map(|e| Ok(entry(e)))
93 }
94 fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
95 self.0.nth_back(n).map(|e| Ok(entry(e)))
96 }
97}