Skip to main content

fuel_core/state/
key_value_view.rs

1use fuel_core_storage::{
2    Result as StorageResult,
3    StorageReadError,
4    kv_store::{
5        KeyValueInspect,
6        StorageColumn,
7        Value,
8    },
9};
10use std::sync::Arc;
11
12#[derive(Clone)]
13pub struct KeyValueViewWrapper<Column>(
14    Arc<dyn KeyValueInspect<Column = Column> + Sync + Send>,
15);
16
17impl<Column> std::fmt::Debug for KeyValueViewWrapper<Column> {
18    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> core::fmt::Result {
19        f.debug_struct("KeyValueViewWrapper").finish()
20    }
21}
22
23impl<Column> KeyValueViewWrapper<Column> {
24    pub fn new<S>(storage: S) -> Self
25    where
26        S: KeyValueInspect<Column = Column> + Send + Sync + 'static,
27    {
28        Self(Arc::new(storage))
29    }
30}
31
32impl<Column> KeyValueInspect for KeyValueViewWrapper<Column>
33where
34    Column: StorageColumn,
35{
36    type Column = Column;
37
38    fn exists(&self, key: &[u8], column: Self::Column) -> StorageResult<bool> {
39        self.0.exists(key, column)
40    }
41
42    fn size_of_value(
43        &self,
44        key: &[u8],
45        column: Self::Column,
46    ) -> StorageResult<Option<usize>> {
47        self.0.size_of_value(key, column)
48    }
49
50    fn get(&self, key: &[u8], column: Self::Column) -> StorageResult<Option<Value>> {
51        self.0.get(key, column)
52    }
53
54    fn read_exact(
55        &self,
56        key: &[u8],
57        column: Self::Column,
58        offset: usize,
59        buf: &mut [u8],
60    ) -> StorageResult<Result<usize, StorageReadError>> {
61        self.0.read_exact(key, column, offset, buf)
62    }
63
64    fn read_zerofill(
65        &self,
66        key: &[u8],
67        column: Self::Column,
68        offset: usize,
69        buf: &mut [u8],
70    ) -> StorageResult<Result<usize, StorageReadError>> {
71        self.0.read_zerofill(key, column, offset, buf)
72    }
73}