fuel_vm/storage/
blob_data.rs

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