use std::ops::Deref;
use vortex_error::{vortex_bail, VortexError};
use crate::{Alignment, Buffer};
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd)]
pub struct ConstBuffer<T, const A: usize>(Buffer<T>);
impl<T, const A: usize> ConstBuffer<T, A> {
pub const fn alignment() -> Alignment {
Alignment::new(A)
}
pub fn align_from<B: Into<Buffer<T>>>(buf: B) -> Self {
Self(buf.into().aligned(Self::alignment()))
}
pub fn into_inner(self) -> Buffer<T> {
self.0
}
}
impl<T, const A: usize> TryFrom<Buffer<T>> for ConstBuffer<T, A> {
type Error = VortexError;
fn try_from(value: Buffer<T>) -> Result<Self, Self::Error> {
if !value.alignment().is_aligned_to(Alignment::new(A)) {
vortex_bail!(
"Cannot convert buffer with alignment {} to buffer with alignment {}",
value.alignment(),
A
);
}
Ok(Self(value))
}
}
impl<T, const A: usize> AsRef<Buffer<T>> for ConstBuffer<T, A> {
fn as_ref(&self) -> &Buffer<T> {
&self.0
}
}
impl<T, const A: usize> Deref for ConstBuffer<T, A> {
type Target = [T];
fn deref(&self) -> &Self::Target {
self.0.as_slice()
}
}