#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct Layout<const N: usize> {
pub(super) shape: [usize; N],
pub(super) strides: [isize; N],
pub(super) offset: usize,
}
impl<const N: usize> Layout<N> {
pub fn try_new(
shape: [usize; N],
strides: [isize; N],
offset: usize,
) -> crate::domain::error::Result<Self> {
let candidate = Self::from_parts_unchecked(shape, strides, offset);
candidate.checked_size()?;
candidate.checked_min_max_offsets()?;
Ok(candidate)
}
#[inline]
pub(crate) const fn from_parts_unchecked(
shape: [usize; N],
strides: [isize; N],
offset: usize,
) -> Self {
Self {
shape,
strides,
offset,
}
}
#[inline]
#[must_use]
pub const fn shape(&self) -> [usize; N] {
self.shape
}
#[inline]
#[must_use]
pub const fn strides(&self) -> [isize; N] {
self.strides
}
#[inline]
#[must_use]
pub const fn offset(&self) -> usize {
self.offset
}
}
impl<const N: usize> TryFrom<([usize; N], [isize; N], usize)> for Layout<N> {
type Error = crate::domain::error::LetoError;
#[inline]
fn try_from(
(shape, strides, offset): ([usize; N], [isize; N], usize),
) -> core::result::Result<Self, Self::Error> {
Self::try_new(shape, strides, offset)
}
}