use super::{MARKER_CONFLICT, calculate_bucket_position};
pub struct Reader<'a>(&'a [u8]);
impl<'a> Reader<'a> {
#[must_use]
pub fn new(bytes: &'a [u8], offset: u32, len: u32) -> Self {
let offset = offset as usize;
let len = len as usize;
let end = offset + len;
#[expect(
clippy::indexing_slicing,
reason = "we consider the caller to be trustworthy"
)]
Self(&bytes[offset..end])
}
#[must_use]
pub fn bucket_count(&self) -> usize {
self.0.len()
}
#[must_use]
#[expect(
clippy::naive_bytecount,
reason = "only used in metrics, so no need to be hyper-optimized"
)]
pub fn conflict_count(&self) -> usize {
self.0
.iter()
.filter(|&&byte| byte == MARKER_CONFLICT)
.count()
}
#[must_use]
pub fn get(&self, key: &[u8]) -> u8 {
#[expect(
clippy::cast_possible_truncation,
reason = "even with very high hash ratio, there won't be nearly enough items to cause us to create ~4 billion buckets"
)]
let bucket_count = self.0.len() as u32;
let bucket_pos = calculate_bucket_position(key, bucket_count);
*unsafe { self.0.get_unchecked(bucket_pos) }
}
}