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