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
use crate::*;
use core::ops::Deref;
use core::ops::DerefMut;
use core::marker::PhantomData;
use elrond_codec::*;

/// Internal key container for BorrowedMutStorage.
enum BorrowedMutStorageKey {
    Const(&'static [u8]),
    Generated(Vec<u8>),
}

impl BorrowedMutStorageKey {
    fn as_bytes(&self) -> &[u8] {
        match self {
            BorrowedMutStorageKey::Generated(v) => v.as_slice(),
            BorrowedMutStorageKey::Const(v) => v,
        }
    }
}

/// Contains a value taken from storage and a reference to the api.
/// The value can be changed and will be saved back to storage 
/// when the lifetime of the BorrowedMutStorage expires.
/// Optimization: will only save back to storage if the value is referenced with deref_mut(),
/// because only in such way can it be changed.
pub struct BorrowedMutStorage<'a, A, BigInt, BigUint, T>
where
    BigInt: Encode + 'static,
    BigUint: Encode + 'static,
    A: ContractHookApi<BigInt, BigUint> + ContractIOApi<BigInt, BigUint> + 'a,
    T: Encode + Decode,
{
    api: &'a A,
    key: BorrowedMutStorageKey,
    value: T,
    dirty: bool,
    _phantom1: PhantomData<BigInt>,
    _phantom2: PhantomData<BigUint>,
}

impl<'a, A, BigInt, BigUint, T> BorrowedMutStorage<'a, A, BigInt, BigUint, T>
where
    BigInt: Encode + 'static,
    BigUint: Encode + 'static,
    A: ContractHookApi<BigInt, BigUint> + ContractIOApi<BigInt, BigUint> + 'a,
    T: Encode + Decode,
{
    pub fn with_const_key(api: &'a A, key: &'static [u8]) -> Self {
        let value: T = storage_get(api, key);
        BorrowedMutStorage {
            api,
            key : BorrowedMutStorageKey::Const(key),
            value,
            dirty: false,
            _phantom1: PhantomData,
            _phantom2: PhantomData,
        }
    }

    pub fn with_generated_key(api: &'a A, key: Vec<u8>) -> Self {
        let value: T = storage_get(api, key.as_slice());
        BorrowedMutStorage {
            api,
            key : BorrowedMutStorageKey::Generated(key),
            value,
            dirty: false,
            _phantom1: PhantomData,
            _phantom2: PhantomData,
        }
    }
}


impl<'a, A, BigInt, BigUint, T> Drop for BorrowedMutStorage<'a, A, BigInt, BigUint, T>
where
    BigInt: Encode + 'static,
    BigUint: Encode + 'static,
    A: ContractHookApi<BigInt, BigUint> + ContractIOApi<BigInt, BigUint> + 'a,
    T: Encode + Decode,
{
    fn drop(&mut self) {
        if self.dirty {
            storage_set(self.api, self.key.as_bytes(), &self.value);
        }
    }
}

impl<'a, A, BigInt, BigUint, T> Deref for BorrowedMutStorage<'a, A, BigInt, BigUint, T>
where
    BigInt: Encode + 'static,
    BigUint: Encode + 'static,
    A: ContractHookApi<BigInt, BigUint> + ContractIOApi<BigInt, BigUint> + 'a,
    T: Encode + Decode,
{
    type Target = T;

    fn deref(&self) -> &T {
        &self.value
    }
}

impl<'a, A, BigInt, BigUint, T> DerefMut for BorrowedMutStorage<'a, A, BigInt, BigUint, T>
where
    BigUint: BigUintApi + 'static,
    BigInt: BigIntApi<BigUint> + 'static,
    A: ContractHookApi<BigInt, BigUint> + ContractIOApi<BigInt, BigUint> + 'a,
    T: Encode + Decode,
{
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.dirty = true;
        &mut self.value
    }
}