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§
Required Methods§
Sourcefn copied<A>(&self) -> Result<A, SizeError>
fn copied<A>(&self) -> Result<A, SizeError>
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()));Sourcefn cloned<A>(&self) -> Result<A, SizeError>
fn cloned<A>(&self) -> Result<A, SizeError>
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()));Sourcefn array_windows<A>(&self) -> ArrayWindows<'_, A> ⓘ
fn array_windows<A>(&self) -> ArrayWindows<'_, A> ⓘ
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.