1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use crate::{
    Mappable, MerkleRoot, MerkleRootStorage, StorageInspect, StorageMut, StorageMutate, StorageRead, StorageRef,
    StorageSize, StorageWrite,
};
use alloc::{borrow::Cow, vec::Vec};

impl<'a, T: StorageInspect<Type> + ?Sized, Type: Mappable> StorageInspect<Type> for &'a T {
    type Error = T::Error;

    fn get(&self, key: &Type::Key) -> Result<Option<Cow<'_, Type::OwnedValue>>, Self::Error> {
        <T as StorageInspect<Type>>::get(self, key)
    }

    fn contains_key(&self, key: &Type::Key) -> Result<bool, Self::Error> {
        <T as StorageInspect<Type>>::contains_key(self, key)
    }
}

impl<'a, T: StorageInspect<Type> + ?Sized, Type: Mappable> StorageInspect<Type> for &'a mut T {
    type Error = T::Error;

    fn get(&self, key: &Type::Key) -> Result<Option<Cow<'_, Type::OwnedValue>>, Self::Error> {
        <T as StorageInspect<Type>>::get(self, key)
    }

    fn contains_key(&self, key: &Type::Key) -> Result<bool, Self::Error> {
        <T as StorageInspect<Type>>::contains_key(self, key)
    }
}

impl<'a, T: StorageMutate<Type> + ?Sized, Type: Mappable> StorageMutate<Type> for &'a mut T {
    fn insert(&mut self, key: &Type::Key, value: &Type::Value) -> Result<Option<Type::OwnedValue>, Self::Error> {
        <T as StorageMutate<Type>>::insert(self, key, value)
    }

    fn remove(&mut self, key: &Type::Key) -> Result<Option<Type::OwnedValue>, Self::Error> {
        <T as StorageMutate<Type>>::remove(self, key)
    }
}

impl<'a, T: StorageSize<Type> + ?Sized, Type: Mappable> StorageSize<Type> for &'a T {
    fn size_of_value(&self, key: &<Type as Mappable>::Key) -> Result<Option<usize>, Self::Error> {
        <T as StorageSize<Type>>::size_of_value(self, key)
    }
}

impl<'a, T: StorageSize<Type> + ?Sized, Type: Mappable> StorageSize<Type> for &'a mut T {
    fn size_of_value(&self, key: &<Type as Mappable>::Key) -> Result<Option<usize>, Self::Error> {
        <T as StorageSize<Type>>::size_of_value(self, key)
    }
}

impl<'a, T: StorageRead<Type> + StorageSize<Type> + ?Sized, Type: Mappable> StorageRead<Type> for &'a T {
    fn read(&self, key: &<Type as Mappable>::Key, buf: &mut [u8]) -> Result<Option<usize>, Self::Error> {
        <T as StorageRead<Type>>::read(self, key, buf)
    }

    fn read_alloc(&self, key: &<Type as Mappable>::Key) -> Result<Option<alloc::vec::Vec<u8>>, Self::Error> {
        <T as StorageRead<Type>>::read_alloc(self, key)
    }
}

impl<'a, T: StorageRead<Type> + StorageSize<Type> + ?Sized, Type: Mappable> StorageRead<Type> for &'a mut T {
    fn read(&self, key: &<Type as Mappable>::Key, buf: &mut [u8]) -> Result<Option<usize>, Self::Error> {
        <T as StorageRead<Type>>::read(self, key, buf)
    }

    fn read_alloc(&self, key: &<Type as Mappable>::Key) -> Result<Option<alloc::vec::Vec<u8>>, Self::Error> {
        <T as StorageRead<Type>>::read_alloc(self, key)
    }
}

impl<'a, T: MerkleRootStorage<Key, Type> + ?Sized, Key, Type: Mappable> MerkleRootStorage<Key, Type> for &'a mut T {
    fn root(&self, key: &Key) -> Result<MerkleRoot, Self::Error> {
        <T as MerkleRootStorage<Key, Type>>::root(self, key)
    }
}

impl<'a, T: StorageInspect<Type>, Type: Mappable> StorageRef<'a, T, Type> {
    #[inline(always)]
    pub fn get(self, key: &Type::Key) -> Result<Option<Cow<'a, Type::OwnedValue>>, T::Error> {
        self.0.get(key)
    }

    #[inline(always)]
    pub fn contains_key(self, key: &Type::Key) -> Result<bool, T::Error> {
        self.0.contains_key(key)
    }
}

impl<'a, T, Type: Mappable> StorageRef<'a, T, Type> {
    #[inline(always)]
    pub fn root<Key>(self, key: &Key) -> Result<MerkleRoot, T::Error>
    where
        T: MerkleRootStorage<Key, Type>,
    {
        self.0.root(key)
    }
}

impl<'a, T: StorageRead<Type>, Type: Mappable> StorageRef<'a, T, Type> {
    #[inline(always)]
    pub fn read(&self, key: &<Type as Mappable>::Key, buf: &mut [u8]) -> Result<Option<usize>, T::Error> {
        self.0.read(key, buf)
    }

    #[inline(always)]
    pub fn read_alloc(&self, key: &<Type as Mappable>::Key) -> Result<Option<alloc::vec::Vec<u8>>, T::Error> {
        self.0.read_alloc(key)
    }
}

impl<'a, T: StorageInspect<Type>, Type: Mappable> StorageMut<'a, T, Type> {
    #[inline(always)]
    pub fn get(self, key: &Type::Key) -> Result<Option<Cow<'a, Type::OwnedValue>>, T::Error> {
        // Workaround, because compiler doesn't convert the lifetime to `'a` by default.
        let self_: &'a T = self.0;
        self_.get(key)
    }

    #[inline(always)]
    pub fn contains_key(self, key: &Type::Key) -> Result<bool, T::Error> {
        self.0.contains_key(key)
    }
}

impl<'a, T: StorageMutate<Type>, Type: Mappable> StorageMut<'a, T, Type> {
    #[inline(always)]
    pub fn insert(self, key: &Type::Key, value: &Type::Value) -> Result<Option<Type::OwnedValue>, T::Error> {
        self.0.insert(key, value)
    }

    #[inline(always)]
    pub fn remove(self, key: &Type::Key) -> Result<Option<Type::OwnedValue>, T::Error> {
        self.0.remove(key)
    }
}

impl<'a, T, Type: Mappable> StorageMut<'a, T, Type> {
    #[inline(always)]
    pub fn root<Key>(self, key: &Key) -> Result<MerkleRoot, T::Error>
    where
        T: MerkleRootStorage<Key, Type>,
    {
        self.0.root(key)
    }
}

impl<'a, T: StorageWrite<Type>, Type: Mappable> StorageMut<'a, T, Type> {
    #[inline(always)]
    pub fn write(&mut self, key: &Type::Key, buf: Vec<u8>) -> Result<usize, T::Error> {
        self.0.write(key, buf)
    }

    #[inline(always)]
    pub fn replace(&mut self, key: &Type::Key, buf: Vec<u8>) -> Result<(usize, Option<Vec<u8>>), T::Error>
    where
        T: StorageSize<Type>,
    {
        self.0.replace(key, buf)
    }

    #[inline(always)]
    pub fn take(&mut self, key: &Type::Key) -> Result<Option<Vec<u8>>, T::Error> {
        self.0.take(key)
    }
}