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, 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, 41 } 42 impl FooData { conforms(bytes: &[u8]) -> bool43 fn conforms(bytes: &[u8]) -> bool { 44 bytes.len() >= 8 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() < 8 { 53 return Err(DecodeError::InvalidLengthError { 54 obj: "Foo", 55 wanted: 8, 56 got: bytes.get().remaining(), 57 }); 58 } 59 let x = bytes.get_mut().get_u64(); 60 Ok(Self { x }) 61 } write_to<T: BufMut>(&self, buffer: &mut T) -> Result<(), EncodeError>62 fn write_to<T: BufMut>(&self, buffer: &mut T) -> Result<(), EncodeError> { 63 buffer.put_u64(self.x); 64 Ok(()) 65 } get_total_size(&self) -> usize66 fn get_total_size(&self) -> usize { 67 self.get_size() 68 } get_size(&self) -> usize69 fn get_size(&self) -> usize { 70 8 71 } 72 } 73 impl Packet for Foo { encoded_len(&self) -> usize74 fn encoded_len(&self) -> usize { 75 self.get_size() 76 } encode(&self, buf: &mut impl BufMut) -> Result<(), EncodeError>77 fn encode(&self, buf: &mut impl BufMut) -> Result<(), EncodeError> { 78 self.foo.write_to(buf) 79 } decode(_: &[u8]) -> Result<(Self, &[u8]), DecodeError>80 fn decode(_: &[u8]) -> Result<(Self, &[u8]), DecodeError> { 81 unimplemented!("Rust legacy does not implement full packet trait") 82 } 83 } 84 impl TryFrom<Foo> for Bytes { 85 type Error = EncodeError; try_from(packet: Foo) -> Result<Self, Self::Error>86 fn try_from(packet: Foo) -> Result<Self, Self::Error> { 87 packet.encode_to_bytes() 88 } 89 } 90 impl TryFrom<Foo> for Vec<u8> { 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_vec() 94 } 95 } 96 impl Foo { parse(bytes: &[u8]) -> Result<Self, DecodeError>97 pub fn parse(bytes: &[u8]) -> Result<Self, DecodeError> { 98 let mut cell = Cell::new(bytes); 99 let packet = Self::parse_inner(&mut cell)?; 100 Ok(packet) 101 } parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError>102 fn parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError> { 103 let data = FooData::parse_inner(&mut bytes)?; 104 Self::new(data) 105 } new(foo: FooData) -> Result<Self, DecodeError>106 fn new(foo: FooData) -> Result<Self, DecodeError> { 107 Ok(Self { foo }) 108 } get_x(&self) -> u64109 pub fn get_x(&self) -> u64 { 110 self.foo.x 111 } write_to(&self, buffer: &mut impl BufMut) -> Result<(), EncodeError>112 fn write_to(&self, buffer: &mut impl BufMut) -> Result<(), EncodeError> { 113 self.foo.write_to(buffer) 114 } get_size(&self) -> usize115 pub fn get_size(&self) -> usize { 116 self.foo.get_size() 117 } 118 } 119 impl FooBuilder { build(self) -> Foo120 pub fn build(self) -> Foo { 121 let foo = FooData { x: self.x }; 122 Foo::new(foo).unwrap() 123 } 124 } 125 impl From<FooBuilder> for Foo { from(builder: FooBuilder) -> Foo126 fn from(builder: FooBuilder) -> Foo { 127 builder.build().into() 128 } 129 } 130