stratus/eth/storage/permanent/rocks/types/
bytes.rs

1use std::fmt::Debug;
2use std::fmt::Display;
3use std::ops::Deref;
4
5use revm::primitives::Bytes as RevmBytes;
6use rocksdb::WriteBatch;
7
8use crate::eth::primitives::Bytes;
9use crate::eth::storage::permanent::rocks::SerializeDeserializeWithContext;
10
11#[derive(Clone, Default, PartialEq, Eq, bincode::Encode, bincode::Decode, serde::Serialize, serde::Deserialize)]
12#[cfg_attr(test, derive(fake::Dummy))]
13pub struct BytesRocksdb(pub Vec<u8>);
14
15impl Deref for BytesRocksdb {
16    type Target = Vec<u8>;
17
18    fn deref(&self) -> &Self::Target {
19        &self.0
20    }
21}
22
23impl Display for BytesRocksdb {
24    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
25        if self.len() <= 256 {
26            write!(f, "{}", const_hex::encode_prefixed(&self.0))
27        } else {
28            write!(f, "too long")
29        }
30    }
31}
32
33impl Debug for BytesRocksdb {
34    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35        f.debug_tuple("Bytes").field(&self.to_string()).finish()
36    }
37}
38
39impl From<Bytes> for BytesRocksdb {
40    fn from(value: Bytes) -> Self {
41        Self(value.0)
42    }
43}
44
45impl From<BytesRocksdb> for Bytes {
46    fn from(value: BytesRocksdb) -> Self {
47        Self(value.0)
48    }
49}
50
51impl From<Vec<u8>> for BytesRocksdb {
52    fn from(value: Vec<u8>) -> Self {
53        Self(value)
54    }
55}
56
57impl From<RevmBytes> for BytesRocksdb {
58    fn from(value: RevmBytes) -> Self {
59        value.to_vec().into()
60    }
61}
62
63impl From<BytesRocksdb> for RevmBytes {
64    fn from(value: BytesRocksdb) -> Self {
65        value.to_vec().into()
66    }
67}
68
69impl From<WriteBatch> for BytesRocksdb {
70    fn from(batch: WriteBatch) -> Self {
71        Self(batch.data().to_vec())
72    }
73}
74
75impl BytesRocksdb {
76    pub fn to_write_batch(&self) -> WriteBatch {
77        WriteBatch::from_data(&self.0)
78    }
79}
80
81impl SerializeDeserializeWithContext for BytesRocksdb {}