diff --git a/crates/cross/src/dim/dim_slice.rs b/crates/cross/src/dim/dim_slice.rs index 9858217..f83e866 100644 --- a/crates/cross/src/dim/dim_slice.rs +++ b/crates/cross/src/dim/dim_slice.rs @@ -1,3 +1,4 @@ +use core::convert::{AsMut, AsRef}; use core::iter::Zip; use core::ops::{Index, IndexMut}; @@ -5,6 +6,8 @@ use crate::vec::Vec3u; /// use this to wrap a flat region of memory into something which can be indexed by coordinates in /// 3d space. +#[cfg_attr(feature = "fmt", derive(Debug))] +#[derive(PartialEq)] pub struct DimSlice { dim: Vec3u, items: T, @@ -20,6 +23,19 @@ impl DimSlice { pub fn dim(&self) -> Vec3u { self.dim } + + /// re-borrow the slice with a different lifetime. + pub fn as_ref(&self) -> DimSlice<&R> + where T: AsRef + { + DimSlice::new(self.dim, self.items.as_ref()) + } + /// re-borrow the slice with a different lifetime. + pub fn as_mut(&mut self) -> DimSlice<&mut R> + where T: AsMut + { + DimSlice::new(self.dim, self.items.as_mut()) + } } #[cfg(feature = "iter")] @@ -122,6 +138,13 @@ mod test { assert_eq!(index(Vec3u::new(1, 2, 2), dim), 17); } + #[test] + fn as_ref() { + let data = [1, 2]; + let s = DimSlice::new(Vec3u::new(1, 2, 1), &data[..]); + assert_eq!(s.as_ref(), DimSlice::new(Vec3u::new(1, 2, 1), &data[..])); + } + #[test] fn dim_slice_index() { let data = [ diff --git a/crates/cross/src/dim/offset_dim_slice.rs b/crates/cross/src/dim/offset_dim_slice.rs index 4e15a99..868eee0 100644 --- a/crates/cross/src/dim/offset_dim_slice.rs +++ b/crates/cross/src/dim/offset_dim_slice.rs @@ -1,3 +1,4 @@ +use core::convert::{AsMut, AsRef}; use core::iter::Zip; use core::ops::{Index, IndexMut}; @@ -5,6 +6,8 @@ use crate::dim::{DimIter, DimSlice}; use crate::vec::Vec3u; +#[cfg_attr(feature = "fmt", derive(Debug))] +#[derive(PartialEq)] pub struct OffsetDimSlice { offset: Vec3u, inner: DimSlice, @@ -17,6 +20,19 @@ impl OffsetDimSlice { pub fn indices(&self) -> OffsetDimIter { OffsetDimIter::new(self.offset, self.inner.indices()) } + + /// re-borrow the slice with a different lifetime. + pub fn as_ref(&self) -> OffsetDimSlice<&R> + where T: AsRef + { + OffsetDimSlice { offset: self.offset, inner: self.inner.as_ref()} + } + /// re-borrow the slice with a different lifetime. + pub fn as_mut(&mut self) -> OffsetDimSlice<&mut R> + where T: AsMut + { + OffsetDimSlice { offset: self.offset, inner: self.inner.as_mut()} + } } #[cfg(feature = "iter")]