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 true 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 Ok(Self {}) 49 } write_to<T: BufMut>(&self, buffer: &mut T) -> Result<(), EncodeError>50 fn write_to<T: BufMut>(&self, buffer: &mut T) -> Result<(), EncodeError> { 51 Ok(()) 52 } get_total_size(&self) -> usize53 fn get_total_size(&self) -> usize { 54 self.get_size() 55 } get_size(&self) -> usize56 fn get_size(&self) -> usize { 57 0 58 } 59 } 60 impl Packet for Foo { encoded_len(&self) -> usize61 fn encoded_len(&self) -> usize { 62 self.get_size() 63 } encode(&self, buf: &mut impl BufMut) -> Result<(), EncodeError>64 fn encode(&self, buf: &mut impl BufMut) -> Result<(), EncodeError> { 65 self.foo.write_to(buf) 66 } decode(_: &[u8]) -> Result<(Self, &[u8]), DecodeError>67 fn decode(_: &[u8]) -> Result<(Self, &[u8]), DecodeError> { 68 unimplemented!("Rust legacy does not implement full packet trait") 69 } 70 } 71 impl TryFrom<Foo> for Bytes { 72 type Error = EncodeError; try_from(packet: Foo) -> Result<Self, Self::Error>73 fn try_from(packet: Foo) -> Result<Self, Self::Error> { 74 packet.encode_to_bytes() 75 } 76 } 77 impl TryFrom<Foo> for Vec<u8> { 78 type Error = EncodeError; try_from(packet: Foo) -> Result<Self, Self::Error>79 fn try_from(packet: Foo) -> Result<Self, Self::Error> { 80 packet.encode_to_vec() 81 } 82 } 83 impl Foo { parse(bytes: &[u8]) -> Result<Self, DecodeError>84 pub fn parse(bytes: &[u8]) -> Result<Self, DecodeError> { 85 let mut cell = Cell::new(bytes); 86 let packet = Self::parse_inner(&mut cell)?; 87 Ok(packet) 88 } parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError>89 fn parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError> { 90 let data = FooData::parse_inner(&mut bytes)?; 91 Self::new(data) 92 } new(foo: FooData) -> Result<Self, DecodeError>93 fn new(foo: FooData) -> Result<Self, DecodeError> { 94 Ok(Self { foo }) 95 } write_to(&self, buffer: &mut impl BufMut) -> Result<(), EncodeError>96 fn write_to(&self, buffer: &mut impl BufMut) -> Result<(), EncodeError> { 97 self.foo.write_to(buffer) 98 } get_size(&self) -> usize99 pub fn get_size(&self) -> usize { 100 self.foo.get_size() 101 } 102 } 103 impl FooBuilder { build(self) -> Foo104 pub fn build(self) -> Foo { 105 let foo = FooData {}; 106 Foo::new(foo).unwrap() 107 } 108 } 109 impl From<FooBuilder> for Foo { from(builder: FooBuilder) -> Foo110 fn from(builder: FooBuilder) -> Foo { 111 builder.build().into() 112 } 113 } 114