fuel_storage/
impls.rs

1use crate::{
2    Mappable,
3    MerkleRoot,
4    MerkleRootStorage,
5    StorageInspect,
6    StorageMut,
7    StorageMutate,
8    StorageRead,
9    StorageRef,
10    StorageSize,
11    StorageWrite,
12};
13use alloc::{
14    borrow::Cow,
15    vec::Vec,
16};
17
18impl<T: StorageInspect<Type> + ?Sized, Type: Mappable> StorageInspect<Type> for &'_ T {
19    type Error = T::Error;
20
21    fn get(
22        &self,
23        key: &Type::Key,
24    ) -> Result<Option<Cow<'_, Type::OwnedValue>>, Self::Error> {
25        <T as StorageInspect<Type>>::get(self, key)
26    }
27
28    fn contains_key(&self, key: &Type::Key) -> Result<bool, Self::Error> {
29        <T as StorageInspect<Type>>::contains_key(self, key)
30    }
31}
32
33impl<T: StorageInspect<Type> + ?Sized, Type: Mappable> StorageInspect<Type>
34    for &'_ mut T
35{
36    type Error = T::Error;
37
38    fn get(
39        &self,
40        key: &Type::Key,
41    ) -> Result<Option<Cow<'_, Type::OwnedValue>>, Self::Error> {
42        <T as StorageInspect<Type>>::get(self, key)
43    }
44
45    fn contains_key(&self, key: &Type::Key) -> Result<bool, Self::Error> {
46        <T as StorageInspect<Type>>::contains_key(self, key)
47    }
48}
49
50impl<T: StorageMutate<Type> + ?Sized, Type: Mappable> StorageMutate<Type> for &'_ mut T {
51    fn insert(
52        &mut self,
53        key: &Type::Key,
54        value: &Type::Value,
55    ) -> Result<(), Self::Error> {
56        <T as StorageMutate<Type>>::insert(self, key, value)
57    }
58
59    fn replace(
60        &mut self,
61        key: &Type::Key,
62        value: &Type::Value,
63    ) -> Result<Option<Type::OwnedValue>, Self::Error> {
64        <T as StorageMutate<Type>>::replace(self, key, value)
65    }
66
67    fn remove(&mut self, key: &Type::Key) -> Result<(), Self::Error> {
68        <T as StorageMutate<Type>>::remove(self, key)
69    }
70
71    fn take(&mut self, key: &Type::Key) -> Result<Option<Type::OwnedValue>, Self::Error> {
72        <T as StorageMutate<Type>>::take(self, key)
73    }
74}
75
76impl<T: StorageSize<Type> + ?Sized, Type: Mappable> StorageSize<Type> for &'_ T {
77    fn size_of_value(
78        &self,
79        key: &<Type as Mappable>::Key,
80    ) -> Result<Option<usize>, Self::Error> {
81        <T as StorageSize<Type>>::size_of_value(self, key)
82    }
83}
84
85impl<T: StorageSize<Type> + ?Sized, Type: Mappable> StorageSize<Type> for &'_ mut T {
86    fn size_of_value(
87        &self,
88        key: &<Type as Mappable>::Key,
89    ) -> Result<Option<usize>, Self::Error> {
90        <T as StorageSize<Type>>::size_of_value(self, key)
91    }
92}
93
94impl<T: StorageRead<Type> + StorageSize<Type> + ?Sized, Type: Mappable> StorageRead<Type>
95    for &'_ T
96{
97    fn read(
98        &self,
99        key: &<Type as Mappable>::Key,
100        offset: usize,
101        buf: &mut [u8],
102    ) -> Result<bool, Self::Error> {
103        <T as StorageRead<Type>>::read(self, key, offset, buf)
104    }
105
106    fn read_alloc(
107        &self,
108        key: &<Type as Mappable>::Key,
109    ) -> Result<Option<alloc::vec::Vec<u8>>, Self::Error> {
110        <T as StorageRead<Type>>::read_alloc(self, key)
111    }
112}
113
114impl<T: StorageRead<Type> + StorageSize<Type> + ?Sized, Type: Mappable> StorageRead<Type>
115    for &'_ mut T
116{
117    fn read(
118        &self,
119        key: &<Type as Mappable>::Key,
120        offset: usize,
121        buf: &mut [u8],
122    ) -> Result<bool, Self::Error> {
123        <T as StorageRead<Type>>::read(self, key, offset, buf)
124    }
125
126    fn read_alloc(
127        &self,
128        key: &<Type as Mappable>::Key,
129    ) -> Result<Option<alloc::vec::Vec<u8>>, Self::Error> {
130        <T as StorageRead<Type>>::read_alloc(self, key)
131    }
132}
133
134impl<T: StorageWrite<Type> + ?Sized, Type: Mappable> StorageWrite<Type> for &'_ mut T {
135    fn write_bytes(&mut self, key: &Type::Key, buf: &[u8]) -> Result<(), Self::Error> {
136        <T as StorageWrite<Type>>::write_bytes(self, key, buf)
137    }
138
139    fn replace_bytes(
140        &mut self,
141        key: &Type::Key,
142        buf: &[u8],
143    ) -> Result<Option<Vec<u8>>, Self::Error> {
144        <T as StorageWrite<Type>>::replace_bytes(self, key, buf)
145    }
146
147    fn take_bytes(&mut self, key: &Type::Key) -> Result<Option<Vec<u8>>, Self::Error> {
148        <T as StorageWrite<Type>>::take_bytes(self, key)
149    }
150}
151
152impl<T: MerkleRootStorage<Key, Type> + ?Sized, Key, Type: Mappable>
153    MerkleRootStorage<Key, Type> for &'_ T
154{
155    fn root(&self, key: &Key) -> Result<MerkleRoot, Self::Error> {
156        <T as MerkleRootStorage<Key, Type>>::root(self, key)
157    }
158}
159
160impl<T: MerkleRootStorage<Key, Type> + ?Sized, Key, Type: Mappable>
161    MerkleRootStorage<Key, Type> for &'_ mut T
162{
163    fn root(&self, key: &Key) -> Result<MerkleRoot, Self::Error> {
164        <T as MerkleRootStorage<Key, Type>>::root(self, key)
165    }
166}
167
168impl<'a, T: StorageInspect<Type>, Type: Mappable> StorageRef<'a, T, Type> {
169    #[inline(always)]
170    pub fn get(
171        self,
172        key: &Type::Key,
173    ) -> Result<Option<Cow<'a, Type::OwnedValue>>, T::Error> {
174        self.0.get(key)
175    }
176
177    #[inline(always)]
178    pub fn contains_key(self, key: &Type::Key) -> Result<bool, T::Error> {
179        self.0.contains_key(key)
180    }
181}
182
183impl<T, Type: Mappable> StorageRef<'_, T, Type> {
184    #[inline(always)]
185    pub fn root<Key>(self, key: &Key) -> Result<MerkleRoot, T::Error>
186    where
187        T: MerkleRootStorage<Key, Type>,
188    {
189        self.0.root(key)
190    }
191}
192
193impl<T: StorageRead<Type>, Type: Mappable> StorageRef<'_, T, Type> {
194    #[inline(always)]
195    pub fn read(
196        &self,
197        key: &<Type as Mappable>::Key,
198        offset: usize,
199        buf: &mut [u8],
200    ) -> Result<bool, T::Error> {
201        self.0.read(key, offset, buf)
202    }
203
204    #[inline(always)]
205    pub fn read_alloc(
206        &self,
207        key: &<Type as Mappable>::Key,
208    ) -> Result<Option<alloc::vec::Vec<u8>>, T::Error> {
209        self.0.read_alloc(key)
210    }
211}
212
213impl<'a, T: StorageInspect<Type>, Type: Mappable> StorageMut<'a, T, Type> {
214    #[inline(always)]
215    pub fn get(
216        self,
217        key: &Type::Key,
218    ) -> Result<Option<Cow<'a, Type::OwnedValue>>, T::Error> {
219        // Workaround, because compiler doesn't convert the lifetime to `'a` by default.
220        let self_: &'a T = self.0;
221        self_.get(key)
222    }
223
224    #[inline(always)]
225    pub fn contains_key(self, key: &Type::Key) -> Result<bool, T::Error> {
226        self.0.contains_key(key)
227    }
228}
229
230impl<T, Type> StorageMut<'_, T, Type>
231where
232    T: StorageMutate<Type>,
233    Type: Mappable,
234{
235    #[inline(always)]
236    pub fn insert(self, key: &Type::Key, value: &Type::Value) -> Result<(), T::Error> {
237        StorageMutate::insert(self.0, key, value)
238    }
239
240    #[inline(always)]
241    pub fn replace(
242        self,
243        key: &Type::Key,
244        value: &Type::Value,
245    ) -> Result<Option<Type::OwnedValue>, T::Error> {
246        StorageMutate::replace(self.0, key, value)
247    }
248
249    #[inline(always)]
250    pub fn remove(self, key: &Type::Key) -> Result<(), T::Error> {
251        StorageMutate::remove(self.0, key)
252    }
253
254    #[inline(always)]
255    pub fn take(self, key: &Type::Key) -> Result<Option<Type::OwnedValue>, T::Error> {
256        StorageMutate::take(self.0, key)
257    }
258}
259
260impl<T, Type: Mappable> StorageMut<'_, T, Type> {
261    #[inline(always)]
262    pub fn root<Key>(self, key: &Key) -> Result<MerkleRoot, T::Error>
263    where
264        T: MerkleRootStorage<Key, Type>,
265    {
266        self.0.root(key)
267    }
268}
269
270impl<T, Type> StorageMut<'_, T, Type>
271where
272    Type: Mappable,
273    T: StorageWrite<Type>,
274{
275    #[inline(always)]
276    pub fn write_bytes(&mut self, key: &Type::Key, buf: &[u8]) -> Result<(), T::Error> {
277        StorageWrite::write_bytes(self.0, key, buf)
278    }
279
280    #[inline(always)]
281    pub fn replace_bytes(
282        &mut self,
283        key: &Type::Key,
284        buf: &[u8],
285    ) -> Result<Option<Vec<u8>>, T::Error>
286    where
287        T: StorageSize<Type>,
288    {
289        StorageWrite::replace_bytes(self.0, key, buf)
290    }
291
292    #[inline(always)]
293    pub fn take_bytes(&mut self, key: &Type::Key) -> Result<Option<Vec<u8>>, T::Error> {
294        StorageWrite::take_bytes(self.0, key)
295    }
296}