fuel_vm/storage/
blob_data.rs1use core::borrow::Borrow;
2
3use fuel_storage::Mappable;
4use fuel_types::{
5 BlobId,
6 fmt_truncated_hex,
7};
8
9use alloc::vec::Vec;
10use educe::Educe;
11
12#[cfg(feature = "random")]
13use rand::{
14 Rng,
15 distributions::{
16 Distribution,
17 Standard,
18 },
19};
20
21pub struct BlobData;
23
24impl Mappable for BlobData {
25 type Key = Self::OwnedKey;
26 type OwnedKey = BlobId;
27 type OwnedValue = BlobBytes;
28 type Value = [u8];
29}
30
31#[derive(Educe, Clone, PartialEq, Eq, Hash)]
33#[educe(Debug)]
34#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
35pub struct BlobBytes(#[educe(Debug(method(fmt_truncated_hex::<16>)))] pub Vec<u8>);
36
37impl From<Vec<u8>> for BlobBytes {
38 fn from(c: Vec<u8>) -> Self {
39 Self(c)
40 }
41}
42
43impl From<&[u8]> for BlobBytes {
44 fn from(c: &[u8]) -> Self {
45 Self(c.into())
46 }
47}
48
49impl From<&mut [u8]> for BlobBytes {
50 fn from(c: &mut [u8]) -> Self {
51 Self(c.into())
52 }
53}
54
55impl From<BlobBytes> for Vec<u8> {
56 fn from(c: BlobBytes) -> Vec<u8> {
57 c.0
58 }
59}
60
61impl Borrow<[u8]> for BlobBytes {
62 fn borrow(&self) -> &[u8] {
63 &self.0
64 }
65}
66
67impl AsRef<[u8]> for BlobBytes {
68 fn as_ref(&self) -> &[u8] {
69 self.0.as_ref()
70 }
71}
72
73impl AsMut<[u8]> for BlobBytes {
74 fn as_mut(&mut self) -> &mut [u8] {
75 self.0.as_mut()
76 }
77}
78
79#[cfg(feature = "random")]
80impl Distribution<BlobBytes> for Standard {
81 fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> BlobBytes {
82 let len = rng.gen_range(0..1024);
83 let mut val = Vec::new();
84 for _ in 0..len {
85 val.push(rng.r#gen());
86 }
87 BlobBytes(val)
88 }
89}
90
91impl IntoIterator for BlobBytes {
92 type IntoIter = alloc::vec::IntoIter<Self::Item>;
93 type Item = u8;
94
95 fn into_iter(self) -> Self::IntoIter {
96 self.0.into_iter()
97 }
98}