use std::num::NonZeroUsize;
pub const ONE: NonZeroUsize = NonZeroUsize::new(1).unwrap();
pub fn read_exact_into<T, R>(reader: &mut R, count: usize) -> std::io::Result<Vec<T>>
where
T: bytemuck::Pod + Default,
R: std::io::Read,
{
let mut buf = vec![T::default(); count];
let byte_slice = bytemuck::must_cast_slice_mut(&mut buf);
reader.read_exact(byte_slice)?;
Ok(buf)
}
pub unsafe trait IntoUsize {
fn into_usize(self) -> usize;
}
macro_rules! impl_to_usize {
($type:ty) => {
#[cfg(target_pointer_width = "64")]
unsafe impl IntoUsize for $type {
fn into_usize(self) -> usize {
#[allow(unused)]
const STATIC_ASSERT: () = {
if usize::BITS != 64 {
panic!("diskann is not compatible with non-64-bit systems");
}
};
self as usize
}
}
};
}
impl_to_usize!(u8);
impl_to_usize!(u16);
impl_to_usize!(u32);
impl_to_usize!(u64);
impl_to_usize!(usize);
pub trait TypeStr {
fn type_str() -> &'static str;
}
impl TypeStr for u32 {
fn type_str() -> &'static str {
"u32"
}
}
impl TypeStr for u64 {
fn type_str() -> &'static str {
"u64"
}
}
impl TypeStr for usize {
fn type_str() -> &'static str {
"usize"
}
}
#[cfg(test)]
mod test_utils {
use super::*;
#[test]
fn type_str() {
assert_eq!(u32::type_str(), "u32");
assert_eq!(u64::type_str(), "u64");
assert_eq!(usize::type_str(), "usize");
}
}