• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     x: [u64; 7],
30 }
31 #[derive(Debug, Clone, PartialEq, Eq)]
32 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
33 pub struct Foo {
34     #[cfg_attr(feature = "serde", serde(flatten))]
35     foo: FooData,
36 }
37 #[derive(Debug)]
38 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
39 pub struct FooBuilder {
40     pub x: [u64; 7],
41 }
42 impl FooData {
conforms(bytes: &[u8]) -> bool43     fn conforms(bytes: &[u8]) -> bool {
44         bytes.len() >= 56
45     }
parse(bytes: &[u8]) -> Result<Self, DecodeError>46     fn parse(bytes: &[u8]) -> Result<Self, DecodeError> {
47         let mut cell = Cell::new(bytes);
48         let packet = Self::parse_inner(&mut cell)?;
49         Ok(packet)
50     }
parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError>51     fn parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError> {
52         if bytes.get().remaining() < 7 * 8 {
53             return Err(DecodeError::InvalidLengthError {
54                 obj: "Foo",
55                 wanted: 7 * 8,
56                 got: bytes.get().remaining(),
57             });
58         }
59         let x = (0..7)
60             .map(|_| Ok::<_, DecodeError>(bytes.get_mut().get_u64()))
61             .collect::<Result<Vec<_>, DecodeError>>()?
62             .try_into()
63             .map_err(|_| DecodeError::InvalidPacketError)?;
64         Ok(Self { x })
65     }
write_to<T: BufMut>(&self, buffer: &mut T) -> Result<(), EncodeError>66     fn write_to<T: BufMut>(&self, buffer: &mut T) -> Result<(), EncodeError> {
67         for elem in &self.x {
68             buffer.put_u64(*elem);
69         }
70         Ok(())
71     }
get_total_size(&self) -> usize72     fn get_total_size(&self) -> usize {
73         self.get_size()
74     }
get_size(&self) -> usize75     fn get_size(&self) -> usize {
76         56
77     }
78 }
79 impl Packet for Foo {
encoded_len(&self) -> usize80     fn encoded_len(&self) -> usize {
81         self.get_size()
82     }
encode(&self, buf: &mut impl BufMut) -> Result<(), EncodeError>83     fn encode(&self, buf: &mut impl BufMut) -> Result<(), EncodeError> {
84         self.foo.write_to(buf)
85     }
decode(_: &[u8]) -> Result<(Self, &[u8]), DecodeError>86     fn decode(_: &[u8]) -> Result<(Self, &[u8]), DecodeError> {
87         unimplemented!("Rust legacy does not implement full packet trait")
88     }
89 }
90 impl TryFrom<Foo> for Bytes {
91     type Error = EncodeError;
try_from(packet: Foo) -> Result<Self, Self::Error>92     fn try_from(packet: Foo) -> Result<Self, Self::Error> {
93         packet.encode_to_bytes()
94     }
95 }
96 impl TryFrom<Foo> for Vec<u8> {
97     type Error = EncodeError;
try_from(packet: Foo) -> Result<Self, Self::Error>98     fn try_from(packet: Foo) -> Result<Self, Self::Error> {
99         packet.encode_to_vec()
100     }
101 }
102 impl Foo {
parse(bytes: &[u8]) -> Result<Self, DecodeError>103     pub fn parse(bytes: &[u8]) -> Result<Self, DecodeError> {
104         let mut cell = Cell::new(bytes);
105         let packet = Self::parse_inner(&mut cell)?;
106         Ok(packet)
107     }
parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError>108     fn parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError> {
109         let data = FooData::parse_inner(&mut bytes)?;
110         Self::new(data)
111     }
new(foo: FooData) -> Result<Self, DecodeError>112     fn new(foo: FooData) -> Result<Self, DecodeError> {
113         Ok(Self { foo })
114     }
get_x(&self) -> &[u64; 7]115     pub fn get_x(&self) -> &[u64; 7] {
116         &self.foo.x
117     }
write_to(&self, buffer: &mut impl BufMut) -> Result<(), EncodeError>118     fn write_to(&self, buffer: &mut impl BufMut) -> Result<(), EncodeError> {
119         self.foo.write_to(buffer)
120     }
get_size(&self) -> usize121     pub fn get_size(&self) -> usize {
122         self.foo.get_size()
123     }
124 }
125 impl FooBuilder {
build(self) -> Foo126     pub fn build(self) -> Foo {
127         let foo = FooData { x: self.x };
128         Foo::new(foo).unwrap()
129     }
130 }
131 impl From<FooBuilder> for Foo {
from(builder: FooBuilder) -> Foo132     fn from(builder: FooBuilder) -> Foo {
133         builder.build().into()
134     }
135 }
136