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
//! Low-level storage primitive that holds one value.
use std::marker::PhantomData;

use oasis_contract_sdk::storage::{ConfidentialStore, PublicStore};

macro_rules! declare_cell {
    ($name:ident, $store:ident) => {
        /// A storage cell identifies a storage key of a specific type.
        pub struct $name<'key, T> {
            key: &'key [u8],
            _type: PhantomData<T>,
        }

        impl<'key, T> $name<'key, T> {
            /// Create a new storage cell with the specified key and type.
            pub const fn new(key: &'key [u8]) -> Self {
                Self {
                    key,
                    _type: PhantomData,
                }
            }

            /// Clear the value in the storage cell.
            pub fn clear(&self, store: &mut dyn $store) {
                store.remove(self.key);
            }
        }

        impl<'key, T> $name<'key, T>
        where
            T: cbor::Decode,
        {
            /// Return the current value of the storage cell.
            ///
            /// # Panics
            ///
            /// The method will panic in case the raw cell value cannot be deserialized.
            ///
            pub fn get(&self, store: &dyn $store) -> Option<T> {
                store
                    .get(self.key)
                    .map(|raw| cbor::from_slice(&raw).unwrap())
            }
        }

        impl<'key, T> $name<'key, T>
        where
            T: cbor::Encode,
        {
            /// Set the value of the storage cell.
            pub fn set(&self, store: &mut dyn $store, value: T) {
                store.insert(self.key, &cbor::to_vec(value));
            }
        }
    };
}

declare_cell!(PublicCell, PublicStore);
declare_cell!(ConfidentialCell, ConfidentialStore);