diff options
| author | mat <27899617+mat-1@users.noreply.github.com> | 2022-09-02 12:11:14 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-09-02 12:11:14 -0500 |
| commit | cfb190d00c70f1b09789e23f89a3c67840e0fd87 (patch) | |
| tree | 9bdc021943753d60bf437526c4c294275eae13ac /azalea-buf/src/write.rs | |
| parent | 32458d743f757da3193717fe5554f490703640c0 (diff) | |
| download | azalea-drasl-cfb190d00c70f1b09789e23f89a3c67840e0fd87.tar.xz | |
get rid of Readable & Writable (#21)
Diffstat (limited to 'azalea-buf/src/write.rs')
| -rw-r--r-- | azalea-buf/src/write.rs | 180 |
1 files changed, 56 insertions, 124 deletions
diff --git a/azalea-buf/src/write.rs b/azalea-buf/src/write.rs index 15fa9680..0bbed25b 100644 --- a/azalea-buf/src/write.rs +++ b/azalea-buf/src/write.rs @@ -2,115 +2,22 @@ use super::{UnsizedByteArray, MAX_STRING_LENGTH}; use byteorder::{BigEndian, WriteBytesExt}; use std::{collections::HashMap, io::Write}; -// TODO: get rid of Writable and use McBufWritable everywhere - -pub trait Writable: Write { - fn write_list<F, T>(&mut self, list: &[T], writer: F) -> Result<(), std::io::Error> - where - F: FnOnce(&mut Self, &T) -> Result<(), std::io::Error> + Copy, - { - self.write_varint(list.len() as i32)?; - for item in list { - writer(self, item)?; - } - Ok(()) - } - - fn write_int_id_list(&mut self, list: &[i32]) -> Result<(), std::io::Error> { - self.write_list(list, |buf, n| buf.write_varint(*n)) - } - - fn write_map<KF, VF, KT, VT>( - &mut self, - map: Vec<(KT, VT)>, - key_writer: KF, - value_writer: VF, - ) -> Result<(), std::io::Error> - where - KF: Fn(&mut Self, KT) -> Result<(), std::io::Error> + Copy, - VF: Fn(&mut Self, VT) -> Result<(), std::io::Error> + Copy, - { - self.write_varint(map.len() as i32)?; - for (key, value) in map { - key_writer(self, key)?; - value_writer(self, value)?; - } - Ok(()) - } - - fn write_byte(&mut self, n: u8) -> Result<(), std::io::Error> { - WriteBytesExt::write_u8(self, n) - } - - fn write_bytes(&mut self, bytes: &[u8]) -> Result<(), std::io::Error> { - self.write_all(bytes)?; - Ok(()) - } - - fn write_varint(&mut self, mut value: i32) -> Result<(), std::io::Error> { - let mut buffer = [0]; - if value == 0 { - self.write_all(&buffer).unwrap(); - } - while value != 0 { - buffer[0] = (value & 0b0111_1111) as u8; - value = (value >> 7) & (i32::max_value() >> 6); - if value != 0 { - buffer[0] |= 0b1000_0000; - } - self.write_all(&buffer)?; - } - Ok(()) - } - - fn write_utf_with_len(&mut self, string: &str, len: usize) -> Result<(), std::io::Error> { - if string.len() > len { - panic!( - "String too big (was {} bytes encoded, max {})", - string.len(), - len - ); - } - self.write_varint(string.len() as i32)?; - self.write_bytes(string.as_bytes()) - } - - fn write_utf(&mut self, string: &str) -> Result<(), std::io::Error> { - self.write_utf_with_len(string, MAX_STRING_LENGTH.into()) - } - - fn write_short(&mut self, n: i16) -> Result<(), std::io::Error> { - WriteBytesExt::write_i16::<BigEndian>(self, n) - } - - fn write_byte_array(&mut self, bytes: &[u8]) -> Result<(), std::io::Error> { - self.write_varint(bytes.len() as i32)?; - self.write_bytes(bytes) - } - - fn write_int(&mut self, n: i32) -> Result<(), std::io::Error> { - WriteBytesExt::write_i32::<BigEndian>(self, n) - } - - fn write_boolean(&mut self, b: bool) -> Result<(), std::io::Error> { - self.write_byte(if b { 1 } else { 0 }) - } - - fn write_long(&mut self, n: i64) -> Result<(), std::io::Error> { - WriteBytesExt::write_i64::<BigEndian>(self, n) - } - - fn write_float(&mut self, n: f32) -> Result<(), std::io::Error> { - WriteBytesExt::write_f32::<BigEndian>(self, n) - } - - fn write_double(&mut self, n: f64) -> Result<(), std::io::Error> { - WriteBytesExt::write_f64::<BigEndian>(self, n) - } +fn write_utf_with_len( + buf: &mut impl Write, + string: &str, + len: usize, +) -> Result<(), std::io::Error> { + if string.len() > len { + panic!( + "String too big (was {} bytes encoded, max {})", + string.len(), + len + ); + } + string.as_bytes().to_vec().write_into(buf)?; + Ok(()) } -impl<W: Write + ?Sized> Writable for W {} - pub trait McBufWritable { fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error>; } @@ -121,25 +28,48 @@ pub trait McBufVarWritable { impl McBufWritable for i32 { fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - Writable::write_int(buf, *self) + WriteBytesExt::write_i32::<BigEndian>(buf, *self) } } impl McBufVarWritable for i32 { fn var_write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - buf.write_varint(*self) + let mut buffer = [0]; + let mut value = *self; + if value == 0 { + buf.write_all(&buffer).unwrap(); + } + while value != 0 { + buffer[0] = (value & 0b0111_1111) as u8; + value = (value >> 7) & (i32::max_value() >> 6); + if value != 0 { + buffer[0] |= 0b1000_0000; + } + buf.write_all(&buffer)?; + } + Ok(()) } } impl McBufWritable for UnsizedByteArray { fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - buf.write_bytes(self) + buf.write_all(self) } } impl<T: McBufWritable> McBufWritable for Vec<T> { default fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - buf.write_list(self, |buf, i| T::write_into(i, buf)) + self[..].write_into(buf) + } +} + +impl<T: McBufWritable> McBufWritable for [T] { + default fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { + (self.len() as u32).var_write_into(buf)?; + for item in self { + T::write_into(item, buf)?; + } + Ok(()) } } @@ -169,13 +99,14 @@ impl<K: McBufWritable, V: McBufVarWritable> McBufVarWritable for HashMap<K, V> { impl McBufWritable for Vec<u8> { fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - buf.write_byte_array(self) + (self.len() as u32).var_write_into(buf)?; + buf.write_all(self) } } impl McBufWritable for String { fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - buf.write_utf(self) + write_utf_with_len(buf, self, MAX_STRING_LENGTH.into()) } } @@ -239,19 +170,19 @@ impl<T: McBufVarWritable> McBufVarWritable for Vec<T> { impl McBufWritable for u8 { fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - buf.write_byte(*self) + WriteBytesExt::write_u8(buf, *self) } } impl McBufWritable for i16 { fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - Writable::write_short(buf, *self) + WriteBytesExt::write_i16::<BigEndian>(buf, *self) } } impl McBufWritable for i64 { fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - Writable::write_long(buf, *self) + WriteBytesExt::write_i64::<BigEndian>(buf, *self) } } @@ -263,35 +194,36 @@ impl McBufWritable for u64 { impl McBufWritable for bool { fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - buf.write_boolean(*self) + let byte: u8 = if *self { 1 } else { 0 }; + byte.write_into(buf) } } impl McBufWritable for i8 { fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - buf.write_byte(*self as u8) + (*self as u8).write_into(buf) } } impl McBufWritable for f32 { fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - buf.write_float(*self) + WriteBytesExt::write_f32::<BigEndian>(buf, *self) } } impl McBufWritable for f64 { fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - buf.write_double(*self) + WriteBytesExt::write_f64::<BigEndian>(buf, *self) } } impl<T: McBufWritable> McBufWritable for Option<T> { default fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { if let Some(s) = self { - buf.write_boolean(true)?; + true.write_into(buf)?; s.write_into(buf)?; } else { - buf.write_boolean(false)?; + false.write_into(buf)?; }; Ok(()) } @@ -300,10 +232,10 @@ impl<T: McBufWritable> McBufWritable for Option<T> { impl<T: McBufVarWritable> McBufVarWritable for Option<T> { default fn var_write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { if let Some(s) = self { - buf.write_boolean(true)?; + true.write_into(buf)?; s.var_write_into(buf)?; } else { - buf.write_boolean(false)?; + false.write_into(buf)?; }; Ok(()) } |
