cross: DimSlice: allow the underlying data to be a Vec

This commit is contained in:
2022-08-19 03:54:01 -07:00
parent 35dbdffda7
commit 9f97f474d7
2 changed files with 38 additions and 2 deletions

View File

@@ -7,7 +7,7 @@ use crate::vec::Vec3u;
/// use this to wrap a flat region of memory into something which can be indexed by coordinates in /// use this to wrap a flat region of memory into something which can be indexed by coordinates in
/// 3d space. /// 3d space.
#[cfg_attr(feature = "fmt", derive(Debug))] #[cfg_attr(feature = "fmt", derive(Debug))]
#[derive(PartialEq)] #[derive(Clone, PartialEq)]
pub struct DimSlice<T> { pub struct DimSlice<T> {
dim: Vec3u, dim: Vec3u,
items: T, items: T,
@@ -78,6 +78,42 @@ impl<'a, T: IndexMut<usize> + ?Sized> IndexMut<Vec3u> for DimSlice<&'a mut T> {
} }
} }
#[cfg(feature = "std")]
impl<T> Index<Vec3u> for DimSlice<Vec<T>> {
type Output=T;
fn index(&self, idx: Vec3u) -> &Self::Output {
let idx = index(idx, self.dim);
&self.items[idx]
}
}
#[cfg(feature = "std")]
impl<T> IndexMut<Vec3u> for DimSlice<Vec<T>> {
fn index_mut(&mut self, idx: Vec3u) -> &mut Self::Output {
let idx = index(idx, self.dim);
&mut self.items[idx]
}
}
#[cfg(feature = "std")]
impl<T> Index<Vec3u> for DimSlice<Box<[T]>> {
type Output=T;
fn index(&self, idx: Vec3u) -> &Self::Output {
let idx = index(idx, self.dim);
&self.items[idx]
}
}
#[cfg(feature = "std")]
impl<T> IndexMut<Vec3u> for DimSlice<Box<[T]>> {
fn index_mut(&mut self, idx: Vec3u) -> &mut Self::Output {
let idx = index(idx, self.dim);
&mut self.items[idx]
}
}
impl<T: IntoIterator> IntoIterator for DimSlice<T> { impl<T: IntoIterator> IntoIterator for DimSlice<T> {
type Item = T::Item; type Item = T::Item;
type IntoIter = T::IntoIter; type IntoIter = T::IntoIter;

View File

@@ -7,7 +7,7 @@ use crate::vec::Vec3u;
#[cfg_attr(feature = "fmt", derive(Debug))] #[cfg_attr(feature = "fmt", derive(Debug))]
#[derive(PartialEq)] #[derive(Clone, PartialEq)]
pub struct OffsetDimSlice<T> { pub struct OffsetDimSlice<T> {
offset: Vec3u, offset: Vec3u,
inner: DimSlice<T>, inner: DimSlice<T>,