1 #![rustfmt::skip]
2 /// @generated rust packets from test.
3 use bytes::{Buf, BufMut, Bytes, BytesMut};
4 use std::convert::{TryFrom, TryInto};
5 use std::cell::Cell;
6 use std::fmt;
7 use std::result::Result;
8 use pdl_runtime::{DecodeError, EncodeError, Packet};
9 /// Private prevents users from creating arbitrary scalar values
10 /// in situations where the value needs to be validated.
11 /// Users can freely deref the value, but only the backend
12 /// may create it.
13 #[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
14 pub struct Private<T>(T);
15 impl<T> std::ops::Deref for Private<T> {
16     type Target = T;
deref(&self) -> &Self::Target17     fn deref(&self) -> &Self::Target {
18         &self.0
19     }
20 }
21 impl<T: std::fmt::Debug> std::fmt::Debug for Private<T> {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result22     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23         T::fmt(&self.0, f)
24     }
25 }
26 #[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)]
27 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
28 #[cfg_attr(feature = "serde", serde(try_from = "u32", into = "u32"))]
29 pub struct Bar1(u32);
30 impl From<&Bar1> for u32 {
from(value: &Bar1) -> u3231     fn from(value: &Bar1) -> u32 {
32         value.0
33     }
34 }
35 impl From<Bar1> for u32 {
from(value: Bar1) -> u3236     fn from(value: Bar1) -> u32 {
37         value.0
38     }
39 }
40 impl TryFrom<u32> for Bar1 {
41     type Error = u32;
try_from(value: u32) -> Result<Self, Self::Error>42     fn try_from(value: u32) -> Result<Self, Self::Error> {
43         if value > 0xff_ffff { Err(value) } else { Ok(Bar1(value)) }
44     }
45 }
46 #[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)]
47 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
48 #[cfg_attr(feature = "serde", serde(from = "u32", into = "u32"))]
49 pub struct Bar2(u32);
50 impl From<&Bar2> for u32 {
from(value: &Bar2) -> u3251     fn from(value: &Bar2) -> u32 {
52         value.0
53     }
54 }
55 impl From<Bar2> for u32 {
from(value: Bar2) -> u3256     fn from(value: Bar2) -> u32 {
57         value.0
58     }
59 }
60 impl From<u32> for Bar2 {
from(value: u32) -> Self61     fn from(value: u32) -> Self {
62         Bar2(value)
63     }
64 }
65 #[derive(Debug, Clone, PartialEq, Eq)]
66 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
67 pub struct FooData {
68     a: Bar1,
69     b: Bar2,
70 }
71 #[derive(Debug, Clone, PartialEq, Eq)]
72 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
73 pub struct Foo {
74     #[cfg_attr(feature = "serde", serde(flatten))]
75     foo: FooData,
76 }
77 #[derive(Debug)]
78 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
79 pub struct FooBuilder {
80     pub a: Bar1,
81     pub b: Bar2,
82 }
83 impl FooData {
conforms(bytes: &[u8]) -> bool84     fn conforms(bytes: &[u8]) -> bool {
85         bytes.len() >= 7
86     }
parse(bytes: &[u8]) -> Result<Self, DecodeError>87     fn parse(bytes: &[u8]) -> Result<Self, DecodeError> {
88         let mut cell = Cell::new(bytes);
89         let packet = Self::parse_inner(&mut cell)?;
90         Ok(packet)
91     }
parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError>92     fn parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError> {
93         let a = (bytes.get_mut().get_uint_le(3) as u32).try_into().unwrap();
94         let b = bytes.get_mut().get_u32_le().into();
95         Ok(Self { a, b })
96     }
write_to<T: BufMut>(&self, buffer: &mut T) -> Result<(), EncodeError>97     fn write_to<T: BufMut>(&self, buffer: &mut T) -> Result<(), EncodeError> {
98         buffer.put_uint_le(u32::from(self.a) as u64, 3);
99         buffer.put_u32_le(u32::from(self.b));
100         Ok(())
101     }
get_total_size(&self) -> usize102     fn get_total_size(&self) -> usize {
103         self.get_size()
104     }
get_size(&self) -> usize105     fn get_size(&self) -> usize {
106         7
107     }
108 }
109 impl Packet for Foo {
encoded_len(&self) -> usize110     fn encoded_len(&self) -> usize {
111         self.get_size()
112     }
encode(&self, buf: &mut impl BufMut) -> Result<(), EncodeError>113     fn encode(&self, buf: &mut impl BufMut) -> Result<(), EncodeError> {
114         self.foo.write_to(buf)
115     }
decode(_: &[u8]) -> Result<(Self, &[u8]), DecodeError>116     fn decode(_: &[u8]) -> Result<(Self, &[u8]), DecodeError> {
117         unimplemented!("Rust legacy does not implement full packet trait")
118     }
119 }
120 impl TryFrom<Foo> for Bytes {
121     type Error = EncodeError;
try_from(packet: Foo) -> Result<Self, Self::Error>122     fn try_from(packet: Foo) -> Result<Self, Self::Error> {
123         packet.encode_to_bytes()
124     }
125 }
126 impl TryFrom<Foo> for Vec<u8> {
127     type Error = EncodeError;
try_from(packet: Foo) -> Result<Self, Self::Error>128     fn try_from(packet: Foo) -> Result<Self, Self::Error> {
129         packet.encode_to_vec()
130     }
131 }
132 impl Foo {
parse(bytes: &[u8]) -> Result<Self, DecodeError>133     pub fn parse(bytes: &[u8]) -> Result<Self, DecodeError> {
134         let mut cell = Cell::new(bytes);
135         let packet = Self::parse_inner(&mut cell)?;
136         Ok(packet)
137     }
parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError>138     fn parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError> {
139         let data = FooData::parse_inner(&mut bytes)?;
140         Self::new(data)
141     }
new(foo: FooData) -> Result<Self, DecodeError>142     fn new(foo: FooData) -> Result<Self, DecodeError> {
143         Ok(Self { foo })
144     }
get_a(&self) -> Bar1145     pub fn get_a(&self) -> Bar1 {
146         self.foo.a
147     }
get_b(&self) -> Bar2148     pub fn get_b(&self) -> Bar2 {
149         self.foo.b
150     }
write_to(&self, buffer: &mut impl BufMut) -> Result<(), EncodeError>151     fn write_to(&self, buffer: &mut impl BufMut) -> Result<(), EncodeError> {
152         self.foo.write_to(buffer)
153     }
get_size(&self) -> usize154     pub fn get_size(&self) -> usize {
155         self.foo.get_size()
156     }
157 }
158 impl FooBuilder {
build(self) -> Foo159     pub fn build(self) -> Foo {
160         let foo = FooData { a: self.a, b: self.b };
161         Foo::new(foo).unwrap()
162     }
163 }
164 impl From<FooBuilder> for Foo {
from(builder: FooBuilder) -> Foo165     fn from(builder: FooBuilder) -> Foo {
166         builder.build().into()
167     }
168 }
169