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