Skip to content

Commit

Permalink
add safety comments and simplify some code
Browse files Browse the repository at this point in the history
  • Loading branch information
mat-1 committed Oct 5, 2023
1 parent c2fb991 commit 17734cd
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 10 deletions.
2 changes: 2 additions & 0 deletions azalea-core/src/bitset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,12 @@ where
}
}

#[inline]
pub fn index(&self, index: usize) -> bool {
(self.data[index / 8] & (1u8 << (index % 8))) != 0
}

#[inline]
pub fn set(&mut self, bit_index: usize) {
self.data[bit_index / 8] |= 1u8 << (bit_index % 8);
}
Expand Down
11 changes: 1 addition & 10 deletions azalea-core/src/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,6 @@ impl ChunkSectionPos {
}
/// The coordinates of a block inside a chunk.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[repr(align(8))]
pub struct ChunkBlockPos {
pub x: u8,
pub y: i32,
Expand Down Expand Up @@ -300,7 +299,6 @@ impl nohash_hasher::IsEnabled for ChunkBlockPos {}
/// The coordinates of a block inside a chunk section. Each coordinate must be
/// in the range [0, 15].
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[repr(align(4))]
pub struct ChunkSectionBlockPos {
pub x: u8,
pub y: u8,
Expand Down Expand Up @@ -330,14 +328,7 @@ impl Hash for ChunkSectionBlockPos {
impl From<ChunkSectionBlockPos> for u16 {
#[inline]
fn from(pos: ChunkSectionBlockPos) -> Self {
let mut val: u16 = 0;
// first 4 bits are z
val |= pos.z as u16;
// next 4 bits are y
val |= (pos.y as u16) << 4;
// last 4 bits are x
val |= (pos.x as u16) << 8;
val
(pos.z as u16) | ((pos.y as u16) << 4) | ((pos.x as u16) << 8)
}
}
impl nohash_hasher::IsEnabled for ChunkSectionBlockPos {}
Expand Down
2 changes: 2 additions & 0 deletions azalea/src/pathfinder/moves/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ impl PathfinderCtx {
let (section_pos, section_block_pos) =
(ChunkSectionPos::from(pos), ChunkSectionBlockPos::from(pos));
let index = u16::from(section_block_pos) as usize;
// SAFETY: we're only accessing this from one thread
let cached_block_passable = unsafe { &mut *self.cached_block_passable.get() };
if let Some(cached) = cached_block_passable.iter_mut().find_map(|cached| {
if cached.pos == section_pos {
Expand Down Expand Up @@ -183,6 +184,7 @@ impl PathfinderCtx {
let (section_pos, section_block_pos) =
(ChunkSectionPos::from(pos), ChunkSectionBlockPos::from(pos));
let index = u16::from(section_block_pos) as usize;
// SAFETY: we're only accessing this from one thread
let cached_block_solid = unsafe { &mut *self.cached_block_solid.get() };
if let Some(cached) = cached_block_solid.iter_mut().find_map(|cached| {
if cached.pos == section_pos {
Expand Down

0 comments on commit 17734cd

Please sign in to comment.