Skip to content

Commit

Permalink
fix for latest nightly by changing the FixedBitSet generic to take by…
Browse files Browse the repository at this point in the history
…tes instead of bits
  • Loading branch information
mat-1 committed Dec 11, 2024
1 parent 2feef49 commit 097a620
Show file tree
Hide file tree
Showing 17 changed files with 51 additions and 111 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ flamegraph.svg
perf.data
perf.data.old
heaptrack.*

rustc-ice-*
40 changes: 16 additions & 24 deletions azalea-core/src/bitset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,26 +126,24 @@ 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
///
/// 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)
/// instead of u8s).
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct FixedBitSet<const N: usize>
where
[(); N.div_ceil(8)]: Sized,
{
data: [u8; N.div_ceil(8)],
pub struct FixedBitSet<const N: usize> {
data: [u8; N],
}

impl<const N: usize> FixedBitSet<N>
where
[u8; N.div_ceil(8)]: Sized,
{
impl<const N: usize> FixedBitSet<N> {
pub fn new() -> Self {
FixedBitSet {
data: [0; N.div_ceil(8)],
}
FixedBitSet { data: [0; N] }
}

#[inline]
Expand All @@ -159,24 +157,18 @@ where
}
}

impl<const N: usize> AzaleaRead for FixedBitSet<N>
where
[u8; N.div_ceil(8)]: Sized,
{
impl<const N: usize> AzaleaRead for FixedBitSet<N> {
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let mut data = [0; N.div_ceil(8)];
for item in data.iter_mut().take(N.div_ceil(8)) {
let mut data = [0; N];
for item in data.iter_mut().take(N) {
*item = u8::azalea_read(buf)?;
}
Ok(FixedBitSet { data })
}
}
impl<const N: usize> AzaleaWrite for FixedBitSet<N>
where
[u8; N.div_ceil(8)]: Sized,
{
impl<const N: usize> AzaleaWrite for FixedBitSet<N> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
for i in 0..N.div_ceil(8) {
for i in 0..N {
self.data[i].azalea_write(buf)?;
}
Ok(())
Expand Down
53 changes: 0 additions & 53 deletions azalea-protocol/build.rs

This file was deleted.

4 changes: 2 additions & 2 deletions azalea-protocol/src/common/client_information.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ impl Default for ModelCustomization {

impl AzaleaRead for ModelCustomization {
fn azalea_read(buf: &mut std::io::Cursor<&[u8]>) -> Result<Self, azalea_buf::BufReadError> {
let set = FixedBitSet::<7>::azalea_read(buf)?;
let set = FixedBitSet::<{ 7_usize.div_ceil(8) }>::azalea_read(buf)?;
Ok(Self {
cape: set.index(0),
jacket: set.index(1),
Expand All @@ -112,7 +112,7 @@ impl AzaleaRead for ModelCustomization {

impl AzaleaWrite for ModelCustomization {
fn azalea_write(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> {
let mut set = FixedBitSet::<7>::new();
let mut set = FixedBitSet::<{ 7_usize.div_ceil(8) }>::new();
if self.cape {
set.set(0);
}
Expand Down
4 changes: 2 additions & 2 deletions azalea-protocol/src/packets/game/c_boss_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub struct Properties {

impl AzaleaRead for Properties {
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let set = FixedBitSet::<3>::azalea_read(buf)?;
let set = FixedBitSet::<{ 3_usize.div_ceil(8) }>::azalea_read(buf)?;
Ok(Self {
darken_screen: set.index(0),
play_music: set.index(1),
Expand All @@ -127,7 +127,7 @@ impl AzaleaRead for Properties {

impl AzaleaWrite for Properties {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
let mut set = FixedBitSet::<3>::new();
let mut set = FixedBitSet::<{ 3_usize.div_ceil(8) }>::new();
if self.darken_screen {
set.set(0);
}
Expand Down
12 changes: 6 additions & 6 deletions azalea-protocol/src/packets/game/c_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl<T: PartialEq> PartialEq for BrigadierNumber<T> {

impl<T: AzaleaRead> AzaleaRead for BrigadierNumber<T> {
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let flags = FixedBitSet::<2>::azalea_read(buf)?;
let flags = FixedBitSet::<{ 2_usize.div_ceil(8) }>::azalea_read(buf)?;
let min = if flags.index(0) {
Some(T::azalea_read(buf)?)
} else {
Expand All @@ -62,7 +62,7 @@ impl<T: AzaleaRead> AzaleaRead for BrigadierNumber<T> {
}
impl<T: AzaleaWrite> AzaleaWrite for BrigadierNumber<T> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
let mut flags = FixedBitSet::<2>::new();
let mut flags = FixedBitSet::<{ 2_usize.div_ceil(8) }>::new();
if self.min.is_some() {
flags.set(0);
}
Expand Down Expand Up @@ -156,7 +156,7 @@ pub struct EntityParser {
}
impl AzaleaRead for EntityParser {
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let flags = FixedBitSet::<2>::azalea_read(buf)?;
let flags = FixedBitSet::<{ 2_usize.div_ceil(8) }>::azalea_read(buf)?;
Ok(EntityParser {
single: flags.index(0),
players_only: flags.index(1),
Expand All @@ -165,7 +165,7 @@ impl AzaleaRead for EntityParser {
}
impl AzaleaWrite for EntityParser {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
let mut flags = FixedBitSet::<2>::new();
let mut flags = FixedBitSet::<{ 2_usize.div_ceil(8) }>::new();
if self.single {
flags.set(0);
}
Expand All @@ -180,7 +180,7 @@ impl AzaleaWrite for EntityParser {
// TODO: BrigadierNodeStub should have more stuff
impl AzaleaRead for BrigadierNodeStub {
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let flags = FixedBitSet::<8>::azalea_read(buf)?;
let flags = FixedBitSet::<{ 8_usize.div_ceil(8) }>::azalea_read(buf)?;
if flags.index(5) || flags.index(6) || flags.index(7) {
warn!("Warning: The flags from a Brigadier node are over 31. This is probably a bug.",);
}
Expand Down Expand Up @@ -239,7 +239,7 @@ impl AzaleaRead for BrigadierNodeStub {

impl AzaleaWrite for BrigadierNodeStub {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
let mut flags = FixedBitSet::<4>::new();
let mut flags = FixedBitSet::<{ 4_usize.div_ceil(8) }>::new();
if self.is_executable {
flags.set(2);
}
Expand Down
4 changes: 2 additions & 2 deletions azalea-protocol/src/packets/game/c_player_abilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub struct PlayerAbilitiesFlags {

impl AzaleaRead for PlayerAbilitiesFlags {
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let set = FixedBitSet::<4>::azalea_read(buf)?;
let set = FixedBitSet::<{ 4_usize.div_ceil(8) }>::azalea_read(buf)?;
Ok(PlayerAbilitiesFlags {
invulnerable: set.index(0),
flying: set.index(1),
Expand All @@ -35,7 +35,7 @@ impl AzaleaRead for PlayerAbilitiesFlags {

impl AzaleaWrite for PlayerAbilitiesFlags {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
let mut set = FixedBitSet::<4>::new();
let mut set = FixedBitSet::<{ 4_usize.div_ceil(8) }>::new();
if self.invulnerable {
set.set(0);
}
Expand Down
4 changes: 2 additions & 2 deletions azalea-protocol/src/packets/game/c_player_info_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ pub struct ActionEnumSet {

impl AzaleaRead for ActionEnumSet {
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let set = FixedBitSet::<7>::azalea_read(buf)?;
let set = FixedBitSet::<{ 7_usize.div_ceil(8) }>::azalea_read(buf)?;
Ok(ActionEnumSet {
add_player: set.index(0),
initialize_chat: set.index(1),
Expand All @@ -199,7 +199,7 @@ impl AzaleaRead for ActionEnumSet {

impl AzaleaWrite for ActionEnumSet {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
let mut set = FixedBitSet::<7>::new();
let mut set = FixedBitSet::<{ 7_usize.div_ceil(8) }>::new();
if self.add_player {
set.set(0);
}
Expand Down
4 changes: 2 additions & 2 deletions azalea-protocol/src/packets/game/c_player_position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub struct RelativeMovements {
impl AzaleaRead for RelativeMovements {
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
// yes minecraft seriously wastes that many bits, smh
let set = FixedBitSet::<32>::azalea_read(buf)?;
let set = FixedBitSet::<{ 32_usize.div_ceil(8) }>::azalea_read(buf)?;
Ok(RelativeMovements {
x: set.index(0),
y: set.index(1),
Expand All @@ -56,7 +56,7 @@ impl AzaleaRead for RelativeMovements {

impl AzaleaWrite for RelativeMovements {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
let mut set = FixedBitSet::<32>::new();
let mut set = FixedBitSet::<{ 32_usize.div_ceil(8) }>::new();
let mut set_bit = |index: usize, value: bool| {
if value {
set.set(index);
Expand Down
6 changes: 3 additions & 3 deletions azalea-protocol/src/packets/game/c_stop_sound.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::io::{Cursor, Write};

use azalea_buf::{BufReadError, AzaleaRead, AzaleaWrite};
use azalea_buf::{AzaleaRead, AzaleaWrite, BufReadError};
use azalea_core::{bitset::FixedBitSet, resource_location::ResourceLocation};
use azalea_protocol_macros::ClientboundGamePacket;

Expand All @@ -14,7 +14,7 @@ pub struct ClientboundStopSound {

impl AzaleaRead for ClientboundStopSound {
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let set = FixedBitSet::<2>::azalea_read(buf)?;
let set = FixedBitSet::<{ 2_usize.div_ceil(8) }>::azalea_read(buf)?;
let source = if set.index(0) {
Some(SoundSource::azalea_read(buf)?)
} else {
Expand All @@ -32,7 +32,7 @@ impl AzaleaRead for ClientboundStopSound {

impl AzaleaWrite for ClientboundStopSound {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
let mut set = FixedBitSet::<2>::new();
let mut set = FixedBitSet::<{ 2_usize.div_ceil(8) }>::new();
if self.source.is_some() {
set.set(0);
}
Expand Down
2 changes: 1 addition & 1 deletion azalea-protocol/src/packets/game/s_chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ pub struct ServerboundChat {
pub struct LastSeenMessagesUpdate {
#[var]
pub messages: u32,
pub acknowledged: FixedBitSet<20>,
pub acknowledged: FixedBitSet<{ 20_usize.div_ceil(8) }>,
}
4 changes: 2 additions & 2 deletions azalea-protocol/src/packets/game/s_player_abilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct ServerboundPlayerAbilities {

impl AzaleaRead for ServerboundPlayerAbilities {
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let set = FixedBitSet::<2>::azalea_read(buf)?;
let set = FixedBitSet::<{ 2_usize.div_ceil(8) }>::azalea_read(buf)?;
Ok(Self {
is_flying: set.index(1),
})
Expand All @@ -22,7 +22,7 @@ impl AzaleaRead for ServerboundPlayerAbilities {

impl AzaleaWrite for ServerboundPlayerAbilities {
fn azalea_write(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> {
let mut set = FixedBitSet::<2>::new();
let mut set = FixedBitSet::<{ 2_usize.div_ceil(8) }>::new();
if self.is_flying {
set.set(1);
}
Expand Down
4 changes: 2 additions & 2 deletions azalea-protocol/src/packets/game/s_player_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub struct ServerboundPlayerInput {

impl AzaleaRead for ServerboundPlayerInput {
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let set = FixedBitSet::<7>::azalea_read(buf)?;
let set = FixedBitSet::<{ 7_usize.div_ceil(8) }>::azalea_read(buf)?;
Ok(Self {
forward: set.index(0),
backward: set.index(1),
Expand All @@ -33,7 +33,7 @@ impl AzaleaRead for ServerboundPlayerInput {

impl AzaleaWrite for ServerboundPlayerInput {
fn azalea_write(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> {
let mut set = FixedBitSet::<7>::new();
let mut set = FixedBitSet::<{ 7_usize.div_ceil(8) }>::new();
if self.forward {
set.set(0);
}
Expand Down
4 changes: 2 additions & 2 deletions azalea-protocol/src/packets/game/s_set_command_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl AzaleaRead for ServerboundSetCommandBlock {
let command = String::azalea_read(buf)?;
let mode = Mode::azalea_read(buf)?;

let set = FixedBitSet::<3>::azalea_read(buf)?;
let set = FixedBitSet::<{ 3_usize.div_ceil(8) }>::azalea_read(buf)?;
Ok(Self {
pos,
command,
Expand All @@ -48,7 +48,7 @@ impl AzaleaWrite for ServerboundSetCommandBlock {
self.command.azalea_write(buf)?;
self.mode.azalea_write(buf)?;

let mut set = FixedBitSet::<3>::new();
let mut set = FixedBitSet::<{ 3_usize.div_ceil(8) }>::new();
if self.track_output {
set.set(0);
}
Expand Down
4 changes: 2 additions & 2 deletions azalea-protocol/src/packets/game/s_set_structure_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub struct Flags {

impl AzaleaRead for Flags {
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let set = FixedBitSet::<3>::azalea_read(buf)?;
let set = FixedBitSet::<{ 3_usize.div_ceil(8) }>::azalea_read(buf)?;
Ok(Self {
ignore_entities: set.index(0),
show_air: set.index(1),
Expand All @@ -82,7 +82,7 @@ impl AzaleaRead for Flags {

impl AzaleaWrite for Flags {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
let mut set = FixedBitSet::<3>::new();
let mut set = FixedBitSet::<{ 3_usize.div_ceil(8) }>::new();
if self.ignore_entities {
set.set(0);
}
Expand Down
8 changes: 4 additions & 4 deletions azalea/src/pathfinder/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ impl CachedSections {

pub struct CachedSection {
pub pos: ChunkSectionPos,
pub passable_bitset: FixedBitSet<4096>,
pub solid_bitset: FixedBitSet<4096>,
pub passable_bitset: FixedBitSet<{ 4096_usize.div_ceil(8) }>,
pub solid_bitset: FixedBitSet<{ 4096_usize.div_ceil(8) }>,
}

impl CachedWorld {
Expand Down Expand Up @@ -182,8 +182,8 @@ impl CachedWorld {

fn calculate_bitsets_for_section(&self, section_pos: ChunkSectionPos) -> Option<CachedSection> {
self.with_section(section_pos, |section| {
let mut passable_bitset = FixedBitSet::<4096>::new();
let mut solid_bitset = FixedBitSet::<4096>::new();
let mut passable_bitset = FixedBitSet::<{ 4096_usize.div_ceil(8) }>::new();
let mut solid_bitset = FixedBitSet::<{ 4096_usize.div_ceil(8) }>::new();
for i in 0..4096 {
let block_state_id = section.get_at_index(i);
let block_state = BlockState::try_from(block_state_id).unwrap_or(BlockState::AIR);
Expand Down
Loading

0 comments on commit 097a620

Please sign in to comment.