1use std::fmt;
4use std::ops::Deref;
5
6use typst::foundations::Bytes;
7
8#[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#[derive(PartialEq, Eq)]
69pub struct PackArchiveBytes(Vec<u8>);
70
71impl PackArchiveBytes {
72 pub fn from_vec(bytes: Vec<u8>) -> Self {
74 Self(bytes)
75 }
76
77 pub fn len(&self) -> u64 {
79 u64::try_from(self.0.len()).unwrap_or(u64::MAX)
80 }
81
82 pub fn is_empty(&self) -> bool {
84 self.0.is_empty()
85 }
86
87 pub fn as_slice(&self) -> &[u8] {
89 &self.0
90 }
91
92 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}