Skip to main content

typst_pack/
payload.rs

1//! Byte ownership shared by immutable semantic values.
2
3use std::fmt;
4use std::ops::Deref;
5
6use typst::foundations::Bytes;
7
8/// Privately shared immutable payload bytes.
9#[derive(Clone, PartialEq, Eq, Hash)]
10pub(crate) struct SharedBytes(Bytes);
11
12impl SharedBytes {
13    pub(crate) fn new(data: Vec<u8>) -> Self {
14        Self(Bytes::new(data))
15    }
16
17    pub(crate) fn from_typst(data: Bytes) -> Self {
18        Self(data)
19    }
20
21    pub(crate) fn as_slice(&self) -> &[u8] {
22        self.0.as_slice()
23    }
24
25    pub(crate) fn to_typst(&self) -> Bytes {
26        self.0.clone()
27    }
28
29    pub(crate) fn into_vec(self) -> Vec<u8> {
30        self.0.into_vec()
31    }
32}
33
34impl AsRef<[u8]> for SharedBytes {
35    fn as_ref(&self) -> &[u8] {
36        self.as_slice()
37    }
38}
39
40impl Deref for SharedBytes {
41    type Target = [u8];
42
43    fn deref(&self) -> &Self::Target {
44        self.as_slice()
45    }
46}
47
48impl fmt::Debug for SharedBytes {
49    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
50        formatter
51            .debug_tuple("SharedBytes")
52            .field(&self.0.len())
53            .finish()
54    }
55}
56
57/// Exact uniquely owned bytes of one Pack Archive.
58///
59/// This value is intentionally not cloneable: retries transfer the same exact
60/// archive bytes rather than silently duplicating potentially large buffers.
61///
62/// ```compile_fail
63/// use typst_pack::PackArchiveBytes;
64///
65/// let archive = PackArchiveBytes::from(Vec::new());
66/// let duplicate = archive.clone();
67/// ```
68#[derive(PartialEq, Eq)]
69pub struct PackArchiveBytes(Vec<u8>);
70
71impl PackArchiveBytes {
72    /// Takes unique ownership of exact archive bytes without copying them.
73    pub fn from_vec(bytes: Vec<u8>) -> Self {
74        Self(bytes)
75    }
76
77    /// The exact archive byte length.
78    pub fn len(&self) -> u64 {
79        u64::try_from(self.0.len()).unwrap_or(u64::MAX)
80    }
81
82    /// Whether the archive contains no bytes.
83    pub fn is_empty(&self) -> bool {
84        self.0.is_empty()
85    }
86
87    /// Borrows the exact archive bytes.
88    pub fn as_slice(&self) -> &[u8] {
89        &self.0
90    }
91
92    /// Transfers the exact archive bytes back into their vector.
93    pub fn into_vec(self) -> Vec<u8> {
94        self.0
95    }
96}
97
98impl From<Vec<u8>> for PackArchiveBytes {
99    fn from(bytes: Vec<u8>) -> Self {
100        Self::from_vec(bytes)
101    }
102}
103
104impl AsRef<[u8]> for PackArchiveBytes {
105    fn as_ref(&self) -> &[u8] {
106        self.as_slice()
107    }
108}
109
110impl Deref for PackArchiveBytes {
111    type Target = [u8];
112
113    fn deref(&self) -> &Self::Target {
114        self.as_slice()
115    }
116}
117
118impl fmt::Debug for PackArchiveBytes {
119    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
120        formatter
121            .debug_tuple("PackArchiveBytes")
122            .field(&self.0.len())
123            .finish()
124    }
125}