#[repr(transparent)]pub struct Stride<T, const S: usize> { /* private fields */ }
Expand description
A constant strided slice.
Implementations
sourceimpl<T, const S: usize> Stride<T, S>
impl<T, const S: usize> Stride<T, S>
sourcepub fn new(data: &[T]) -> &Self
pub fn new(data: &[T]) -> &Self
Constructs a new strided slice.
Examples
let data = &[1, 2, 3, 4, 5, 6];
let stride = Stride::<_, 3>::new(data);
sourcepub fn new_mut(data: &mut [T]) -> &mut Self
pub fn new_mut(data: &mut [T]) -> &mut Self
Constructs a new mutable strided slice.
Examples
let data = &mut [1, 2, 3, 4, 5, 6];
let stride = Stride::<_, 3>::new_mut(data);
sourcepub const fn len(&self) -> usize
pub const fn len(&self) -> usize
Returns the number of elements in the strided slice.
This is equivalent to the ceiling division of the underlying slice
length by S
.
Examples
let data = &[1, 2, 3, 4, 5, 6];
assert_eq!(Stride::<_, 1>::new(data).len(), 6);
assert_eq!(Stride::<_, 2>::new(data).len(), 3);
assert_eq!(Stride::<_, 3>::new(data).len(), 2);
sourcepub const fn is_empty(&self) -> bool
pub const fn is_empty(&self) -> bool
Returns true
if the strided slice has a length of 0.
Examples
let stride = Stride::<_, 3>::new(&[1, 2, 3, 4, 5, 6]);
assert!(!stride.is_empty());
sourcepub const fn as_ptr(&self) -> *const T
pub const fn as_ptr(&self) -> *const T
Returns a raw pointer to the underlying slice’s buffer.
See slice::as_ptr()
.
sourcepub fn as_mut_ptr(&mut self) -> *mut T
pub fn as_mut_ptr(&mut self) -> *mut T
Returns an unsafe mutable pointer to the underlying slice’s buffer.
See slice::as_mut_ptr()
.
sourcepub fn get<I>(&self, index: I) -> Option<&I::Output>where
I: StrideIndex<Stride<T, S>>,
pub fn get<I>(&self, index: I) -> Option<&I::Output>where
I: StrideIndex<Stride<T, S>>,
Returns a reference to an element or substride depending on the type of index.
- If given a position, returns a reference to the element at that
position or
None
if out of bounds. - If given a range, returns the substride corresponding to that range,
or
None
if out of bounds.
Examples
let stride = Stride::<_, 2>::new(&[1, 2, 3, 4, 5, 6]);
assert_eq!(stride.get(1), Some(&3));
assert_eq!(stride.get(0..2), Some(Stride::<_, 2>::new(&[1, 2, 3, 4])));
assert_eq!(stride.get(3), None);
assert_eq!(stride.get(0..4), None);
sourcepub fn get_mut<I>(&mut self, index: I) -> Option<&mut I::Output>where
I: StrideIndex<Stride<T, S>>,
pub fn get_mut<I>(&mut self, index: I) -> Option<&mut I::Output>where
I: StrideIndex<Stride<T, S>>,
Returns a mutable reference to an element or substride depending on the
type of index (see get
) or None
if the index is out of bounds.
Examples
let data = &mut [0, 1, 2, 3];
let stride = Stride::<_, 2>::new_mut(data);
if let Some(elem) = stride.get_mut(1) {
*elem = 42;
}
assert_eq!(stride, Stride::<_, 2>::new(&[0, 1, 42, 3]));
sourcepub unsafe fn get_unchecked<I>(&self, index: I) -> &I::Outputwhere
I: StrideIndex<Self>,
pub unsafe fn get_unchecked<I>(&self, index: I) -> &I::Outputwhere
I: StrideIndex<Self>,
Returns a reference to an element or substride, without doing bounds checking.
For a safe alternative see get
.
Safety
Calling this method with an out-of-bounds index is undefined behavior even if the resulting reference is not used.
sourcepub unsafe fn get_unchecked_mut<I>(&mut self, index: I) -> &mut I::Outputwhere
I: StrideIndex<Self>,
pub unsafe fn get_unchecked_mut<I>(&mut self, index: I) -> &mut I::Outputwhere
I: StrideIndex<Self>,
Returns a mutable reference to an element or substride, without doing bounds checking.
For a safe alternative see get_mut
.
Safety
Calling this method with an out-of-bounds index is undefined behavior even if the resulting reference is not used.
sourcepub fn first(&self) -> Option<&T>
pub fn first(&self) -> Option<&T>
Returns a reference to the first element of the strided slice, or None
if it is empty.
sourcepub fn first_mut(&mut self) -> Option<&mut T>
pub fn first_mut(&mut self) -> Option<&mut T>
Returns a mutable reference to the first element of the strided slice,
or None
if it is empty.
sourcepub fn last(&self) -> Option<&T>
pub fn last(&self) -> Option<&T>
Returns a reference to the last element of the strided slice, or None
if it is empty.
sourcepub fn last_mut(&mut self) -> Option<&mut T>
pub fn last_mut(&mut self) -> Option<&mut T>
Returns a mutable reference to the last element of the strided slice, or
None
if it is empty.
sourcepub fn iter(&self) -> Iter<'_, T, S>ⓘ
pub fn iter(&self) -> Iter<'_, T, S>ⓘ
Returns an iterator over the stride.
Examples
let stride = Stride::<_, 2>::new(&[1, 2, 3, 4, 5, 6]);
let mut iterator = stride.iter();
assert_eq!(iterator.next(), Some(&1));
assert_eq!(iterator.next(), Some(&3));
assert_eq!(iterator.next(), Some(&5));
assert_eq!(iterator.next(), None);
sourcepub fn iter_mut(&mut self) -> IterMut<'_, T, S>ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, T, S>ⓘ
Returns an iterator over the stride that allows modifying each value.
Examples
let slice = &mut [1, 1, 2, 2, 3, 3];
let stride = Stride::<_, 2>::new_mut(slice);
for elem in stride.iter_mut() {
*elem *= 2;
}
assert_eq!(slice, &[2, 1, 4, 2, 6, 3]);
sourceimpl<T> Stride<T, 1>
impl<T> Stride<T, 1>
sourcepub fn as_slice(&self) -> &[T]
pub fn as_slice(&self) -> &[T]
Returns a slice containing the entire strided slice.
Only available on strided slices with a stride of 1
.
Examples
let slice = &[1, 2, 3];
let stride = Stride::<_, 1>::new(slice);
assert_eq!(stride.as_slice(), slice);
sourcepub fn as_mut_slice(&mut self) -> &mut [T]
pub fn as_mut_slice(&mut self) -> &mut [T]
Returns a mutable slice containing the entire strided slice.
Only available on strided slices with a stride of 1
.
Examples
let slice = &mut [1, 2, 7];
let stride = Stride::<_, 1>::new_mut(slice);
stride.as_mut_slice()[2] = 3;
assert_eq!(slice, &[1, 2, 3])
Trait Implementations
sourceimpl<I, T, const S: usize> Index<I> for Stride<T, S>where
I: StrideIndex<Self>,
impl<I, T, const S: usize> Index<I> for Stride<T, S>where
I: StrideIndex<Self>,
sourceimpl<I, T, const S: usize> IndexMut<I> for Stride<T, S>where
I: StrideIndex<Self>,
impl<I, T, const S: usize> IndexMut<I> for Stride<T, S>where
I: StrideIndex<Self>,
sourceimpl<'a, T, const S: usize> IntoIterator for &'a Stride<T, S>
impl<'a, T, const S: usize> IntoIterator for &'a Stride<T, S>
sourceimpl<'a, T, const S: usize> IntoIterator for &'a mut Stride<T, S>
impl<'a, T, const S: usize> IntoIterator for &'a mut Stride<T, S>
sourceimpl<T, U, const S: usize, const N: usize> PartialEq<&[U; N]> for Stride<T, S>where
T: PartialEq<U>,
impl<T, U, const S: usize, const N: usize> PartialEq<&[U; N]> for Stride<T, S>where
T: PartialEq<U>,
sourceimpl<T, U, const S: usize> PartialEq<&[U]> for Stride<T, S>where
T: PartialEq<U>,
impl<T, U, const S: usize> PartialEq<&[U]> for Stride<T, S>where
T: PartialEq<U>,
sourceimpl<T, U, const S: usize, const N: usize> PartialEq<[U; N]> for Stride<T, S>where
T: PartialEq<U>,
impl<T, U, const S: usize, const N: usize> PartialEq<[U; N]> for Stride<T, S>where
T: PartialEq<U>,
sourceimpl<T, U, const S: usize> PartialEq<[U]> for Stride<T, S>where
T: PartialEq<U>,
impl<T, U, const S: usize> PartialEq<[U]> for Stride<T, S>where
T: PartialEq<U>,
sourceimpl<T, U, const S: usize, const R: usize> PartialEq<Stride<U, R>> for Stride<T, S>where
T: PartialEq<U>,
impl<T, U, const S: usize, const R: usize> PartialEq<Stride<U, R>> for Stride<T, S>where
T: PartialEq<U>,
sourceimpl<T, U, const S: usize, const R: usize> PartialOrd<Stride<U, R>> for Stride<T, S>where
T: PartialOrd<U>,
impl<T, U, const S: usize, const R: usize> PartialOrd<Stride<U, R>> for Stride<T, S>where
T: PartialOrd<U>,
sourcefn partial_cmp(&self, other: &Stride<U, R>) -> Option<Ordering>
fn partial_cmp(&self, other: &Stride<U, R>) -> Option<Ordering>
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more