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, PartialEq, Eq)]
27 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
28 pub struct FooData {}
29 #[derive(Debug, Clone, PartialEq, Eq)]
30 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
31 pub struct Foo {
32     #[cfg_attr(feature = "serde", serde(flatten))]
33     foo: FooData,
34 }
35 #[derive(Debug)]
36 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
37 pub struct FooBuilder {}
38 impl FooData {
conforms(bytes: &[u8]) -> bool39     fn conforms(bytes: &[u8]) -> bool {
40         bytes.len() >= 5
41     }
parse(bytes: &[u8]) -> Result<Self, DecodeError>42     fn parse(bytes: &[u8]) -> Result<Self, DecodeError> {
43         let mut cell = Cell::new(bytes);
44         let packet = Self::parse_inner(&mut cell)?;
45         Ok(packet)
46     }
parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError>47     fn parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError> {
48         if bytes.get().remaining() < 5 {
49             return Err(DecodeError::InvalidLengthError {
50                 obj: "Foo",
51                 wanted: 5,
52                 got: bytes.get().remaining(),
53             });
54         }
55         bytes.get_mut().advance(5);
56         Ok(Self {})
57     }
write_to<T: BufMut>(&self, buffer: &mut T) -> Result<(), EncodeError>58     fn write_to<T: BufMut>(&self, buffer: &mut T) -> Result<(), EncodeError> {
59         buffer.put_bytes(0, 5);
60         Ok(())
61     }
get_total_size(&self) -> usize62     fn get_total_size(&self) -> usize {
63         self.get_size()
64     }
get_size(&self) -> usize65     fn get_size(&self) -> usize {
66         5
67     }
68 }
69 impl Packet for Foo {
encoded_len(&self) -> usize70     fn encoded_len(&self) -> usize {
71         self.get_size()
72     }
encode(&self, buf: &mut impl BufMut) -> Result<(), EncodeError>73     fn encode(&self, buf: &mut impl BufMut) -> Result<(), EncodeError> {
74         self.foo.write_to(buf)
75     }
decode(_: &[u8]) -> Result<(Self, &[u8]), DecodeError>76     fn decode(_: &[u8]) -> Result<(Self, &[u8]), DecodeError> {
77         unimplemented!("Rust legacy does not implement full packet trait")
78     }
79 }
80 impl TryFrom<Foo> for Bytes {
81     type Error = EncodeError;
try_from(packet: Foo) -> Result<Self, Self::Error>82     fn try_from(packet: Foo) -> Result<Self, Self::Error> {
83         packet.encode_to_bytes()
84     }
85 }
86 impl TryFrom<Foo> for Vec<u8> {
87     type Error = EncodeError;
try_from(packet: Foo) -> Result<Self, Self::Error>88     fn try_from(packet: Foo) -> Result<Self, Self::Error> {
89         packet.encode_to_vec()
90     }
91 }
92 impl Foo {
parse(bytes: &[u8]) -> Result<Self, DecodeError>93     pub fn parse(bytes: &[u8]) -> Result<Self, DecodeError> {
94         let mut cell = Cell::new(bytes);
95         let packet = Self::parse_inner(&mut cell)?;
96         Ok(packet)
97     }
parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError>98     fn parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError> {
99         let data = FooData::parse_inner(&mut bytes)?;
100         Self::new(data)
101     }
new(foo: FooData) -> Result<Self, DecodeError>102     fn new(foo: FooData) -> Result<Self, DecodeError> {
103         Ok(Self { foo })
104     }
write_to(&self, buffer: &mut impl BufMut) -> Result<(), EncodeError>105     fn write_to(&self, buffer: &mut impl BufMut) -> Result<(), EncodeError> {
106         self.foo.write_to(buffer)
107     }
get_size(&self) -> usize108     pub fn get_size(&self) -> usize {
109         self.foo.get_size()
110     }
111 }
112 impl FooBuilder {
build(self) -> Foo113     pub fn build(self) -> Foo {
114         let foo = FooData {};
115         Foo::new(foo).unwrap()
116     }
117 }
118 impl From<FooBuilder> for Foo {
from(builder: FooBuilder) -> Foo119     fn from(builder: FooBuilder) -> Foo {
120         builder.build().into()
121     }
122 }
123