aboutsummaryrefslogtreecommitdiff
path: root/azalea-core/src
diff options
context:
space:
mode:
authormat <git@matdoes.dev>2025-05-30 12:59:08 -1345
committermat <git@matdoes.dev>2025-05-30 12:59:08 -1345
commita64c6505049082175224802c5be51ac8f0cf4677 (patch)
tree443877d470fed94810b4b7180b2584dd5ae3fc9d /azalea-core/src
parentcfdd8e690f230bc84fc126d5a8bfa13df0f6d781 (diff)
downloadazalea-drasl-a64c6505049082175224802c5be51ac8f0cf4677.tar.xz
make fixedbitset require generic const exprs again :3
Diffstat (limited to 'azalea-core/src')
-rw-r--r--azalea-core/src/bitset.rs53
-rw-r--r--azalea-core/src/lib.rs2
2 files changed, 37 insertions, 18 deletions
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<Vec<u8>> 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:
-/// <https://github.com/rust-lang/rust/issues/133199#issuecomment-2531645526>
+/// 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<const N: usize> {
- data: [u8; N],
+pub struct FixedBitSet<const N: usize>
+where
+ [u8; bits_to_bytes(N)]: Sized,
+{
+ data: [u8; bits_to_bytes(N)],
}
-impl<const N: usize> FixedBitSet<N> {
- pub fn new() -> Self {
- FixedBitSet { data: [0; N] }
+impl<const N: usize> FixedBitSet<N>
+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<const N: usize> FixedBitSet<N> {
}
}
-impl<const N: usize> AzaleaRead for FixedBitSet<N> {
+impl<const N: usize> AzaleaRead for FixedBitSet<N>
+where
+ [u8; bits_to_bytes(N)]: Sized,
+{
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
- 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<const N: usize> AzaleaWrite for FixedBitSet<N> {
+impl<const N: usize> AzaleaWrite for FixedBitSet<N>
+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<const N: usize> Default for FixedBitSet<N> {
+impl<const N: usize> Default for FixedBitSet<N>
+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;