object_rainbow/
length_prefixed.rs1use std::{
2 fmt::Display,
3 ops::{Deref, DerefMut},
4};
5
6use crate::{u63::U63, *};
7
8#[pod(no_output, no_parse)]
12#[derive(ParseAsInline)]
13#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
14pub struct Lp<T>(pub T);
15
16impl<T> Deref for Lp<T> {
17 type Target = T;
18
19 fn deref(&self) -> &Self::Target {
20 &self.0
21 }
22}
23
24impl<T> DerefMut for Lp<T> {
25 fn deref_mut(&mut self) -> &mut Self::Target {
26 &mut self.0
27 }
28}
29
30impl<T: ToOutput> ToOutput for Lp<T> {
31 fn to_output(&self, output: &mut (impl ?Sized + Output)) {
32 if output.is_mangling() {
33 self.0.to_output(output);
34 }
35 if output.is_real() {
36 let data = self.0.vec();
37 let prefix = U63::len_of(&data);
38 prefix.to_output(output);
39 data.to_output(output);
40 }
41 }
42}
43
44impl<T: ToOutput> InlineOutput for Lp<T> {}
45
46impl<T: Parse<I>, I: ParseInput> ParseInline<I> for Lp<T> {
47 fn parse_inline(input: &mut I) -> crate::Result<Self> {
48 let prefix: U63 = input.parse_inline()?;
49 Ok(Self(input.split_parse(prefix.as_usize()?)?))
50 }
51}
52
53impl<T: SignificantLength> ByteOrd for Lp<T> {
54 fn bytes_cmp(&self, other: &Self) -> Ordering {
55 self.0.bytes_cmp(&other.0)
56 }
57}
58
59#[test]
60fn prefixed() -> crate::Result<()> {
61 let a = Lp(vec![0, 1, 2]);
62 let data = a.vec();
63 let b = Lp::<Vec<u8>>::parse_slice_refless(&data)?;
64 assert_eq!(*a, *b);
65 Ok(())
66}
67
68#[pod(no_copy, no_output, no_parse)]
70#[derive(ParseAsInline)]
71#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
72pub struct LpBytes(pub Vec<u8>);
73
74impl Deref for LpBytes {
75 type Target = Vec<u8>;
76
77 fn deref(&self) -> &Self::Target {
78 &self.0
79 }
80}
81
82impl DerefMut for LpBytes {
83 fn deref_mut(&mut self) -> &mut Self::Target {
84 &mut self.0
85 }
86}
87
88impl ToOutput for LpBytes {
89 fn to_output(&self, output: &mut (impl ?Sized + Output)) {
90 if output.is_real() {
91 let data = &self.0;
92 let prefix = U63::len_of(data);
93 prefix.to_output(output);
94 data.to_output(output);
95 }
96 }
97}
98
99impl InlineOutput for LpBytes {}
100
101impl<I: ParseInput> ParseInline<I> for LpBytes {
102 fn parse_inline(input: &mut I) -> crate::Result<Self> {
103 let prefix: U63 = input.parse_inline()?;
104 let mut data = vec![0; prefix.as_usize()?];
105 input.read(&mut data)?;
106 Ok(Self(data))
107 }
108}
109
110#[pod(no_copy, no_output, no_parse)]
112#[derive(ParseAsInline)]
113#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
114pub struct LpString(pub String);
115
116impl Display for LpString {
117 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
118 self.0.fmt(f)
119 }
120}
121
122impl From<&'_ str> for LpString {
123 fn from(s: &'_ str) -> Self {
124 Self(s.into())
125 }
126}
127
128impl AsRef<str> for LpString {
129 fn as_ref(&self) -> &str {
130 self
131 }
132}
133
134impl Deref for LpString {
135 type Target = String;
136
137 fn deref(&self) -> &Self::Target {
138 &self.0
139 }
140}
141
142impl DerefMut for LpString {
143 fn deref_mut(&mut self) -> &mut Self::Target {
144 &mut self.0
145 }
146}
147
148impl ToOutput for LpString {
149 fn to_output(&self, output: &mut (impl ?Sized + Output)) {
150 if output.is_real() {
151 let data = &self.0;
152 let prefix = U63::len_of(data.as_bytes());
153 prefix.to_output(output);
154 data.to_output(output);
155 }
156 }
157}
158
159impl InlineOutput for LpString {}
160
161impl<I: ParseInput> ParseInline<I> for LpString {
162 fn parse_inline(input: &mut I) -> crate::Result<Self> {
163 String::from_utf8(input.parse_inline::<LpBytes>()?.0)
164 .map_err(Error::Utf8)
165 .map(Self)
166 }
167}
168
169#[derive(Debug, ListHashes, Topological, Tagged, ParseAsInline, Clone, PartialEq, Eq, Hash)]
170pub struct LpVec<T>(pub Vec<T>);
171
172impl<T: PartialOrd> PartialOrd for LpVec<T> {
173 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
174 (self.len(), &self.0).partial_cmp(&(other.len(), &other.0))
175 }
176}
177
178impl<T: Ord> Ord for LpVec<T> {
179 fn cmp(&self, other: &Self) -> Ordering {
180 (self.len(), &self.0).cmp(&(other.len(), &other.0))
181 }
182}
183
184impl<T: ByteOrd + InlineOutput> ByteOrd for LpVec<T> {
185 fn bytes_cmp(&self, other: &Self) -> Ordering {
186 (U63::len_of(&self.0), &self.0).bytes_cmp(&(U63::len_of(&other.0), &other.0))
187 }
188}
189
190impl<T> Deref for LpVec<T> {
191 type Target = Vec<T>;
192
193 fn deref(&self) -> &Self::Target {
194 &self.0
195 }
196}
197
198impl<T> DerefMut for LpVec<T> {
199 fn deref_mut(&mut self) -> &mut Self::Target {
200 &mut self.0
201 }
202}
203
204impl<T: InlineOutput> ToOutput for LpVec<T> {
205 fn to_output(&self, output: &mut (impl ?Sized + Output)) {
206 if output.is_mangling() {
207 self.0.to_output(output);
208 }
209 if output.is_real() {
210 let prefix = U63::len_of(&self.0);
211 prefix.to_output(output);
212 self.0.to_output(output);
213 }
214 }
215}
216
217impl<T: InlineOutput> InlineOutput for LpVec<T> {}
218
219impl<T: ParseInline<I>, I: ParseInput> ParseInline<I> for LpVec<T> {
220 fn parse_inline(input: &mut I) -> crate::Result<Self> {
221 let prefix: U63 = input.parse_inline()?;
222 Ok(Self(input.parse_vec_n(prefix.as_usize()?)?))
223 }
224}