1use std::ops::Deref;
2
3use bytes::Bytes;
4use ps_hash::hash;
5use rancor::{Error, Strategy};
6use rkyv::{
7 api::high::HighSerializer,
8 bytecheck::CheckBytes,
9 ser::allocator::ArenaHandle,
10 util::AlignedVec,
11 validation::{archive::ArchiveValidator, shared::SharedValidator, Validator},
12 Archive, Serialize,
13};
14
15use crate::{Arc, DataChunk, Hash, Result};
16
17#[derive(Debug, Clone)]
18pub struct AlignedDataChunk {
19 data: AlignedVec,
20 hash: Arc<Hash>,
21}
22
23impl AlignedDataChunk {
24 pub fn from_parts<D, H>(data: D, hash: H) -> Self
25 where
26 D: Into<AlignedVec>,
27 H: Into<Arc<Hash>>,
28 {
29 let data = data.into();
30 let hash = hash.into();
31
32 Self { data, hash }
33 }
34
35 pub fn from_data_vec(data: AlignedVec) -> Result<Self> {
36 let hash = hash(&data)?;
37
38 Ok(Self::from_parts(data, hash))
39 }
40
41 #[must_use]
42 pub fn is_empty(&self) -> bool {
43 self.data.is_empty()
44 }
45
46 #[must_use]
47 pub fn len(&self) -> usize {
48 self.data.len()
49 }
50}
51
52impl AsRef<[u8]> for AlignedDataChunk {
53 fn as_ref(&self) -> &[u8] {
54 self
55 }
56}
57
58impl Deref for AlignedDataChunk {
59 type Target = [u8];
60
61 fn deref(&self) -> &Self::Target {
62 &self.data
63 }
64}
65
66impl AlignedDataChunk {
67 pub fn try_from<T>(value: &T) -> Result<Self>
68 where
69 for<'a> T: Archive + Serialize<HighSerializer<AlignedVec, ArenaHandle<'a>, Error>>,
70 {
71 let data = rkyv::to_bytes::<Error>(value)
72 .map_err(|_| crate::PsDataChunkError::RkyvSerializationFailed)?;
73
74 Self::from_data_vec(data)
75 }
76
77 pub fn try_bytes_as<T: rkyv::Archive>(data: &[u8]) -> Result<&T::Archived>
78 where
79 for<'a> <T as rkyv::Archive>::Archived:
80 CheckBytes<Strategy<Validator<ArchiveValidator<'a>, SharedValidator>, rancor::Error>>,
81 {
82 rkyv::access::<T::Archived, Error>(data)
83 .map_err(|_| crate::PsDataChunkError::RkyvInvalidArchive)
84 }
85
86 pub fn try_as<T: rkyv::Archive>(&self) -> Result<&T::Archived>
87 where
88 for<'a> <T as Archive>::Archived:
89 CheckBytes<Strategy<Validator<ArchiveValidator<'a>, SharedValidator>, rancor::Error>>,
90 {
91 Self::try_bytes_as::<T>(self.data_ref())
92 }
93}
94
95impl DataChunk for AlignedDataChunk {
96 fn data_ref(&self) -> &[u8] {
97 self
98 }
99 fn hash_ref(&self) -> &Hash {
100 &self.hash
101 }
102 fn hash(&self) -> Arc<Hash> {
103 self.hash.clone()
104 }
105
106 fn into_bytes(self) -> Bytes {
108 Bytes::from_owner(self.data)
109 }
110
111 fn into_owned(self) -> crate::OwnedDataChunk {
113 let Self { data, hash } = self;
114
115 crate::OwnedDataChunk::from_data_and_hash(data, hash)
116 }
117}