vortex_buffer/
const.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use std::ops::Deref;
5
6use vortex_error::VortexError;
7use vortex_error::vortex_bail;
8
9use crate::Alignment;
10use crate::Buffer;
11
12/// A buffer of items of `T` with a compile-time alignment.
13#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Hash)]
14pub struct ConstBuffer<T, const A: usize>(Buffer<T>);
15
16impl<T, const A: usize> ConstBuffer<T, A> {
17    /// Returns the alignment of the buffer.
18    pub const fn alignment() -> Alignment {
19        Alignment::new(A)
20    }
21
22    /// Align the given buffer (possibly with a copy) and return a new `ConstBuffer`.
23    pub fn align_from<B: Into<Buffer<T>>>(buf: B) -> Self {
24        Self(buf.into().aligned(Self::alignment()))
25    }
26
27    /// Create a new [`ConstBuffer`] with a copy from the provided slice.
28    pub fn copy_from<B: AsRef<[T]>>(buf: B) -> Self {
29        Self(Buffer::<T>::copy_from_aligned(buf, Self::alignment()))
30    }
31
32    /// Returns a slice over the buffer of elements of type T.
33    #[inline(always)]
34    pub fn as_slice(&self) -> &[T] {
35        self.0.as_slice()
36    }
37
38    /// Unwrap the inner buffer.
39    pub fn inner(&self) -> &Buffer<T> {
40        &self.0
41    }
42
43    /// Unwrap the inner buffer.
44    pub fn into_inner(self) -> Buffer<T> {
45        self.0
46    }
47}
48
49impl<T, const A: usize> TryFrom<Buffer<T>> for ConstBuffer<T, A> {
50    type Error = VortexError;
51
52    fn try_from(value: Buffer<T>) -> Result<Self, Self::Error> {
53        if !value.alignment().is_aligned_to(Alignment::new(A)) {
54            vortex_bail!(
55                "Cannot convert buffer with alignment {} to buffer with alignment {}",
56                value.alignment(),
57                A
58            );
59        }
60        Ok(Self(value))
61    }
62}
63
64impl<T, const A: usize> AsRef<Buffer<T>> for ConstBuffer<T, A> {
65    fn as_ref(&self) -> &Buffer<T> {
66        &self.0
67    }
68}
69
70impl<T, const A: usize> Deref for ConstBuffer<T, A> {
71    type Target = [T];
72
73    fn deref(&self) -> &Self::Target {
74        self.0.as_slice()
75    }
76}