object_rainbow/
with_repr.rs1use crate::*;
2
3#[derive(Debug, Tagged, ListHashes, Topological, Clone)]
4pub struct WithRepr<T> {
5 object: T,
6 data: Vec<u8>,
7}
8
9impl<T: ToOutput> ToOutput for WithRepr<T> {
10 fn to_output(&self, output: &mut impl Output) {
11 if output.is_mangling() {
12 self.object.to_output(output);
13 }
14 if output.is_real() {
15 self.data.to_output(output);
16 }
17 }
18}
19
20impl<T> WithRepr<T> {
21 pub fn new(object: T) -> Self
22 where
23 T: ToOutput,
24 {
25 let data = object.vec();
26 Self { object, data }
27 }
28
29 pub fn parse_zero_terminated<I: ParseInput>(input: &mut I) -> crate::Result<Self>
30 where
31 T: Parse<I>,
32 {
33 let (data, object) = input.parse_zero_terminated()?;
34 Ok(Self { object, data })
35 }
36
37 pub fn data(&self) -> &[u8] {
38 &self.data
39 }
40
41 pub fn object(&self) -> &T {
42 &self.object
43 }
44}
45
46impl<T> PartialEq for WithRepr<T> {
47 fn eq(&self, other: &Self) -> bool {
48 self.data == other.data
49 }
50}
51
52impl<T> Eq for WithRepr<T> {}
53
54impl<T> PartialOrd for WithRepr<T> {
55 fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
56 Some(self.cmp(other))
57 }
58}
59
60impl<T> Ord for WithRepr<T> {
61 fn cmp(&self, other: &Self) -> std::cmp::Ordering {
62 self.data.cmp(&other.data)
63 }
64}
65
66impl<T: ToOutput> ByteOrd for WithRepr<T> {
67 fn bytes_cmp(&self, other: &Self) -> Ordering {
68 self.cmp(other)
69 }
70}
71
72impl<T: Parse<I> + ToOutput, I: ParseInput> Parse<I> for WithRepr<T> {
73 fn parse(input: I) -> crate::Result<Self> {
74 Ok(Self::new(input.parse()?))
75 }
76}
77
78impl<T: ParseInline<I> + ToOutput, I: ParseInput> ParseInline<I> for WithRepr<T> {
79 fn parse_inline(input: &mut I) -> crate::Result<Self> {
80 Ok(Self::new(input.parse_inline()?))
81 }
82}
83
84impl<T: Size> Size for WithRepr<T> {
85 type Size = T::Size;
86 const SIZE: usize = T::SIZE;
87}
88
89impl<T: MaybeHasNiche> MaybeHasNiche for WithRepr<T> {
90 type MnArray = WithRepr<T>;
91}