Struct stride::Stride[][src]

#[repr(transparent)]pub struct Stride<T, const S: usize> { /* fields omitted */ }

A constant strided slice.

Implementations

impl<T, const S: usize> Stride<T, S>[src]

pub fn new(data: &[T]) -> &Self[src]

Constructs a new strided slice.

Examples

let data = &[1, 2, 3, 4, 5, 6];
let stride = Stride::<_, 3>::new(data);

pub fn new_mut(data: &mut [T]) -> &mut Self[src]

Constructs a new mutable strided slice.

Examples

let data = &mut [1, 2, 3, 4, 5, 6];
let stride = Stride::<_, 3>::new_mut(data);

pub const fn len(&self) -> usize[src]

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

pub const fn is_empty(&self) -> bool[src]

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

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

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

See slice::as_ptr().

pub fn as_mut_ptr(&mut self) -> *mut T[src]

Returns an unsafe mutable pointer to the underlying slice's buffer.

See slice::as_mut_ptr().

pub fn get<I>(&self, index: I) -> Option<&I::Output> where
    I: StrideIndex<Stride<T, S>>, 
[src]

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

pub fn get_mut<I>(&mut self, index: I) -> Option<&mut I::Output> where
    I: StrideIndex<Stride<T, S>>, 
[src]

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

pub unsafe fn get_unchecked<I>(&self, index: I) -> &I::Output where
    I: StrideIndex<Self>, 
[src]

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.

pub unsafe fn get_unchecked_mut<I>(&mut self, index: I) -> &mut I::Output where
    I: StrideIndex<Self>, 
[src]

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.

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

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

pub fn first_mut(&mut self) -> Option<&mut T>[src]

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

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

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

pub fn last_mut(&mut self) -> Option<&mut T>[src]

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

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

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.

pub fn iter(&self) -> Iter<'_, T, S>

Notable traits for Iter<'a, T, S>

impl<'a, T, const S: usize> Iterator for Iter<'a, T, S> type Item = &'a T;
[src]

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

pub fn iter_mut(&mut self) -> IterMut<'_, T, S>

Notable traits for IterMut<'a, T, S>

impl<'a, T, const S: usize> Iterator for IterMut<'a, T, S> type Item = &'a mut T;
[src]

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

impl<T> Stride<T, 1>[src]

pub fn as_slice(&self) -> &[T][src]

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

pub fn as_mut_slice(&mut self) -> &mut [T][src]

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

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

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

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

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

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

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

type Output = I::Output

The returned type after indexing.

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

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

type Item = &'a T

The type of the elements being iterated over.

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

Which kind of iterator are we turning this into?

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

type Item = &'a mut T

The type of the elements being iterated over.

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

Which kind of iterator are we turning this into?

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

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

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

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

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

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

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

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

type Output = T

The output type returned by methods.

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

type Output = Stride<T, S>

The output type returned by methods.

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

type Output = Stride<T, S>

The output type returned by methods.

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

type Output = Stride<T, S>

The output type returned by methods.

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

type Output = Stride<T, S>

The output type returned by methods.

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

type Output = Stride<T, S>

The output type returned by methods.

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

type Output = Stride<T, S>

The output type returned by methods.

Auto Trait Implementations

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

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

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

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]