ps_datachunk/aligned/
mod.rs1use 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::{DataChunk, Hash, Result};
16
17#[derive(Debug, Clone)]
18pub struct AlignedDataChunk {
19 data: AlignedVec,
20 hash: Hash,
21}
22
23impl AlignedDataChunk {
24 pub fn from_parts_unchecked<D, H>(data: D, hash: H) -> Self
25 where
26 D: Into<AlignedVec>,
27 H: Into<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_unchecked(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 =
72 rkyv::to_bytes::<Error>(value).map_err(|_| crate::DataChunkError::Serialization)?;
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).map_err(|_| crate::DataChunkError::InvalidArchive)
83 }
84
85 pub fn try_as<T: rkyv::Archive>(&self) -> Result<&T::Archived>
86 where
87 for<'a> <T as Archive>::Archived:
88 CheckBytes<Strategy<Validator<ArchiveValidator<'a>, SharedValidator>, rancor::Error>>,
89 {
90 Self::try_bytes_as::<T>(self.data_ref())
91 }
92}
93
94impl DataChunk for AlignedDataChunk {
95 fn data_ref(&self) -> &[u8] {
96 self
97 }
98 fn hash_ref(&self) -> &Hash {
99 &self.hash
100 }
101
102 fn into_bytes(self) -> Bytes {
104 Bytes::from_owner(self.data)
105 }
106
107 fn into_owned(self) -> crate::OwnedDataChunk {
109 let Self { data, hash } = self;
110
111 crate::OwnedDataChunk::from_data_and_hash_unchecked(data, hash)
112 }
113}