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
use fuel_storage::{Mappable, StorageInspect, StorageMutate};

use alloc::borrow::Cow;
use hashbrown::HashMap;

#[derive(Debug)]
pub struct StorageMap<Type: Mappable> {
    map: HashMap<Type::Key, Type::GetValue>,
}

impl<Type: Mappable> Default for StorageMap<Type> {
    fn default() -> Self {
        Self::new()
    }
}

impl<Type: Mappable> StorageMap<Type> {
    pub fn new() -> Self {
        Self {
            map: Default::default(),
        }
    }
}

impl<Type> StorageInspect<Type> for StorageMap<Type>
where
    Type: Mappable,
    Type::Key: Eq + core::hash::Hash + Clone,
    Type::GetValue: Clone,
{
    type Error = core::convert::Infallible;

    fn get(&self, key: &Type::Key) -> Result<Option<Cow<Type::GetValue>>, Self::Error> {
        let result = self.map.get(key);
        let value = result.map(Cow::Borrowed);
        Ok(value)
    }

    fn contains_key(&self, key: &Type::Key) -> Result<bool, Self::Error> {
        let contains = self.map.contains_key(key);
        Ok(contains)
    }
}

impl<Type> StorageMutate<Type> for StorageMap<Type>
where
    Type: Mappable,
    Type::Key: Eq + core::hash::Hash + Clone,
    Type::SetValue: Clone,
    Type::GetValue: Clone + From<Type::SetValue>,
{
    fn insert(
        &mut self,
        key: &Type::Key,
        value: &Type::SetValue,
    ) -> Result<Option<Type::GetValue>, Self::Error> {
        let previous = self.map.remove(key);

        self.map.insert(key.clone(), value.clone().into());
        Ok(previous)
    }

    fn remove(&mut self, key: &Type::Key) -> Result<Option<Type::GetValue>, Self::Error> {
        let value = self.map.remove(key);
        Ok(value)
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
    struct TestKey(u32);

    #[derive(Debug, Copy, Clone, PartialEq, Eq)]
    struct TestValue(u32);

    struct TestTable;

    impl Mappable for TestTable {
        type Key = TestKey;
        type SetValue = TestValue;
        type GetValue = Self::SetValue;
    }

    #[test]
    fn test_get_returns_value_for_given_key() {
        let key = TestKey(0);
        let mut store = StorageMap::<TestTable>::new();
        let _ = store.insert(&key, &TestValue(0));

        assert_eq!(store.get(&key).unwrap(), Some(Cow::Borrowed(&TestValue(0))));
    }
    #[test]
    fn test_get_returns_none_for_invalid_key() {
        let key = TestKey(0);
        let invalid_key = TestKey(1);
        let mut store = StorageMap::<TestTable>::new();
        let _ = store.insert(&key, &TestValue(0));

        assert_eq!(store.get(&invalid_key).unwrap(), None);
    }

    #[test]
    fn test_insert_existing_key_updates_value_for_given_key() {
        let key = TestKey(0);
        let mut store = StorageMap::<TestTable>::new();
        let _ = store.insert(&key, &TestValue(0));
        let _ = store.insert(&key, &TestValue(1));

        assert_eq!(store.get(&key).unwrap(), Some(Cow::Borrowed(&TestValue(1))));
    }

    #[test]
    fn test_remove_deletes_the_value_for_given_key() {
        let key = TestKey(0);
        let mut store = StorageMap::<TestTable>::new();
        let _ = store.insert(&key, &TestValue(0));
        let _ = store.remove(&key);

        assert_eq!(store.get(&key).unwrap(), None);
    }

    #[test]
    fn test_remove_returns_the_deleted_value_for_given_key() {
        let key = TestKey(0);
        let mut store = StorageMap::<TestTable>::new();
        let _ = store.insert(&key, &TestValue(0));

        assert_eq!(store.remove(&key).unwrap(), Some(TestValue(0)));
    }

    #[test]
    fn test_remove_returns_none_for_invalid_key() {
        let invalid_key = TestKey(0);
        let mut store = StorageMap::<TestTable>::new();

        assert_eq!(store.remove(&invalid_key).unwrap(), None);
    }

    #[test]
    fn test_contains_key_returns_true_for_valid_key() {
        let key = TestKey(0);
        let mut store = StorageMap::<TestTable>::new();
        let _ = store.insert(&key, &TestValue(0));

        assert_eq!(store.contains_key(&key).unwrap(), true);
    }

    #[test]
    fn test_contains_key_returns_false_for_invalid_key() {
        let invalid_key = TestKey(0);
        let store = StorageMap::<TestTable>::new();

        assert_eq!(store.contains_key(&invalid_key).unwrap(), false);
    }
}