fuel_core/state/
iterable_key_value_view.rs

1use fuel_core_storage::{
2    Result as StorageResult,
3    iter::{
4        BoxedIter,
5        IterDirection,
6        IterableStore,
7    },
8    kv_store::{
9        KVItem,
10        KeyItem,
11        KeyValueInspect,
12        StorageColumn,
13        Value,
14    },
15};
16use std::sync::Arc;
17
18#[derive(Clone)]
19pub struct IterableKeyValueViewWrapper<Column>(
20    Arc<dyn IterableStore<Column = Column> + Sync + Send>,
21);
22
23impl<Column> IterableKeyValueViewWrapper<Column> {
24    pub fn into_inner(self) -> Arc<dyn IterableStore<Column = Column> + Sync + Send> {
25        self.0
26    }
27}
28
29impl<Column> std::fmt::Debug for IterableKeyValueViewWrapper<Column> {
30    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> core::fmt::Result {
31        f.debug_struct("IterableKeyValueViewWrapper").finish()
32    }
33}
34
35impl<Column> IterableKeyValueViewWrapper<Column> {
36    pub fn new<S>(storage: S) -> Self
37    where
38        S: IterableStore<Column = Column> + Send + Sync + 'static,
39    {
40        Self(Arc::new(storage))
41    }
42}
43
44impl<Column> KeyValueInspect for IterableKeyValueViewWrapper<Column>
45where
46    Column: StorageColumn,
47{
48    type Column = Column;
49
50    fn exists(&self, key: &[u8], column: Self::Column) -> StorageResult<bool> {
51        self.0.exists(key, column)
52    }
53
54    fn size_of_value(
55        &self,
56        key: &[u8],
57        column: Self::Column,
58    ) -> StorageResult<Option<usize>> {
59        self.0.size_of_value(key, column)
60    }
61
62    fn get(&self, key: &[u8], column: Self::Column) -> StorageResult<Option<Value>> {
63        self.0.get(key, column)
64    }
65
66    fn read(
67        &self,
68        key: &[u8],
69        column: Self::Column,
70        offset: usize,
71        buf: &mut [u8],
72    ) -> StorageResult<bool> {
73        self.0.read(key, column, offset, buf)
74    }
75}
76
77impl<Column> IterableStore for IterableKeyValueViewWrapper<Column>
78where
79    Column: StorageColumn,
80{
81    fn iter_store(
82        &self,
83        column: Self::Column,
84        prefix: Option<&[u8]>,
85        start: Option<&[u8]>,
86        direction: IterDirection,
87    ) -> BoxedIter<'_, KVItem> {
88        self.0.iter_store(column, prefix, start, direction)
89    }
90
91    fn iter_store_keys(
92        &self,
93        column: Self::Column,
94        prefix: Option<&[u8]>,
95        start: Option<&[u8]>,
96        direction: IterDirection,
97    ) -> BoxedIter<'_, KeyItem> {
98        self.0.iter_store_keys(column, prefix, start, direction)
99    }
100}