Debian Codegraph @

third_party/rust/object/src/build/bytes.rs

raw
1 use alloc::borrow::Cow;
2 use alloc::string::String;
3 use alloc::vec::Vec;
4 use core::fmt;
5
6 /// A byte slice.
7 ///
8 /// Uses copy-on-write to avoid unnecessary allocations. The bytes can be
9 /// accessed as a slice using the `Deref` trait, or as a mutable `Vec` using the
10 /// `to_mut` method.
11 ///
12 /// Provides a `Debug` implementation that shows the first 8 bytes and the length.
13 #[derive(Default, Clone, PartialEq, Eq)]
14 pub struct Bytes<'a>(Cow<'a, [u8]>);
15
16 impl<'a> Bytes<'a> {
17 /// Acquire a mutable reference to the bytes.
18 ///
19 /// Clones the bytes if they are shared.
20 pub fn to_mut(&mut self) -> &mut Vec<u8> {
21 self.0.to_mut()
22 }
23
24 /// Get the bytes as a slice.
25 pub fn as_slice(&self) -> &[u8] {
26 self.0.as_ref()
27 }
28 }
29
30 impl<'a> core::ops::Deref for Bytes<'a> {
31 type Target = [u8];
32 fn deref(&self) -> &[u8] {
33 self.0.deref()
34 }
35 }
36
37 impl<'a> From<&'a [u8]> for Bytes<'a> {
38 fn from(bytes: &'a [u8]) -> Self {
39 Bytes(Cow::Borrowed(bytes))
40 }
41 }
42
43 impl<'a> From<Vec<u8>> for Bytes<'a> {
44 fn from(bytes: Vec<u8>) -> Self {
45 Bytes(Cow::Owned(bytes))
46 }
47 }
48
49 impl<'a> fmt::Debug for Bytes<'a> {
50 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51 debug_list_bytes(&self.0, f)
52 }
53 }
54
55 // Only for Debug impl of `Bytes`.
56 fn debug_list_bytes(bytes: &[u8], fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
57 let mut list = fmt.debug_list();
58 list.entries(bytes.iter().take(8).copied().map(DebugByte));
59 if bytes.len() > 8 {
60 list.entry(&DebugLen(bytes.len()));
61 }
62 list.finish()
63 }
64
65 struct DebugByte(u8);
66
67 impl fmt::Debug for DebugByte {
68 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
69 write!(fmt, "0x{:02x}", self.0)
70 }
71 }
72
73 struct DebugLen(usize);
74
75 impl fmt::Debug for DebugLen {
76 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
77 write!(fmt, "...; {}", self.0)
78 }
79 }
80
81 /// A byte slice that is a string of an unknown encoding.
82 ///
83 /// Uses copy-on-write to avoid unnecessary allocations. The bytes can be
84 /// accessed as a slice using the `Deref` trait, or as a mutable `Vec` using the
85 /// `to_mut` method.
86 ///
87 /// Provides a `Debug` implementation that interprets the bytes as UTF-8.
88 #[derive(Default, Clone, PartialEq, Eq, Hash)]
89 pub struct ByteString<'a>(Cow<'a, [u8]>);
90
91 impl<'a> ByteString<'a> {
92 /// Acquire a mutable reference to the bytes.
93 ///
94 /// Clones the bytes if they are shared.
95 pub fn to_mut(&mut self) -> &mut Vec<u8> {
96 self.0.to_mut()
97 }
98
99 /// Get the bytes as a slice.
100 pub fn as_slice(&self) -> &[u8] {
101 self.0.as_ref()
102 }
103 }
104
105 impl<'a> core::borrow::Borrow<[u8]> for ByteString<'a> {
106 fn borrow(&self) -> &[u8] {
107 self.0.borrow()
108 }
109 }
110
111 impl<'a> core::ops::Deref for ByteString<'a> {
112 type Target = [u8];
113 fn deref(&self) -> &[u8] {
114 self.0.deref()
115 }
116 }
117
118 impl<'a> From<&'a [u8]> for ByteString<'a> {
119 fn from(bytes: &'a [u8]) -> Self {
120 ByteString(Cow::Borrowed(bytes))
121 }
122 }
123
124 impl<'a> From<Vec<u8>> for ByteString<'a> {
125 fn from(bytes: Vec<u8>) -> Self {
126 ByteString(Cow::Owned(bytes))
127 }
128 }
129
130 impl<'a> From<&'a str> for ByteString<'a> {
131 fn from(s: &'a str) -> Self {
132 ByteString(Cow::Borrowed(s.as_bytes()))
133 }
134 }
135
136 impl<'a> fmt::Debug for ByteString<'a> {
137 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
138 write!(fmt, "\"{}\"", String::from_utf8_lossy(&self.0))
139 }
140 }
141
142 impl<'a> fmt::Display for ByteString<'a> {
143 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
144 write!(fmt, "{}", String::from_utf8_lossy(&self.0))
145 }
146 }