pub struct Slice2<'a, T>(/* private fields */);Expand description
An immutable rectangular view to a region of a Buf2, another Slice2,
or in general any &[T] slice of memory. A two-dimensional analog to &[T].
A Slice2 may be non-contiguous:
+------stride-----+
| ____w____ |
| |r0_______| |
| |r1_______| h |
| |r2_______| |
+-----------------+TODO More documentation
Implementations§
Source§impl<'a, T> Slice2<'a, T>
impl<'a, T> Slice2<'a, T>
Sourcepub fn new(dims: Dims, stride: u32, data: &'a [T]) -> Self
pub fn new(dims: Dims, stride: u32, data: &'a [T]) -> Self
Returns a new Slice2 view to data with the given dimensions
and stride.
§Examples
let data = &[0, 1, 2, 3, 4, 5, 6];
let slice = Slice2::new((2, 2), 3, data);
assert_eq!(&slice[0], &[0, 1]);
assert_eq!(&slice[1], &[3, 4]);Above, slice represents a 2×2 rectangle with stride 3, such that
the first row maps to data[0..2] and the second to data[3..5]:
slice[0] slice[1]
| |
,---´---. ,---´---.
+---+---+---+---+---+---+---+
| 0 | 1 | 2 | 3 | 4 | 5 | 6 |
+---+---+---+---+---+---+---+Internally, this is implemented as the borrow &data[0..5].
Semantically, however, slice does not contain data[2] as
an element, and attempting to access it eg. with slice[0][2]
will panic, as expected.
§Panics
if stride < width or if the slice would overflow data.
Methods from Deref<Target = Inner<T, &'a [T]>>§
Sourcepub fn is_contiguous(&self) -> bool
pub fn is_contiguous(&self) -> bool
Returns whether the rows of self are stored as one contiguous
slice, without gaps between rows.
Buf2 instances are always contiguous. A Slice2 or MutSlice2
instance is contiguous if its width equals its stride, if its
height is 1, or if it is empty.
Sourcepub fn slice(&self, rect: impl Into<Rect>) -> Slice2<'_, T>
pub fn slice(&self, rect: impl Into<Rect>) -> Slice2<'_, T>
Returns a borrowed rectangular slice of self.
§Panics
If any part of rect is outside the bounds of self.
Sourcepub fn get(&self, pos: impl Into<Point2u>) -> Option<&T>
pub fn get(&self, pos: impl Into<Point2u>) -> Option<&T>
Returns a reference to the element at pos,
or None if pos is out of bounds.
Sourcepub fn rows(&self) -> impl Iterator<Item = &[T]>
pub fn rows(&self) -> impl Iterator<Item = &[T]>
Returns an iterator over the rows of self as &[T] slices.
The length of each slice equals self.width().