From a64c6505049082175224802c5be51ac8f0cf4677 Mon Sep 17 00:00:00 2001 From: mat Date: Fri, 30 May 2025 12:59:08 -1345 Subject: make fixedbitset require generic const exprs again :3 --- azalea-core/src/bitset.rs | 53 +++++++++++++++++++++++++++++++---------------- azalea-core/src/lib.rs | 2 ++ 2 files changed, 37 insertions(+), 18 deletions(-) (limited to 'azalea-core/src') diff --git a/azalea-core/src/bitset.rs b/azalea-core/src/bitset.rs index 5af05e19..715c57fb 100644 --- a/azalea-core/src/bitset.rs +++ b/azalea-core/src/bitset.rs @@ -127,23 +127,27 @@ impl From> for BitSet { /// A list of bits with a known fixed size. /// /// The `N` is the number of bytes reserved for the bitset. You're encouraged to -/// use it like `FixedBitSet<{ 20_usize.div_ceil(8) }>` if you need 20 bits. -/// -/// TODO: this should be changed back to bits once this is resolved: -/// +/// use it like `FixedBitSet<20>` if you need 20 bits. /// /// Note that this is primarily meant for fast serialization and deserialization -/// for Minecraft, if you don't need that you should use the `fixedbitset` crate -/// since it's approximately 20% faster (since it stores the data as usizes -/// instead of u8s). +/// for Minecraft. An alternative that would be ~20% faster for accessing data +/// could store it interally as `usize`s instead of `u8`s. #[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct FixedBitSet { - data: [u8; N], +pub struct FixedBitSet +where + [u8; bits_to_bytes(N)]: Sized, +{ + data: [u8; bits_to_bytes(N)], } -impl FixedBitSet { - pub fn new() -> Self { - FixedBitSet { data: [0; N] } +impl FixedBitSet +where + [u8; bits_to_bytes(N)]: Sized, +{ + pub const fn new() -> Self { + FixedBitSet { + data: [0; bits_to_bytes(N)], + } } #[inline] @@ -157,29 +161,42 @@ impl FixedBitSet { } } -impl AzaleaRead for FixedBitSet { +impl AzaleaRead for FixedBitSet +where + [u8; bits_to_bytes(N)]: Sized, +{ fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result { - let mut data = [0; N]; - for item in data.iter_mut().take(N) { + let mut data = [0; bits_to_bytes(N)]; + for item in data.iter_mut().take(bits_to_bytes(N)) { *item = u8::azalea_read(buf)?; } Ok(FixedBitSet { data }) } } -impl AzaleaWrite for FixedBitSet { +impl AzaleaWrite for FixedBitSet +where + [u8; bits_to_bytes(N)]: Sized, +{ fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - for i in 0..N { + for i in 0..bits_to_bytes(N) { self.data[i].azalea_write(buf)?; } Ok(()) } } -impl Default for FixedBitSet { +impl Default for FixedBitSet +where + [u8; bits_to_bytes(N)]: Sized, +{ fn default() -> Self { Self::new() } } +pub const fn bits_to_bytes(n: usize) -> usize { + n.div_ceil(8) +} + #[cfg(test)] mod tests { use super::*; diff --git a/azalea-core/src/lib.rs b/azalea-core/src/lib.rs index 6c5e57c1..9f6e9386 100644 --- a/azalea-core/src/lib.rs +++ b/azalea-core/src/lib.rs @@ -1,3 +1,5 @@ +#![allow(incomplete_features)] +#![feature(generic_const_exprs)] #![doc = include_str!("../README.md")] pub mod aabb; -- cgit v1.2.3