#![cfg_attr(not(any(test, feature = "std")), no_std)]
#![doc = include_str!("../README.md")]
#![warn(missing_docs)]
mod chain;
mod chunks;
mod cycle;
mod debug;
mod eq;
mod fromfn;
mod impls;
mod index;
mod interleave;
mod iter;
mod map;
mod reverse;
mod slicing;
mod windows;
mod zip;
#[cfg(test)]
mod test;
use core::ops::RangeBounds;
pub use chain::Chain;
pub use chunks::{ArrayChunksBorrowed, ArrayChunksOwned, ChunksBorrowed, ChunksOwned};
pub use cycle::Cycle;
pub use fromfn::FromFn;
pub use interleave::Interleave;
pub use iter::{IterBorrowed, IterOwned};
pub use map::{MapBorrowed, MapOwned};
pub use reverse::Reverse;
pub use slicing::{SliceOf, SplitMut};
pub use windows::{ArrayWindowsBorrowed, ArrayWindowsOwned, WindowsBorrowed, WindowsOwned};
pub use zip::Zip;
pub type Cloned<S> = MapBorrowed<S, for<'a> fn(&<S as Slice>::Output) -> <S as Slice>::Output>;
pub trait Slice {
type Output;
fn len(&self) -> usize;
fn is_empty(&self) -> bool {
self.len() == 0
}
fn get_with<W: FnMut(&Self::Output) -> R, R>(&self, index: usize, f: &mut W) -> Option<R>;
fn chain<S: Slice<Output = Self::Output>>(self, other: S) -> Chain<Self, S>
where
Self: Sized,
{
Chain(self, other)
}
fn cycle(self) -> Cycle<Self>
where
Self: Sized,
{
Cycle(self)
}
fn interleave<S: Slice<Output = Self::Output>>(self, other: S) -> Interleave<Self, S>
where
Self: Sized,
{
Interleave(self, other)
}
fn rev(self) -> Reverse<Self>
where
Self: Sized,
{
Reverse(self)
}
fn slice<R: RangeBounds<usize>>(self, range: R) -> Option<SliceOf<Self>>
where
Self: Sized,
{
SliceOf::new(self, range)
}
fn split(&self, at: usize) -> Option<(SliceOf<&Self>, SliceOf<&Self>)> {
Some((SliceOf::new(self, ..at)?, SliceOf::new(self, at..)?))
}
}
pub trait SliceBorrowed: Slice {
fn get(&self, index: usize) -> Option<&Self::Output>;
fn array_chunks<const N: usize>(&self) -> ArrayChunksBorrowed<Self, N> {
ArrayChunksBorrowed::new(self)
}
fn array_windows<const N: usize>(&self) -> ArrayWindowsBorrowed<Self, N> {
ArrayWindowsBorrowed::new(self)
}
fn chunks(&self, size: usize) -> ChunksBorrowed<Self> {
ChunksBorrowed::new(self, size)
}
fn map<F: Fn(&Self::Output) -> R, R>(self, f: F) -> MapBorrowed<Self, F>
where
Self: Sized,
{
MapBorrowed(self, f)
}
fn cloned(self) -> Cloned<Self>
where
Self: Sized,
Self::Output: Clone,
{
MapBorrowed(self, Clone::clone)
}
fn iter(&self) -> IterBorrowed<Self> {
IterBorrowed::new(self)
}
fn windows(&self, size: usize) -> WindowsBorrowed<Self> {
WindowsBorrowed::new(self, size)
}
}
pub trait SliceMut: Slice {
fn get_mut(&mut self, index: usize) -> Option<&mut Self::Output>;
fn split_mut(&mut self, at: usize) -> Option<(SplitMut<Self>, SplitMut<Self>)>
where
Self: Unique,
{
SplitMut::new(self, at)
}
fn copy_from_slice<S>(&mut self, src: &S)
where
S: Slice<Output = Self::Output>,
S::Output: Clone,
{
if self.len() != src.len() {
panic!("source slice length ({}) does not match destination slice length ({})",
src.len(), self.len())
}
for i in 0..src.len() {
src.get_with(i, &mut |item| *self.get_mut(i).unwrap_or_else(|| {
panic!("destination slice is shorter than source slice");
}) = item.clone());
}
}
}
pub trait SliceOwned: Slice {
fn get_owned(&self, index: usize) -> Option<Self::Output>;
fn array_chunks<const N: usize>(self) -> ArrayChunksOwned<Self, N>
where
Self: Sized,
{
ArrayChunksOwned::new(self)
}
fn array_windows<const N: usize>(self) -> ArrayWindowsOwned<Self, N>
where
Self: Sized,
{
ArrayWindowsOwned::new(self)
}
fn chunks(&self, size: usize) -> ChunksOwned<Self> {
ChunksOwned::new(self, size)
}
fn map<F: Fn(Self::Output) -> R, R>(self, f: F) -> MapOwned<Self, F>
where
Self: Sized,
{
MapOwned(self, f)
}
fn iter(self) -> IterOwned<Self>
where
Self: Sized,
{
IterOwned::new(self)
}
fn try_array<const N: usize>(&self) -> Option<[Self::Output; N]> {
if self.len() != N {
None
} else {
Some(core::array::from_fn(|i| self.get_owned(i).unwrap()))
}
}
fn windows(&self, size: usize) -> WindowsOwned<Self> {
WindowsOwned::new(self, size)
}
fn zip<O: SliceOwned>(self, other: O) -> Zip<Self, O>
where
Self: Sized,
{
Zip(self, other)
}
#[cfg(feature = "std")]
fn collect(&self) -> Vec<Self::Output> {
let mut v = Vec::with_capacity(self.len());
for i in 0..self.len() {
v.push(self.get_owned(i).unwrap());
}
v
}
}
pub trait ContiguousBorrowed: SliceBorrowed + Unique {
fn contiguous(&self) -> &[Self::Output];
}
pub trait ContiguousMut: SliceMut + Unique {
fn contiguous_mut(&mut self) -> &mut [Self::Output];
}
pub unsafe trait Unique {}
pub fn from_fn<F, T>(f: F, len: Option<usize>) -> FromFn<F>
where
F: Fn(usize) -> Option<T>,
{
FromFn::new(f, len)
}