fuel_vm/storage/
contracts_state.rs1use core::borrow::Borrow;
2
3use crate::double_key;
4use fuel_storage::Mappable;
5use fuel_types::{
6 Bytes32,
7 ContractId,
8};
9
10use alloc::vec::Vec;
11use educe::Educe;
12
13use fuel_types::bytes::Bytes;
14#[cfg(feature = "random")]
15use rand::{
16 Rng,
17 distributions::{
18 Distribution,
19 Standard,
20 },
21};
22
23pub struct ContractsState;
27
28impl Mappable for ContractsState {
29 type Key = Self::OwnedKey;
30 type OwnedKey = ContractsStateKey;
33 type OwnedValue = ContractsStateData;
34 type Value = [u8];
35}
36
37double_key!(
38 ContractsStateKey,
39 ContractId,
40 contract_id,
41 Bytes32,
42 state_key
43);
44
45#[derive(Educe, Clone, PartialEq, Eq, Hash)]
47#[educe(Debug)]
48#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
49pub struct ContractsStateData(pub Bytes);
50
51impl From<Vec<u8>> for ContractsStateData {
52 fn from(c: Vec<u8>) -> Self {
53 Self(c.into())
54 }
55}
56
57impl From<ContractsStateData> for Vec<u8> {
58 fn from(c: ContractsStateData) -> Vec<u8> {
59 c.0.into_inner()
60 }
61}
62
63impl From<&[u8]> for ContractsStateData {
64 fn from(c: &[u8]) -> Self {
65 Self(c.to_vec().into())
66 }
67}
68
69impl From<&mut [u8]> for ContractsStateData {
70 fn from(c: &mut [u8]) -> Self {
71 Self(c.to_vec().into())
72 }
73}
74
75impl Borrow<[u8]> for ContractsStateData {
76 fn borrow(&self) -> &[u8] {
77 &self.0
78 }
79}
80
81impl AsRef<[u8]> for ContractsStateData {
82 fn as_ref(&self) -> &[u8] {
83 self.0.as_ref()
84 }
85}
86
87impl AsMut<[u8]> for ContractsStateData {
88 fn as_mut(&mut self) -> &mut [u8] {
89 self.0.as_mut()
90 }
91}
92
93#[cfg(feature = "random")]
94impl Distribution<ContractsStateData> for Standard {
95 fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> ContractsStateData {
96 ContractsStateData(rng.r#gen::<Bytes32>().to_vec().into())
97 }
98}
99
100impl IntoIterator for ContractsStateData {
101 type IntoIter = alloc::vec::IntoIter<Self::Item>;
102 type Item = u8;
103
104 fn into_iter(self) -> Self::IntoIter {
105 self.0.into_inner().into_iter()
106 }
107}