Struct Stride

Source
pub struct Stride<T, const S: usize> { /* private fields */ }
Expand description

A constant strided slice.

Implementations§

Source§

impl<T, const S: usize> Stride<T, S>

Source

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);
Source

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);
Source

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);
Source

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());
Source

pub const fn as_ptr(&self) -> *const T

Returns a raw pointer to the underlying slice’s buffer.

See slice::as_ptr().

Source

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().

Source

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);
Source

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]));
Source

pub unsafe fn get_unchecked<I>(&self, index: I) -> &I::Output
where 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.

Source

pub unsafe fn get_unchecked_mut<I>(&mut self, index: I) -> &mut I::Output
where 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.

Source

pub fn first(&self) -> Option<&T>

Returns a reference to the first element of the strided slice, or None if it is empty.

Source

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.

Source

pub fn last(&self) -> Option<&T>

Returns a reference to the last element of the strided slice, or None if it is empty.

Source

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.

Source

pub fn swap(&mut self, a: usize, b: usize)

Swaps two elements in the strided slice.

§Arguments
  • a - The index of the first element
  • b - The index of the second element
§Panics

If a or b are out of bounds.

Source

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);
Source

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]);
Source§

impl<T> Stride<T, 1>

Source

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);
Source

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§

Source§

impl<T, const S: usize> Debug for Stride<T, S>
where T: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T, const S: usize> Default for &Stride<T, S>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T, const S: usize> Default for &mut Stride<T, S>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T, const S: usize> Hash for Stride<T, S>
where T: Hash,

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
Source§

impl<I, T, const S: usize> Index<I> for Stride<T, S>
where I: StrideIndex<Self>,

Source§

type Output = <I as StrideIndex<Stride<T, S>>>::Output

The returned type after indexing.
Source§

fn index(&self, index: I) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<I, T, const S: usize> IndexMut<I> for Stride<T, S>
where I: StrideIndex<Self>,

Source§

fn index_mut(&mut self, index: I) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<'a, T, const S: usize> IntoIterator for &'a Stride<T, S>

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T, S>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, T, const S: usize> IntoIterator for &'a mut Stride<T, S>

Source§

type Item = &'a mut T

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, T, S>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> IterMut<'a, T, S>

Creates an iterator from a value. Read more
Source§

impl<T, const S: usize> Ord for Stride<T, S>
where T: Ord,

Source§

fn cmp(&self, other: &Stride<T, S>) -> Ordering

This method returns an Ordering between self and other. Read more
Source§

impl<T, U, const S: usize> PartialEq<&[U]> for Stride<T, S>
where T: PartialEq<U>,

Source§

fn eq(&self, other: &&[U]) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T, U, const S: usize, const N: usize> PartialEq<&[U; N]> for Stride<T, S>
where T: PartialEq<U>,

Source§

fn eq(&self, other: &&[U; N]) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T, U, const S: usize> PartialEq<[U]> for Stride<T, S>
where T: PartialEq<U>,

Source§

fn eq(&self, other: &[U]) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T, U, const S: usize, const N: usize> PartialEq<[U; N]> for Stride<T, S>
where T: PartialEq<U>,

Source§

fn eq(&self, other: &[U; N]) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T, U, const S: usize, const R: usize> PartialEq<Stride<U, R>> for Stride<T, S>
where T: PartialEq<U>,

Source§

fn eq(&self, other: &Stride<U, R>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T, U, const S: usize, const R: usize> PartialOrd<Stride<U, R>> for Stride<T, S>
where T: PartialOrd<U>,

Source§

fn partial_cmp(&self, other: &Stride<U, R>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T, const S: usize> StrideIndex<Stride<T, S>> for Range<usize>

Source§

type Output = Stride<T, S>

The output type returned by methods.
Source§

fn get(self, stride: &Stride<T, S>) -> Option<&Self::Output>

Returns a shared reference to the output at this location, if in bounds.
Source§

fn get_mut(self, stride: &mut Stride<T, S>) -> Option<&mut Self::Output>

Returns a mutable reference to the output at this location, if in bounds.
Source§

unsafe fn get_unchecked( self, stride: *const Stride<T, S>, ) -> *const Self::Output

Returns a shared reference to the output at this location, without performing any bounds checking. Read more
Source§

unsafe fn get_unchecked_mut( self, stride: *mut Stride<T, S>, ) -> *mut Self::Output

Returns a mutable reference to the output at this location, without performing any bounds checking. Read more
Source§

fn index(self, stride: &Stride<T, S>) -> &Self::Output

Returns a shared reference to the output at this location, panicking if out of bounds.
Source§

fn index_mut(self, stride: &mut Stride<T, S>) -> &mut Self::Output

Returns a mutable reference to the output at this location, panicking if out of bounds.
Source§

impl<T, const S: usize> StrideIndex<Stride<T, S>> for RangeFrom<usize>

Source§

type Output = Stride<T, S>

The output type returned by methods.
Source§

fn get(self, stride: &Stride<T, S>) -> Option<&Self::Output>

Returns a shared reference to the output at this location, if in bounds.
Source§

fn get_mut(self, stride: &mut Stride<T, S>) -> Option<&mut Self::Output>

Returns a mutable reference to the output at this location, if in bounds.
Source§

unsafe fn get_unchecked( self, stride: *const Stride<T, S>, ) -> *const Self::Output

Returns a shared reference to the output at this location, without performing any bounds checking. Read more
Source§

unsafe fn get_unchecked_mut( self, stride: *mut Stride<T, S>, ) -> *mut Self::Output

Returns a mutable reference to the output at this location, without performing any bounds checking. Read more
Source§

fn index(self, stride: &Stride<T, S>) -> &Self::Output

Returns a shared reference to the output at this location, panicking if out of bounds.
Source§

fn index_mut(self, stride: &mut Stride<T, S>) -> &mut Self::Output

Returns a mutable reference to the output at this location, panicking if out of bounds.
Source§

impl<T, const S: usize> StrideIndex<Stride<T, S>> for RangeFull

Source§

type Output = Stride<T, S>

The output type returned by methods.
Source§

fn get(self, stride: &Stride<T, S>) -> Option<&Self::Output>

Returns a shared reference to the output at this location, if in bounds.
Source§

fn get_mut(self, stride: &mut Stride<T, S>) -> Option<&mut Self::Output>

Returns a mutable reference to the output at this location, if in bounds.
Source§

unsafe fn get_unchecked( self, stride: *const Stride<T, S>, ) -> *const Self::Output

Returns a shared reference to the output at this location, without performing any bounds checking. Read more
Source§

unsafe fn get_unchecked_mut( self, stride: *mut Stride<T, S>, ) -> *mut Self::Output

Returns a mutable reference to the output at this location, without performing any bounds checking. Read more
Source§

fn index(self, stride: &Stride<T, S>) -> &Self::Output

Returns a shared reference to the output at this location, panicking if out of bounds.
Source§

fn index_mut(self, stride: &mut Stride<T, S>) -> &mut Self::Output

Returns a mutable reference to the output at this location, panicking if out of bounds.
Source§

impl<T, const S: usize> StrideIndex<Stride<T, S>> for RangeInclusive<usize>

Source§

type Output = Stride<T, S>

The output type returned by methods.
Source§

fn get(self, stride: &Stride<T, S>) -> Option<&Self::Output>

Returns a shared reference to the output at this location, if in bounds.
Source§

fn get_mut(self, stride: &mut Stride<T, S>) -> Option<&mut Self::Output>

Returns a mutable reference to the output at this location, if in bounds.
Source§

unsafe fn get_unchecked( self, stride: *const Stride<T, S>, ) -> *const Self::Output

Returns a shared reference to the output at this location, without performing any bounds checking. Read more
Source§

unsafe fn get_unchecked_mut( self, stride: *mut Stride<T, S>, ) -> *mut Self::Output

Returns a mutable reference to the output at this location, without performing any bounds checking. Read more
Source§

fn index(self, stride: &Stride<T, S>) -> &Self::Output

Returns a shared reference to the output at this location, panicking if out of bounds.
Source§

fn index_mut(self, stride: &mut Stride<T, S>) -> &mut Self::Output

Returns a mutable reference to the output at this location, panicking if out of bounds.
Source§

impl<T, const S: usize> StrideIndex<Stride<T, S>> for RangeTo<usize>

Source§

type Output = Stride<T, S>

The output type returned by methods.
Source§

fn get(self, stride: &Stride<T, S>) -> Option<&Self::Output>

Returns a shared reference to the output at this location, if in bounds.
Source§

fn get_mut(self, stride: &mut Stride<T, S>) -> Option<&mut Self::Output>

Returns a mutable reference to the output at this location, if in bounds.
Source§

unsafe fn get_unchecked( self, stride: *const Stride<T, S>, ) -> *const Self::Output

Returns a shared reference to the output at this location, without performing any bounds checking. Read more
Source§

unsafe fn get_unchecked_mut( self, stride: *mut Stride<T, S>, ) -> *mut Self::Output

Returns a mutable reference to the output at this location, without performing any bounds checking. Read more
Source§

fn index(self, stride: &Stride<T, S>) -> &Self::Output

Returns a shared reference to the output at this location, panicking if out of bounds.
Source§

fn index_mut(self, stride: &mut Stride<T, S>) -> &mut Self::Output

Returns a mutable reference to the output at this location, panicking if out of bounds.
Source§

impl<T, const S: usize> StrideIndex<Stride<T, S>> for RangeToInclusive<usize>

Source§

type Output = Stride<T, S>

The output type returned by methods.
Source§

fn get(self, stride: &Stride<T, S>) -> Option<&Self::Output>

Returns a shared reference to the output at this location, if in bounds.
Source§

fn get_mut(self, stride: &mut Stride<T, S>) -> Option<&mut Self::Output>

Returns a mutable reference to the output at this location, if in bounds.
Source§

unsafe fn get_unchecked( self, stride: *const Stride<T, S>, ) -> *const Self::Output

Returns a shared reference to the output at this location, without performing any bounds checking. Read more
Source§

unsafe fn get_unchecked_mut( self, stride: *mut Stride<T, S>, ) -> *mut Self::Output

Returns a mutable reference to the output at this location, without performing any bounds checking. Read more
Source§

fn index(self, stride: &Stride<T, S>) -> &Self::Output

Returns a shared reference to the output at this location, panicking if out of bounds.
Source§

fn index_mut(self, stride: &mut Stride<T, S>) -> &mut Self::Output

Returns a mutable reference to the output at this location, panicking if out of bounds.
Source§

impl<T, const S: usize> StrideIndex<Stride<T, S>> for usize

Source§

type Output = T

The output type returned by methods.
Source§

fn get(self, stride: &Stride<T, S>) -> Option<&Self::Output>

Returns a shared reference to the output at this location, if in bounds.
Source§

fn get_mut(self, stride: &mut Stride<T, S>) -> Option<&mut Self::Output>

Returns a mutable reference to the output at this location, if in bounds.
Source§

unsafe fn get_unchecked( self, stride: *const Stride<T, S>, ) -> *const Self::Output

Returns a shared reference to the output at this location, without performing any bounds checking. Read more
Source§

unsafe fn get_unchecked_mut( self, stride: *mut Stride<T, S>, ) -> *mut Self::Output

Returns a mutable reference to the output at this location, without performing any bounds checking. Read more
Source§

fn index(self, stride: &Stride<T, S>) -> &Self::Output

Returns a shared reference to the output at this location, panicking if out of bounds.
Source§

fn index_mut(self, stride: &mut Stride<T, S>) -> &mut Self::Output

Returns a mutable reference to the output at this location, panicking if out of bounds.
Source§

impl<T, const S: usize> Eq for Stride<T, S>
where T: Eq,

Auto Trait Implementations§

§

impl<T, const S: usize> Freeze for Stride<T, S>
where T: Freeze,

§

impl<T, const S: usize> RefUnwindSafe for Stride<T, S>
where T: RefUnwindSafe,

§

impl<T, const S: usize> Send for Stride<T, S>
where T: Send,

§

impl<T, const S: usize> !Sized for Stride<T, S>

§

impl<T, const S: usize> Sync for Stride<T, S>
where T: Sync,

§

impl<T, const S: usize> Unpin for Stride<T, S>
where T: Unpin,

§

impl<T, const S: usize> UnwindSafe for Stride<T, S>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more