Trait SliceOwned

Source
pub trait SliceOwned: Slice {
    // Required method
    fn get_owned(&self, index: usize) -> Option<Self::Output>;

    // Provided methods
    fn array_chunks<const N: usize>(self) -> ArrayChunksOwned<Self, N> 
       where Self: Sized { ... }
    fn array_windows<const N: usize>(self) -> ArrayWindowsOwned<Self, N> 
       where Self: Sized { ... }
    fn chunks(&self, size: usize) -> ChunksOwned<'_, Self>  { ... }
    fn map<F: Fn(Self::Output) -> R, R>(self, f: F) -> MapOwned<Self, F>
       where Self: Sized { ... }
    fn iter(self) -> IterOwned<Self> 
       where Self: Sized { ... }
    fn try_array<const N: usize>(&self) -> Option<[Self::Output; N]> { ... }
    fn windows(&self, size: usize) -> WindowsOwned<'_, Self>  { ... }
    fn zip<O: SliceOwned>(self, other: O) -> Zip<Self, O>
       where Self: Sized { ... }
}
Expand description

A Slice that can return owned values.

Required Methods§

Source

fn get_owned(&self, index: usize) -> Option<Self::Output>

Index the slice, returning an owned value.

Provided Methods§

Source

fn array_chunks<const N: usize>(self) -> ArrayChunksOwned<Self, N>
where Self: Sized,

Return an iterator over arrays covering consecutive portions of the slice.

Analagous to [slice::array_chunks].

§Panics

If N == 0, panics.

§Examples
let slice = [1, 2, 3, 4, 5];
let mut iter = slice.array_chunks::<2>();

assert_eq!(iter.next(), Some([1, 2]));
assert_eq!(iter.next(), Some([3, 4]));
assert!(iter.next().is_none());
assert_eq!(iter.remainder(), [5]);
Source

fn array_windows<const N: usize>(self) -> ArrayWindowsOwned<Self, N>
where Self: Sized,

Return a slice/iterator over arrays covering overlapping portions of the slice.

Analagous to slice::array_windows.

§Panics

If N == 0, panics.

§Examples
let slice = [1, 2, 3, 4, 5];
let mut windows = slice.array_windows::<3>();

assert_eq!(windows.next(), Some([1, 2, 3]));
assert_eq!(windows.next(), Some([2, 3, 4]));
assert_eq!(windows.next(), Some([3, 4, 5]));
assert!(windows.next().is_none());
assert_eq!(windows.get_owned(1), Some([2, 3, 4]));
Source

fn chunks(&self, size: usize) -> ChunksOwned<'_, Self>

Return an iterator over slices covering consecutive portions of the slice.

Analagous to slice::chunks.

§Panics

If size == 0, panics.

§Examples
let slice = [1, 2, 3, 4, 5];
let mut iter = slice.chunks(2);

assert_eq!(iter.next().unwrap(), [1, 2]);
assert_eq!(iter.next().unwrap(), [3, 4]);
assert_eq!(iter.next().unwrap(), [5]);
assert!(iter.next().is_none());
Source

fn map<F: Fn(Self::Output) -> R, R>(self, f: F) -> MapOwned<Self, F>
where Self: Sized,

Call a closure on index, returning a new type.

Analagous to Iterator::map.

§Examples
let slice = [0, 1, 2].map(|x| x != 0);
assert_eq!(slice, [false, true, true]);
Source

fn iter(self) -> IterOwned<Self>
where Self: Sized,

Creates an iterator over the slice.

§Examples
let slice = [1, 2].chain([3]);
let mut iter = slice.iter();
assert_eq!(iter.next(), Some(1));
assert_eq!(iter.next(), Some(2));
assert_eq!(iter.next(), Some(3));
assert!(iter.next().is_none());
Source

fn try_array<const N: usize>(&self) -> Option<[Self::Output; N]>

Try to collect the slice into an array, failing if the lengths don’t match up.

§Examples
let slice = [1, 2, 3].map(|i| i * i);
let arr: [i32; 3] = slice.try_array().unwrap();
assert_eq!(arr, [1, 4, 9]);
Source

fn windows(&self, size: usize) -> WindowsOwned<'_, Self>

Return a slice/iterator over slices covering overlapping portions of the slice.

Analagous to slice::windows.

§Panics

If size == 0, panics.

§Examples
let slice = [1, 2, 3, 4, 5];
let mut windows = slice.windows(3);

assert_eq!(windows.next().unwrap(), [1, 2, 3]);
assert_eq!(windows.next().unwrap(), [2, 3, 4]);
assert_eq!(windows.next().unwrap(), [3, 4, 5]);
assert!(windows.next().is_none());
assert_eq!(windows.get_owned(1).unwrap(), [2, 3, 4]);
Source

fn zip<O: SliceOwned>(self, other: O) -> Zip<Self, O>
where Self: Sized,

Zip two slices into a single slice, where indexing returns a tuple of their items.

Analagous to Iterator::zip.

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

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

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

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

Source§

fn get_owned(&self, index: usize) -> Option<Self::Output>

Source§

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

Source§

fn get_owned(&self, index: usize) -> Option<Self::Output>

Source§

impl<T> SliceOwned for [T]
where T: Copy,

Source§

fn get_owned(&self, index: usize) -> Option<Self::Output>

Source§

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

Source§

fn get_owned(&self, index: usize) -> Option<T>

Source§

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

Source§

fn get_owned(&self, index: usize) -> Option<T>

Source§

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

Source§

fn get_owned(&self, index: usize) -> Option<T>

Source§

impl<T, const N: usize> SliceOwned for [T; N]
where T: Copy,

Source§

fn get_owned(&self, index: usize) -> Option<Self::Output>

Implementors§

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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