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};
9
10use alloc::{
11    vec,
12    vec::Vec,
13};
14use educe::Educe;
15
16use fuel_types::bytes::Bytes;
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(pub Bytes);
53
54// TODO: Remove fixed size default when adding support for dynamic storage
55impl Default for ContractsStateData {
56    fn default() -> Self {
57        Self(vec![0u8; 32].into())
58    }
59}
60
61impl From<Vec<u8>> for ContractsStateData {
62    fn from(c: Vec<u8>) -> Self {
63        Self(c.into())
64    }
65}
66
67impl From<ContractsStateData> for Vec<u8> {
68    fn from(c: ContractsStateData) -> Vec<u8> {
69        c.0.into_inner()
70    }
71}
72
73impl From<&[u8]> for ContractsStateData {
74    fn from(c: &[u8]) -> Self {
75        Self(c.to_vec().into())
76    }
77}
78
79impl From<&mut [u8]> for ContractsStateData {
80    fn from(c: &mut [u8]) -> Self {
81        Self(c.to_vec().into())
82    }
83}
84
85impl Borrow<[u8]> for ContractsStateData {
86    fn borrow(&self) -> &[u8] {
87        &self.0
88    }
89}
90
91impl AsRef<[u8]> for ContractsStateData {
92    fn as_ref(&self) -> &[u8] {
93        self.0.as_ref()
94    }
95}
96
97impl AsMut<[u8]> for ContractsStateData {
98    fn as_mut(&mut self) -> &mut [u8] {
99        self.0.as_mut()
100    }
101}
102
103#[cfg(feature = "random")]
104impl Distribution<ContractsStateData> for Standard {
105    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> ContractsStateData {
106        ContractsStateData(rng.r#gen::<Bytes32>().to_vec().into())
107    }
108}
109
110impl IntoIterator for ContractsStateData {
111    type IntoIter = alloc::vec::IntoIter<Self::Item>;
112    type Item = u8;
113
114    fn into_iter(self) -> Self::IntoIter {
115        self.0.into_inner().into_iter()
116    }
117}