From 4fd8d47d7e532a153961262fc4274f7ba8bddf6a Mon Sep 17 00:00:00 2001 From: Snowiiii Date: Wed, 7 Aug 2024 14:18:53 +0200 Subject: [PATCH] Protocol: Add support for lists in Serializer --- pumpkin-protocol/src/bytebuf/mod.rs | 11 +++++++++++ pumpkin-protocol/src/bytebuf/serializer.rs | 17 ++++++++++++----- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/pumpkin-protocol/src/bytebuf/mod.rs b/pumpkin-protocol/src/bytebuf/mod.rs index 86d49df2e..d7046496b 100644 --- a/pumpkin-protocol/src/bytebuf/mod.rs +++ b/pumpkin-protocol/src/bytebuf/mod.rs @@ -297,6 +297,17 @@ impl ByteBuffer { self.buffer.put_slice(src) } + pub fn put(&mut self, mut src: T) + where + Self: Sized, + { + self.buffer.put(src) + } + + pub fn reserve(&mut self, additional: usize) { + self.buffer.reserve(additional) + } + pub fn get_slice(&mut self) -> BytesMut { self.buffer.split() } diff --git a/pumpkin-protocol/src/bytebuf/serializer.rs b/pumpkin-protocol/src/bytebuf/serializer.rs index 025bad91b..8685fb7ba 100644 --- a/pumpkin-protocol/src/bytebuf/serializer.rs +++ b/pumpkin-protocol/src/bytebuf/serializer.rs @@ -1,8 +1,11 @@ use std::fmt::Display; +use bytes::Buf; use serde::{ser, Serialize}; use thiserror::Error; +use crate::VarInt; + use super::ByteBuffer; pub struct Serializer { @@ -123,8 +126,12 @@ impl<'a> ser::Serializer for &'a mut Serializer { fn serialize_none(self) -> Result { unimplemented!() } - fn serialize_seq(self, _len: Option) -> Result { - unimplemented!() + fn serialize_seq(self, len: Option) -> Result { + if let Some(len) = len { + self.output.reserve(len); + self.output.put_var_int(&VarInt(len as i32)); + } + Ok(self) } fn serialize_some(self, _value: &T) -> Result where @@ -213,16 +220,16 @@ impl<'a> ser::SerializeSeq for &'a mut Serializer { type Error = SerializerError; // Serialize a single element of the sequence. - fn serialize_element(&mut self, _value: &T) -> Result<(), Self::Error> + fn serialize_element(&mut self, value: &T) -> Result<(), Self::Error> where T: ?Sized + Serialize, { - todo!() + value.serialize(&mut **self) } // Close the sequence. fn end(self) -> Result<(), Self::Error> { - todo!() + Ok(()) } }