Trait slice_utils::Slice

source ·
pub trait Slice {
    type Output;

    // Required methods
    fn len(&self) -> usize;
    fn get_with<W: FnMut(&Self::Output) -> R, R>(
        &self,
        index: usize,
        f: &mut W,
    ) -> Option<R>;

    // Provided methods
    fn is_empty(&self) -> bool { ... }
    fn chain<S: Slice<Output = Self::Output>>(self, other: S) -> Chain<Self, S>
       where Self: Sized { ... }
    fn cycle(self) -> Cycle<Self>
       where Self: Sized { ... }
    fn interleave<S: Slice<Output = Self::Output>>(
        self,
        other: S,
    ) -> Interleave<Self, S>
       where Self: Sized { ... }
    fn rev(self) -> Reverse<Self>
       where Self: Sized { ... }
    fn slice<R: RangeBounds<usize>>(self, range: R) -> Option<SliceOf<Self>>
       where Self: Sized { ... }
    fn split(&self, at: usize) -> Option<(SliceOf<&Self>, SliceOf<&Self>)> { ... }
}
Expand description

The base trait for SliceOwned, SliceBorrowed, and SliceMut.

Required Associated Types§

source

type Output

The type this slice returns; analagous to Index::Output.

Required Methods§

source

fn len(&self) -> usize

Returns the length of the slice.

source

fn get_with<W: FnMut(&Self::Output) -> R, R>( &self, index: usize, f: &mut W, ) -> Option<R>

Call a closure with the indicated element, returning the result or None if the index was out-of-bounds.

This allows operations that are independent of indexing method.

§Examples
let a = [1_i32, 2, 3];

// Doesn't care about ownership
fn stringify_first<S>(slice: &S) -> String
where
    S: Slice,
    S::Output: ToString,
{
    slice.get_with(0, &mut |x| x.to_string()).unwrap_or_default()
}

assert_eq!(stringify_first(&a), "1");

Provided Methods§

source

fn is_empty(&self) -> bool

Returns whether or not the slice is empty.

Equivalent to slice.len() == 0.

source

fn chain<S: Slice<Output = Self::Output>>(self, other: S) -> Chain<Self, S>
where Self: Sized,

Chains two slices together, back-to-back.

Analagous to Iterator::chain.

§Examples
let a = [1, 2, 3];
let b = [4, 5, 6];

assert_eq!(a.chain(b), [1, 2, 3, 4, 5, 6]);
source

fn cycle(self) -> Cycle<Self>
where Self: Sized,

Cycles the slice infinitely.

Analagous to Iterator::cycle.

§Examples
let slice = [1, 2, 3].cycle();
assert_eq!(slice.get_owned(2), Some(3));
assert_eq!(slice.get_owned(4), Some(2));
assert_eq!(slice.get_owned(6), Some(1));
source

fn interleave<S: Slice<Output = Self::Output>>( self, other: S, ) -> Interleave<Self, S>
where Self: Sized,

Interleaves two slices, e.g. [A, B, A, B, …].

§Examples
let a = [1, 2, 3];
let b = [4, 5, 6];
let c = a.interleave(b);

assert_eq!(c, [1, 4, 2, 5, 3, 6]);
source

fn rev(self) -> Reverse<Self>
where Self: Sized,

Reverses the slice.

Analagous to Iterator::rev.

§Examples
let slice = [1, 2, 3].rev();
assert_eq!(slice, [3, 2, 1]);
source

fn slice<R: RangeBounds<usize>>(self, range: R) -> Option<SliceOf<Self>>
where Self: Sized,

Create a sub-slice of the slice.

Analagous to slicing &[T].

§Examples
let slice = [1, 2, 3, 4, 5].slice(1..4).unwrap();
assert_eq!(slice, [2, 3, 4]);
source

fn split(&self, at: usize) -> Option<(SliceOf<&Self>, SliceOf<&Self>)>

Returns (&self[..at], &self[at..]). Returns None if at is out-of-bounds.

Analagous to slice::split.

§Examples
let slice = [1, 2, 3, 4, 5, 6];
let (a, b) = slice.split(3).unwrap();

assert_eq!(a, [1, 2, 3]);
assert_eq!(b, [4, 5, 6]);

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl<'a, S> Slice for &'a S
where S: Slice + ?Sized,

source§

type Output = <S as Slice>::Output

source§

fn len(&self) -> usize

source§

fn get_with<W: FnMut(&Self::Output) -> R, R>( &self, index: usize, f: &mut W, ) -> Option<R>

source§

impl<'a, S> Slice for &'a mut S
where S: Slice + ?Sized,

source§

type Output = <S as Slice>::Output

source§

fn len(&self) -> usize

source§

fn get_with<W: FnMut(&Self::Output) -> R, R>( &self, index: usize, f: &mut W, ) -> Option<R>

source§

impl<T> Slice for [T]

source§

type Output = T

source§

fn len(&self) -> usize

source§

fn get_with<W: FnMut(&Self::Output) -> R, R>( &self, index: usize, f: &mut W, ) -> Option<R>

source§

impl<T> Slice for Range<T>
where T: Clone, Range<T>: Iterator<Item = T>,

source§

type Output = T

source§

fn len(&self) -> usize

source§

fn get_with<W: FnMut(&Self::Output) -> R, R>( &self, index: usize, f: &mut W, ) -> Option<R>

source§

impl<T> Slice for RangeFrom<T>
where T: Clone, RangeFrom<T>: Iterator<Item = T>,

source§

type Output = T

source§

fn len(&self) -> usize

source§

fn get_with<W: FnMut(&Self::Output) -> R, R>( &self, index: usize, f: &mut W, ) -> Option<R>

source§

impl<T> Slice for RangeInclusive<T>
where T: Clone, RangeInclusive<T>: Iterator<Item = T>,

source§

type Output = T

source§

fn len(&self) -> usize

source§

fn get_with<W: FnMut(&Self::Output) -> R, R>( &self, index: usize, f: &mut W, ) -> Option<R>

source§

impl<T, const N: usize> Slice for [T; N]

source§

type Output = T

source§

fn len(&self) -> usize

source§

fn get_with<W: FnMut(&Self::Output) -> R, R>( &self, index: usize, f: &mut W, ) -> Option<R>

Implementors§

source§

impl<'a, S> Slice for WindowsBorrowed<'a, S>
where S: SliceBorrowed + ?Sized,

source§

impl<'a, S> Slice for WindowsOwned<'a, S>
where S: SliceOwned + ?Sized,

source§

impl<'a, S, const N: usize> Slice for ArrayWindowsBorrowed<'a, S, N>
where S: SliceBorrowed + ?Sized,

source§

type Output = [&'a <S as Slice>::Output; N]

source§

impl<A> Slice for SliceOf<A>
where A: Slice,

source§

type Output = <A as Slice>::Output

source§

impl<A> Slice for SplitMut<'_, A>
where A: Slice + ?Sized,

source§

type Output = <A as Slice>::Output

source§

impl<F, T> Slice for FromFn<F>
where F: Fn(usize) -> Option<T>,

source§

impl<S1, S2> Slice for Chain<S1, S2>
where S1: Slice, S2: Slice<Output = S1::Output>,

source§

type Output = <S1 as Slice>::Output

source§

impl<S1, S2> Slice for Interleave<S1, S2>
where S1: Slice, S2: Slice<Output = S1::Output>,

source§

type Output = <S1 as Slice>::Output

source§

impl<S1, S2> Slice for Zip<S1, S2>
where S1: SliceOwned, S2: SliceOwned,

source§

type Output = (<S1 as Slice>::Output, <S2 as Slice>::Output)

source§

impl<S> Slice for Cycle<S>
where S: Slice,

source§

type Output = <S as Slice>::Output

source§

impl<S> Slice for Reverse<S>
where S: Slice,

source§

type Output = <S as Slice>::Output

source§

impl<S, F, U> Slice for MapBorrowed<S, F>
where S: SliceBorrowed, F: Fn(&S::Output) -> U,

source§

impl<S, F, U> Slice for MapOwned<S, F>
where S: SliceOwned, F: Fn(S::Output) -> U,

source§

impl<S, const N: usize> Slice for ArrayWindowsOwned<S, N>
where S: SliceOwned + Sized,

source§

type Output = [<S as Slice>::Output; N]