diff options
| author | mat <27899617+mat-1@users.noreply.github.com> | 2026-01-13 10:51:45 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-01-13 10:51:45 -0600 |
| commit | b21ac946cafaacc9ee2478ea48ed9e72554f79ed (patch) | |
| tree | 4d05744b9801e94f5da6563d8fabddfb20d1c7b7 /azalea-core/src | |
| parent | d5fa5e32b37754b3b5c136e58821e48cd3b7c2ff (diff) | |
| download | azalea-drasl-b21ac946cafaacc9ee2478ea48ed9e72554f79ed.tar.xz | |
Merge AzaleaRead and AzaleaWrite (#305)
Diffstat (limited to 'azalea-core/src')
| -rw-r--r-- | azalea-core/src/bitset.rs | 15 | ||||
| -rw-r--r-- | azalea-core/src/delta.rs | 8 | ||||
| -rw-r--r-- | azalea-core/src/difficulty.rs | 7 | ||||
| -rw-r--r-- | azalea-core/src/entity_id.rs | 10 | ||||
| -rw-r--r-- | azalea-core/src/filterable.rs | 35 | ||||
| -rw-r--r-- | azalea-core/src/game_type.rs | 14 | ||||
| -rw-r--r-- | azalea-core/src/position.rs | 47 |
7 files changed, 53 insertions, 83 deletions
diff --git a/azalea-core/src/bitset.rs b/azalea-core/src/bitset.rs index 6abb5d1d..f8c9da28 100644 --- a/azalea-core/src/bitset.rs +++ b/azalea-core/src/bitset.rs @@ -3,7 +3,7 @@ use std::{ ops::Range, }; -use azalea_buf::{AzBuf, AzaleaRead, AzaleaWrite, BufReadError}; +use azalea_buf::{AzBuf, BufReadError}; /// Represents Java's BitSet, a list of bits. #[derive(AzBuf, Clone, Debug, Default, Eq, Hash, PartialEq)] @@ -176,8 +176,8 @@ impl From<Vec<u8>> for BitSet { /// /// Note that this is optimized for fast serialization and deserialization for /// Minecraft, and may not be as performant as it could be for other purposes. -/// Consider using [`FastFixedBitSet`] if you don't need the -/// `AzaleaRead`/`AzaleaWrite` implementation. +/// Consider using [`FastFixedBitSet`] if you don't need the `AzBuf` +/// implementation. #[derive(Clone, Debug, Eq, Hash, PartialEq)] pub struct FixedBitSet<const N: usize> where @@ -211,7 +211,7 @@ where } } -impl<const N: usize> AzaleaRead for FixedBitSet<N> +impl<const N: usize> AzBuf for FixedBitSet<N> where [u8; bits_to_bytes(N)]: Sized, { @@ -222,11 +222,6 @@ where } Ok(FixedBitSet { data }) } -} -impl<const N: usize> AzaleaWrite for FixedBitSet<N> -where - [u8; bits_to_bytes(N)]: Sized, -{ fn azalea_write(&self, buf: &mut impl Write) -> io::Result<()> { for i in 0..bits_to_bytes(N) { self.data[i].azalea_write(buf)?; @@ -253,7 +248,7 @@ pub const fn bits_to_bytes(n: usize) -> usize { /// use it like `FastFixedBitSet<20>` if you need 20 bits. /// /// This is almost identical to [`FixedBitSet`], but more efficient (~20% faster -/// access) and doesn't implement `AzaleaRead`/`AzaleaWrite`. +/// access) and doesn't implement `AzBuf`. #[derive(Clone, Debug, Eq, Hash, PartialEq)] pub struct FastFixedBitSet<const N: usize> where diff --git a/azalea-core/src/delta.rs b/azalea-core/src/delta.rs index 055ba3b8..c45900d8 100644 --- a/azalea-core/src/delta.rs +++ b/azalea-core/src/delta.rs @@ -1,7 +1,7 @@ use std::io::{self, Cursor, Write}; pub use azalea_buf::AzBuf; -use azalea_buf::{AzaleaRead, AzaleaReadVar, AzaleaWrite, AzaleaWriteVar, BufReadError}; +use azalea_buf::{AzBufVar, BufReadError}; use crate::{math, position::Vec3}; @@ -91,7 +91,7 @@ pub enum LpVec3 { }, } -impl AzaleaRead for LpVec3 { +impl AzBuf for LpVec3 { fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { let a = u8::azalea_read(buf)?; if a == 0 { @@ -106,8 +106,6 @@ impl AzaleaRead for LpVec3 { Ok(LpVec3::Normal { a, b, c }) } } -} -impl AzaleaWrite for LpVec3 { fn azalea_write(&self, buf: &mut impl Write) -> io::Result<()> { match self { LpVec3::Zero => { @@ -218,8 +216,6 @@ impl From<Vec3> for LpVec3 { } #[cfg(test)] mod tests { - use azalea_buf::AzaleaWrite; - use super::*; static TEST_VALUES: [Vec3; 3] = [ diff --git a/azalea-core/src/difficulty.rs b/azalea-core/src/difficulty.rs index 7a018a8e..81db0f3a 100644 --- a/azalea-core/src/difficulty.rs +++ b/azalea-core/src/difficulty.rs @@ -3,7 +3,7 @@ use std::{ io::{self, Cursor, Write}, }; -use azalea_buf::{AzaleaRead, AzaleaWrite, BufReadError}; +use azalea_buf::{AzBuf, BufReadError}; #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] pub enum Difficulty { @@ -61,13 +61,10 @@ impl Difficulty { } } -impl AzaleaRead for Difficulty { +impl AzBuf for Difficulty { fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { Ok(Difficulty::by_id(u8::azalea_read(buf)?)) } -} - -impl AzaleaWrite for Difficulty { fn azalea_write(&self, buf: &mut impl Write) -> io::Result<()> { u8::azalea_write(&self.id(), buf) } diff --git a/azalea-core/src/entity_id.rs b/azalea-core/src/entity_id.rs index c66d3a2e..59798e05 100644 --- a/azalea-core/src/entity_id.rs +++ b/azalea-core/src/entity_id.rs @@ -4,7 +4,7 @@ use std::{ io::{self, Cursor}, }; -use azalea_buf::{AzaleaRead, AzaleaReadVar, AzaleaWrite, AzaleaWriteVar, BufReadError}; +use azalea_buf::{AzBuf, AzBufVar, BufReadError}; use derive_more::{Deref, DerefMut}; // note: this is here instead of in azalea-entity because azalea-world depends @@ -41,22 +41,18 @@ impl nohash_hasher::IsEnabled for MinecraftEntityId {} // we can't have the default be #[var] because mojang doesn't use varints for // entities sometimes :( -impl AzaleaRead for MinecraftEntityId { +impl AzBuf for MinecraftEntityId { fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { i32::azalea_read(buf).map(MinecraftEntityId) } -} -impl AzaleaWrite for MinecraftEntityId { fn azalea_write(&self, buf: &mut impl io::Write) -> io::Result<()> { i32::azalea_write(&self.0, buf) } } -impl AzaleaReadVar for MinecraftEntityId { +impl AzBufVar for MinecraftEntityId { fn azalea_read_var(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { i32::azalea_read_var(buf).map(MinecraftEntityId) } -} -impl AzaleaWriteVar for MinecraftEntityId { fn azalea_write_var(&self, buf: &mut impl io::Write) -> io::Result<()> { i32::azalea_write_var(&self.0, buf) } diff --git a/azalea-core/src/filterable.rs b/azalea-core/src/filterable.rs index 1c9766b6..16de6711 100644 --- a/azalea-core/src/filterable.rs +++ b/azalea-core/src/filterable.rs @@ -3,7 +3,7 @@ use std::{ io::{self, Cursor, Write}, }; -use azalea_buf::{AzaleaRead, AzaleaReadLimited, AzaleaReadVar, AzaleaWrite}; +use azalea_buf::{AzBuf, AzBufLimited, AzBufVar}; /// Used for written books. #[cfg_attr(feature = "serde", derive(serde::Serialize))] @@ -13,36 +13,39 @@ pub struct Filterable<T> { pub filtered: Option<T>, } -impl<T: AzaleaWrite> azalea_buf::AzaleaWrite for Filterable<T> { +impl<T: AzBuf> AzBuf for Filterable<T> { + fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, azalea_buf::BufReadError> { + let raw = AzBuf::azalea_read(buf)?; + let filtered = AzBuf::azalea_read(buf)?; + Ok(Self { raw, filtered }) + } fn azalea_write(&self, buf: &mut impl Write) -> io::Result<()> { self.raw.azalea_write(buf)?; self.filtered.azalea_write(buf)?; Ok(()) } } -impl<T: AzaleaRead> azalea_buf::AzaleaRead for Filterable<T> { - fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, azalea_buf::BufReadError> { - let raw = AzaleaRead::azalea_read(buf)?; - let filtered = AzaleaRead::azalea_read(buf)?; - Ok(Self { raw, filtered }) - } -} -impl<T: AzaleaReadLimited> azalea_buf::AzaleaReadLimited for Filterable<T> { +impl<T: AzBufLimited> AzBufLimited for Filterable<T> { fn azalea_read_limited( buf: &mut Cursor<&[u8]>, - limit: usize, + limit: u32, ) -> Result<Self, azalea_buf::BufReadError> { - let raw = AzaleaReadLimited::azalea_read_limited(buf, limit)?; - let filtered = AzaleaReadLimited::azalea_read_limited(buf, limit)?; + let raw = AzBufLimited::azalea_read_limited(buf, limit)?; + let filtered = AzBufLimited::azalea_read_limited(buf, limit)?; Ok(Self { raw, filtered }) } } -impl<T: AzaleaReadVar> azalea_buf::AzaleaReadVar for Filterable<T> { +impl<T: AzBufVar> AzBufVar for Filterable<T> { fn azalea_read_var(buf: &mut Cursor<&[u8]>) -> Result<Self, azalea_buf::BufReadError> { - let raw = AzaleaReadVar::azalea_read_var(buf)?; - let filtered = AzaleaReadVar::azalea_read_var(buf)?; + let raw = AzBufVar::azalea_read_var(buf)?; + let filtered = AzBufVar::azalea_read_var(buf)?; Ok(Self { raw, filtered }) } + fn azalea_write_var(&self, buf: &mut impl Write) -> io::Result<()> { + self.raw.azalea_write_var(buf)?; + self.filtered.azalea_write_var(buf)?; + Ok(()) + } } impl<T: Clone> Clone for Filterable<T> { diff --git a/azalea-core/src/game_type.rs b/azalea-core/src/game_type.rs index 95d330d2..a8c73a41 100644 --- a/azalea-core/src/game_type.rs +++ b/azalea-core/src/game_type.rs @@ -1,6 +1,6 @@ use std::io::{self, Cursor, Write}; -use azalea_buf::{AzaleaRead, AzaleaReadVar, AzaleaWrite, BufReadError}; +use azalea_buf::{AzBuf, AzBufVar, BufReadError}; use azalea_chat::translatable_component::TranslatableComponent; use tracing::debug; @@ -93,7 +93,7 @@ impl GameMode { } } -impl AzaleaRead for GameMode { +impl AzBuf for GameMode { fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { let id = u32::azalea_read_var(buf)?; let id = id.try_into().unwrap_or_else(|_| { @@ -105,15 +105,12 @@ impl AzaleaRead for GameMode { GameMode::Survival })) } -} - -impl AzaleaWrite for GameMode { fn azalea_write(&self, buf: &mut impl Write) -> io::Result<()> { u8::azalea_write(&self.to_id(), buf) } } -/// Rust doesn't let us `impl AzaleaRead for Option<GameType>` so we have to +/// Rust doesn't let us `impl AzBuf for Option<GameType>` so we have to /// make a new type :( #[derive(Clone, Copy, Debug, Hash, PartialEq)] pub struct OptionalGameType(pub Option<GameMode>); @@ -130,14 +127,11 @@ impl From<OptionalGameType> for Option<GameMode> { } } -impl AzaleaRead for OptionalGameType { +impl AzBuf for OptionalGameType { fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { let id = i8::azalea_read(buf)?; GameMode::from_optional_id(id).ok_or(BufReadError::UnexpectedEnumVariant { id: id as i32 }) } -} - -impl AzaleaWrite for OptionalGameType { fn azalea_write(&self, buf: &mut impl Write) -> io::Result<()> { GameMode::to_optional_id(*self).azalea_write(buf) } diff --git a/azalea-core/src/position.rs b/azalea-core/src/position.rs index 6da037ff..3f2a724f 100644 --- a/azalea-core/src/position.rs +++ b/azalea-core/src/position.rs @@ -12,7 +12,7 @@ use std::{ str::FromStr, }; -use azalea_buf::{AzBuf, AzaleaRead, AzaleaWrite, BufReadError}; +use azalea_buf::{AzBuf, BufReadError}; use azalea_registry::identifier::Identifier; #[cfg(feature = "serde")] use serde::Serializer; @@ -584,13 +584,11 @@ impl From<u64> for ChunkPos { } } } -impl AzaleaRead for ChunkPos { +impl AzBuf for ChunkPos { fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { let long = u64::azalea_read(buf)?; Ok(ChunkPos::from(long)) } -} -impl AzaleaWrite for ChunkPos { fn azalea_write(&self, buf: &mut impl Write) -> io::Result<()> { u64::from(*self).azalea_write(buf)?; Ok(()) @@ -986,7 +984,7 @@ const PACKED_Z_MASK: u64 = (1 << PACKED_Z_LENGTH) - 1; const Z_OFFSET: u64 = PACKED_Y_LENGTH; const X_OFFSET: u64 = PACKED_Y_LENGTH + PACKED_Z_LENGTH; -impl AzaleaRead for BlockPos { +impl AzBuf for BlockPos { fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { let val = i64::azalea_read(buf)?; let x = (val << (64 - X_OFFSET - PACKED_X_LENGTH) >> (64 - PACKED_X_LENGTH)) as i32; @@ -994,18 +992,31 @@ impl AzaleaRead for BlockPos { let z = (val << (64 - Z_OFFSET - PACKED_Z_LENGTH) >> (64 - PACKED_Z_LENGTH)) as i32; Ok(BlockPos { x, y, z }) } + fn azalea_write(&self, buf: &mut impl Write) -> io::Result<()> { + let mut val: u64 = 0; + val |= ((self.x as u64) & PACKED_X_MASK) << X_OFFSET; + val |= (self.y as u64) & PACKED_Y_MASK; + val |= ((self.z as u64) & PACKED_Z_MASK) << Z_OFFSET; + val.azalea_write(buf) + } } -impl AzaleaRead for GlobalPos { +impl AzBuf for GlobalPos { fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { Ok(GlobalPos { dimension: Identifier::azalea_read(buf)?, pos: BlockPos::azalea_read(buf)?, }) } + fn azalea_write(&self, buf: &mut impl Write) -> io::Result<()> { + Identifier::azalea_write(&self.dimension, buf)?; + BlockPos::azalea_write(&self.pos, buf)?; + + Ok(()) + } } -impl AzaleaRead for ChunkSectionPos { +impl AzBuf for ChunkSectionPos { fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { let long = i64::azalea_read(buf)?; Ok(ChunkSectionPos { @@ -1014,28 +1025,6 @@ impl AzaleaRead for ChunkSectionPos { z: (long << 22 >> 42) as i32, }) } -} - -impl AzaleaWrite for BlockPos { - fn azalea_write(&self, buf: &mut impl Write) -> io::Result<()> { - let mut val: u64 = 0; - val |= ((self.x as u64) & PACKED_X_MASK) << X_OFFSET; - val |= (self.y as u64) & PACKED_Y_MASK; - val |= ((self.z as u64) & PACKED_Z_MASK) << Z_OFFSET; - val.azalea_write(buf) - } -} - -impl AzaleaWrite for GlobalPos { - fn azalea_write(&self, buf: &mut impl Write) -> io::Result<()> { - Identifier::azalea_write(&self.dimension, buf)?; - BlockPos::azalea_write(&self.pos, buf)?; - - Ok(()) - } -} - -impl AzaleaWrite for ChunkSectionPos { fn azalea_write(&self, buf: &mut impl Write) -> io::Result<()> { let long = (((self.x & 0x3FFFFF) as i64) << 42) | (self.y & 0xFFFFF) as i64 |
