use crate::request::RECORD_ALIGNMENT;
pub(crate) struct NativeBuffer {
words: Vec<u64>,
}
impl NativeBuffer {
pub(crate) fn try_new(capacity: usize) -> Option<Self> {
debug_assert_eq!(
capacity % RECORD_ALIGNMENT,
0,
"an effective capacity is always a whole number of words"
);
let words = capacity / RECORD_ALIGNMENT;
let mut storage: Vec<u64> = Vec::new();
storage.try_reserve_exact(words).ok()?;
storage.resize(words, 0);
Some(Self { words: storage })
}
pub(crate) fn capacity(&self) -> u32 {
let bytes = self.words.len() * RECORD_ALIGNMENT;
u32::try_from(bytes).expect("an effective capacity always fits a u32")
}
pub(crate) fn as_mut_ptr(&mut self) -> *mut core::ffi::c_void {
self.words.as_mut_ptr().cast()
}
pub(crate) fn as_bytes(&self) -> &[u8] {
unsafe {
core::slice::from_raw_parts(
self.words.as_ptr().cast::<u8>(),
self.words.len() * RECORD_ALIGNMENT,
)
}
}
}
impl std::fmt::Debug for NativeBuffer {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("NativeBuffer")
.field("capacity", &self.capacity())
.finish()
}
}
#[cfg(test)]
mod tests;