diff options
| author | mat <github@matdoes.dev> | 2022-04-30 01:43:00 -0500 |
|---|---|---|
| committer | mat <github@matdoes.dev> | 2022-04-30 01:43:00 -0500 |
| commit | 4d7bf6c50eda970ac4998f10f5e46006dffb6ca2 (patch) | |
| tree | 8b677c96440dfae63132303df3fa9bf37d3bcf65 /azalea-protocol/src/mc_buf | |
| parent | c8c356685d9050d166226fb01353faba5fc00f4a (diff) | |
| download | azalea-drasl-4d7bf6c50eda970ac4998f10f5e46006dffb6ca2.tar.xz | |
significantly optimize reading Vec<u8>
unfortunately, this introduces the requirement of using rust nightly
Diffstat (limited to 'azalea-protocol/src/mc_buf')
| -rwxr-xr-x | azalea-protocol/src/mc_buf/read.rs | 12 | ||||
| -rwxr-xr-x | azalea-protocol/src/mc_buf/write.rs | 8 |
2 files changed, 18 insertions, 2 deletions
diff --git a/azalea-protocol/src/mc_buf/read.rs b/azalea-protocol/src/mc_buf/read.rs index 3d1aa0b3..a0e3e79f 100755 --- a/azalea-protocol/src/mc_buf/read.rs +++ b/azalea-protocol/src/mc_buf/read.rs @@ -272,7 +272,7 @@ impl McBufReadable for UnsizedByteArray { #[async_trait] impl<T: McBufReadable + Send> McBufReadable for Vec<T> { - async fn read_into<R>(buf: &mut R) -> Result<Self, String> + default async fn read_into<R>(buf: &mut R) -> Result<Self, String> where R: AsyncRead + std::marker::Unpin + std::marker::Send, { @@ -285,6 +285,16 @@ impl<T: McBufReadable + Send> McBufReadable for Vec<T> { } } +#[async_trait] +impl McBufReadable for Vec<u8> { + async fn read_into<R>(buf: &mut R) -> Result<Self, String> + where + R: AsyncRead + std::marker::Unpin + std::marker::Send, + { + buf.read_byte_array().await + } +} + // string #[async_trait] impl McBufReadable for String { diff --git a/azalea-protocol/src/mc_buf/write.rs b/azalea-protocol/src/mc_buf/write.rs index bd5e3f52..4c7ac60c 100755 --- a/azalea-protocol/src/mc_buf/write.rs +++ b/azalea-protocol/src/mc_buf/write.rs @@ -212,11 +212,17 @@ impl McBufWritable for UnsizedByteArray { // TODO: use specialization when that gets stabilized into rust // to optimize for Vec<u8> byte arrays impl<T: McBufWritable> McBufWritable for Vec<T> { - fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> { + default fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> { buf.write_list(self, |buf, i| T::write_into(i, buf)) } } +impl McBufWritable for Vec<u8> { + fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> { + buf.write_byte_array(self) + } +} + // string impl McBufWritable for String { fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> { |
