Skip to main content

object_rainbow/
length_prefixed.rs

1use std::ops::{Deref, DerefMut};
2
3use crate::{u63::U63, *};
4
5/// Length-prefixed value. Used to make [`Inline`]s out of arbitrary [`Object`]s.
6///
7/// If you can guarantee absence of zeroes, see [`zero_terminated::Zt`].
8#[derive(ListHashes, Topological, Tagged, ParseAsInline, Default, Clone, PartialEq, Eq)]
9#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
10pub struct Lp<T>(pub T);
11
12impl<T> Deref for Lp<T> {
13    type Target = T;
14
15    fn deref(&self) -> &Self::Target {
16        &self.0
17    }
18}
19
20impl<T> DerefMut for Lp<T> {
21    fn deref_mut(&mut self) -> &mut Self::Target {
22        &mut self.0
23    }
24}
25
26impl<T: ToOutput> ToOutput for Lp<T> {
27    fn to_output(&self, output: &mut impl Output) {
28        if output.is_mangling() {
29            self.0.to_output(output);
30        }
31        if output.is_real() {
32            let data = self.0.vec();
33            let prefix = U63::len_of(&data);
34            prefix.to_output(output);
35            data.to_output(output);
36        }
37    }
38}
39
40impl<T: ToOutput> InlineOutput for Lp<T> {}
41
42impl<T: Parse<I>, I: ParseInput> ParseInline<I> for Lp<T> {
43    fn parse_inline(input: &mut I) -> crate::Result<Self> {
44        let prefix: U63 = input.parse_inline()?;
45        Ok(Self(input.split_parse(prefix.as_usize()?)?))
46    }
47}
48
49#[test]
50fn prefixed() {
51    let a = Lp(vec![0, 1, 2]);
52    let data = a.vec();
53    let b = Lp::<Vec<u8>>::parse_slice_refless(&data).unwrap();
54    assert_eq!(*a, *b);
55}
56
57/// Length-prefixed [`Vec<u8>`]
58#[derive(Debug, Clone, ParseAsInline, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
59#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
60pub struct LpBytes(pub Vec<u8>);
61
62impl Deref for LpBytes {
63    type Target = Vec<u8>;
64
65    fn deref(&self) -> &Self::Target {
66        &self.0
67    }
68}
69
70impl DerefMut for LpBytes {
71    fn deref_mut(&mut self) -> &mut Self::Target {
72        &mut self.0
73    }
74}
75
76impl ToOutput for LpBytes {
77    fn to_output(&self, output: &mut impl Output) {
78        if output.is_real() {
79            let data = &self.0;
80            let prefix = U63::len_of(data);
81            prefix.to_output(output);
82            data.to_output(output);
83        }
84    }
85}
86
87impl InlineOutput for LpBytes {}
88
89impl<I: ParseInput> ParseInline<I> for LpBytes {
90    fn parse_inline(input: &mut I) -> crate::Result<Self> {
91        let prefix: U63 = input.parse_inline()?;
92        let mut data = vec![0; prefix.as_usize()?];
93        input.read(&mut data)?;
94        Ok(Self(data))
95    }
96}
97
98impl Tagged for LpBytes {}
99impl ListHashes for LpBytes {}
100impl Topological for LpBytes {}
101
102/// Length-prefixed [`String`].
103#[derive(Debug, Clone, ParseAsInline, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
104#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
105pub struct LpString(pub String);
106
107impl Deref for LpString {
108    type Target = String;
109
110    fn deref(&self) -> &Self::Target {
111        &self.0
112    }
113}
114
115impl DerefMut for LpString {
116    fn deref_mut(&mut self) -> &mut Self::Target {
117        &mut self.0
118    }
119}
120
121impl ToOutput for LpString {
122    fn to_output(&self, output: &mut impl Output) {
123        if output.is_real() {
124            let data = &self.0;
125            let prefix = U63::len_of(data.as_bytes());
126            prefix.to_output(output);
127            data.to_output(output);
128        }
129    }
130}
131
132impl InlineOutput for LpString {}
133
134impl<I: ParseInput> ParseInline<I> for LpString {
135    fn parse_inline(input: &mut I) -> crate::Result<Self> {
136        String::from_utf8(input.parse_inline::<LpBytes>()?.0)
137            .map_err(Error::Utf8)
138            .map(Self)
139    }
140}
141
142impl Tagged for LpString {}
143impl ListHashes for LpString {}
144impl Topological for LpString {}