Trait Slice

Source
pub trait Slice {
    type Item;

    // Required methods
    fn copied<A>(&self) -> Result<A, SizeError>
       where A: Array<Item = Self::Item>,
             A::Item: Copy;
    fn cloned<A>(&self) -> Result<A, SizeError>
       where A: Array<Item = Self::Item>,
             A::Item: Clone;
    fn array_windows<A>(&self) -> ArrayWindows<'_, A> 
       where A: Array<Item = Self::Item>;
}
Expand description

Extension for slice

Required Associated Types§

Source

type Item

Item of the slice, i.e.

[T]: Slice<Item = T>

Required Methods§

Source

fn copied<A>(&self) -> Result<A, SizeError>
where A: Array<Item = Self::Item>, A::Item: Copy,

Copy self into an owned array. Return Err(SizeError) if len of self is not equal to A::SIZE.

§Examples
use arraylib::Slice;

let slice: &[i32] = &[0, 1, 2, 3, 4];
let array: [i32; 5] = slice.copied().unwrap();
assert_eq!(array, [0, 1, 2, 3, 4]);
use arraylib::{SizeError, Slice};

let slice: &[i32] = &[0, 1, 2, 3, 4];
let result = slice.copied::<[i32; 2]>();
assert_eq!(result, Err(SizeError::default()));
Source

fn cloned<A>(&self) -> Result<A, SizeError>
where A: Array<Item = Self::Item>, A::Item: Clone,

Clone self into an owned array. Return Err(SizeError) if len of self is not equal to A::SIZE.

§Examples
use arraylib::Slice;
use core::ops::Range;

// Range is not `Copy`
let slice: &[Range<usize>] = &[0..1, 1..3, 2..10];
let array: [Range<usize>; 3] = slice.cloned().unwrap();
assert_eq!(array, [0..1, 1..3, 2..10]);
use arraylib::{SizeError, Slice};
use core::ops::Range;

let slice: &[Range<usize>] = &[0..1, 1..3, 2..10];
let result = slice.cloned::<[Range<usize>; 5]>();
assert_eq!(result, Err(SizeError::default()));
Source

fn array_windows<A>(&self) -> ArrayWindows<'_, A>
where A: Array<Item = Self::Item>,

Returns an iterator over all contiguous windows of type A (length A::SIZE). The windows overlap. If the slice is shorter than size (A::SIZE), the iterator returns None.

§Panics

Panics if A::SIZE is 0 (A = [T; 0]).

§Examples
use arraylib::Slice;

let mut iter = [1, 2, 3, 4].array_windows::<[_; 2]>();
assert_eq!(iter.next(), Some(&[1, 2]));
assert_eq!(iter.next(), Some(&[2, 3]));
assert_eq!(iter.next(), Some(&[3, 4]));
assert_eq!(iter.next(), None);

In difference with [<[T]>::windows], this method returns iterator that returns arrays, so you can use array destruction:

[<[T]>::windows]: https://doc.rust-lang.org/std/primitive.slice.html#method.windows

use arraylib::Slice;

assert_eq!(
    [1, 2, 3, 4, 5]
        .array_windows::<[u32; 3]>()
        .map(|[a, b, c]| a + b + c)
        .sum::<u32>(),
    27
)

If the slice is shorter than size:

use arraylib::Slice;

let slice = ['f', 'o', 'o'];
let mut iter = slice.array_windows::<[_; 4]>();
assert!(iter.next().is_none());

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<T> Slice for [T]

Source§

type Item = T

Source§

fn copied<A>(&self) -> Result<A, SizeError>
where A: Array<Item = Self::Item>, A::Item: Copy,

Source§

fn cloned<A>(&self) -> Result<A, SizeError>
where A: Array<Item = Self::Item>, A::Item: Clone,

Source§

fn array_windows<A>(&self) -> ArrayWindows<'_, A>
where A: Array<Item = Self::Item>,

Implementors§