casper_node/types/
chunkable.rs1use std::{borrow::Cow, convert::Infallible};
2
3use casper_types::{
4 bytesrepr::{self, Bytes, ToBytes},
5 execution::{ExecutionResult, ExecutionResultV1, ExecutionResultV2},
6 Digest,
7};
8
9use super::value_or_chunk::HashingTrieRaw;
10
11pub trait Chunkable {
14 type Error: std::fmt::Debug;
16 fn as_bytes(&self) -> Result<Cow<Vec<u8>>, Self::Error>;
24
25 fn hash(&self) -> Result<Digest, Self::Error> {
28 let bytes = self.as_bytes()?;
29 Ok(Digest::hash_into_chunks_if_necessary(&bytes))
30 }
31}
32
33impl Chunkable for Vec<u8> {
34 type Error = Infallible;
35
36 fn as_bytes(&self) -> Result<Cow<Vec<u8>>, Self::Error> {
37 Ok(Cow::Borrowed(self))
38 }
39}
40
41impl Chunkable for Bytes {
42 type Error = Infallible;
43
44 fn as_bytes(&self) -> Result<Cow<Vec<u8>>, Self::Error> {
45 Ok(Cow::Borrowed(self.inner_bytes()))
46 }
47}
48
49impl Chunkable for HashingTrieRaw {
50 type Error = Infallible;
51
52 fn as_bytes(&self) -> Result<Cow<Vec<u8>>, Self::Error> {
53 Ok(Cow::Borrowed(self.inner().inner().inner_bytes()))
54 }
55}
56
57impl Chunkable for &Vec<ExecutionResult> {
58 type Error = bytesrepr::Error;
59
60 fn as_bytes(&self) -> Result<Cow<Vec<u8>>, Self::Error> {
61 Ok(Cow::Owned((*self).to_bytes()?))
62 }
63}
64
65impl Chunkable for Vec<ExecutionResult> {
66 type Error = bytesrepr::Error;
67
68 fn as_bytes(&self) -> Result<Cow<Vec<u8>>, Self::Error> {
69 Ok(Cow::Owned(self.to_bytes()?))
70 }
71}
72
73impl Chunkable for Vec<&ExecutionResultV1> {
74 type Error = bytesrepr::Error;
75
76 fn as_bytes(&self) -> Result<Cow<Vec<u8>>, Self::Error> {
77 Ok(Cow::Owned(self.to_bytes()?))
78 }
79}
80
81impl Chunkable for Vec<&ExecutionResultV2> {
82 type Error = bytesrepr::Error;
83
84 fn as_bytes(&self) -> Result<Cow<Vec<u8>>, Self::Error> {
85 Ok(Cow::Owned(self.to_bytes()?))
86 }
87}