Skip to main content

fuel_core/state/
iterable_key_value_view.rs

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