fuel_vm/storage/
contracts_state.rs

1use core::borrow::Borrow;
2
3use crate::double_key;
4use fuel_storage::Mappable;
5use fuel_types::{
6    Bytes32,
7    ContractId,
8    fmt_truncated_hex,
9};
10
11use alloc::{
12    vec,
13    vec::Vec,
14};
15use educe::Educe;
16
17#[cfg(feature = "random")]
18use rand::{
19    Rng,
20    distributions::{
21        Distribution,
22        Standard,
23    },
24};
25
26/// The storage table for contract's hashed key-value state.
27///
28/// Lifetime is for optimization to avoid `clone`.
29pub struct ContractsState;
30
31impl Mappable for ContractsState {
32    type Key = Self::OwnedKey;
33    /// The table key is combination of the `ContractId` and `Bytes32` hash of the value's
34    /// key.
35    type OwnedKey = ContractsStateKey;
36    type OwnedValue = ContractsStateData;
37    type Value = [u8];
38}
39
40double_key!(
41    ContractsStateKey,
42    ContractId,
43    contract_id,
44    Bytes32,
45    state_key
46);
47
48/// Storage type for contract state
49#[derive(Educe, Clone, PartialEq, Eq, Hash)]
50#[educe(Debug)]
51#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
52pub struct ContractsStateData(
53    #[educe(Debug(method(fmt_truncated_hex::<16>)))] pub Vec<u8>,
54);
55
56// TODO: Remove fixed size default when adding support for dynamic storage
57impl Default for ContractsStateData {
58    fn default() -> Self {
59        Self(vec![0u8; 32])
60    }
61}
62
63impl From<Vec<u8>> for ContractsStateData {
64    fn from(c: Vec<u8>) -> Self {
65        Self(c)
66    }
67}
68
69impl From<&[u8]> for ContractsStateData {
70    fn from(c: &[u8]) -> Self {
71        Self(c.into())
72    }
73}
74
75impl From<&mut [u8]> for ContractsStateData {
76    fn from(c: &mut [u8]) -> Self {
77        Self(c.into())
78    }
79}
80
81impl From<ContractsStateData> for Vec<u8> {
82    fn from(c: ContractsStateData) -> Vec<u8> {
83        c.0
84    }
85}
86
87impl Borrow<[u8]> for ContractsStateData {
88    fn borrow(&self) -> &[u8] {
89        &self.0
90    }
91}
92
93impl AsRef<[u8]> for ContractsStateData {
94    fn as_ref(&self) -> &[u8] {
95        self.0.as_ref()
96    }
97}
98
99impl AsMut<[u8]> for ContractsStateData {
100    fn as_mut(&mut self) -> &mut [u8] {
101        self.0.as_mut()
102    }
103}
104
105#[cfg(feature = "random")]
106impl Distribution<ContractsStateData> for Standard {
107    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> ContractsStateData {
108        ContractsStateData(rng.r#gen::<Bytes32>().to_vec())
109    }
110}
111
112impl IntoIterator for ContractsStateData {
113    type IntoIter = alloc::vec::IntoIter<Self::Item>;
114    type Item = u8;
115
116    fn into_iter(self) -> Self::IntoIter {
117        self.0.into_iter()
118    }
119}