ps_datachunk/owned/
mod.rs1mod implementations;
2
3use crate::DataChunk;
4use crate::Result;
5use bytes::Bytes;
6use ps_hash::Hash;
7
8#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
9pub struct OwnedDataChunk {
11 hash: Hash,
12 data: Bytes,
13}
14
15impl OwnedDataChunk {
16 #[must_use]
20 pub fn bytes(&self) -> Bytes {
21 self.data.clone()
22 }
23
24 #[must_use]
25 pub fn data_ref(&self) -> &[u8] {
26 &self.data
27 }
28
29 #[must_use]
30 pub const fn hash_ref(&self) -> &Hash {
31 &self.hash
32 }
33
34 #[must_use]
35 pub const fn hash(&self) -> Hash {
36 self.hash
37 }
38
39 #[inline]
44 #[must_use]
45 pub const fn from_parts_unchecked(data: Bytes, hash: Hash) -> Self {
46 Self { hash, data }
47 }
48
49 pub fn from_data_and_hash_unchecked<D>(data: D, hash: Hash) -> Self
50 where
51 D: AsRef<[u8]> + Send + 'static,
52 {
53 Self::from_parts_unchecked(Bytes::from_owner(data), hash)
54 }
55
56 pub fn from_bytes(data: Bytes) -> Result<Self> {
58 let hash = ps_hash::hash(&data)?;
59
60 Ok(Self::from_parts_unchecked(data, hash))
61 }
62
63 pub fn from_data<D>(data: D) -> Result<Self>
65 where
66 D: AsRef<[u8]> + Send + 'static,
67 {
68 Self::from_bytes(Bytes::from_owner(data))
69 }
70}
71
72impl DataChunk for OwnedDataChunk {
73 fn data_ref(&self) -> &[u8] {
74 self.data_ref()
75 }
76 fn hash_ref(&self) -> &Hash {
77 self.hash_ref()
78 }
79
80 fn into_bytes(self) -> Bytes {
82 self.data
83 }
84
85 fn into_owned(self) -> Self {
87 self
88 }
89}