use crate::{
misc::BufferTooSmall, wrapper::Array,
std::{
fmt::Debug, ops::RangeBounds,
slice::{ Iter as SliceIter, IterMut as SliceIterMut }
}
};
#[cfg(feature = "std")]
use crate::misc::WillPanic;
pub trait ArrayRef<T> {
fn as_slice(&self) -> &[T];
fn len(&self) -> usize;
fn is_empty(&self) -> bool;
fn get(&self, index: usize) -> Option<&T>;
fn get_n<Range>(&self, range: Range) -> Option<Array<&[T]>> where Range: RangeBounds<usize>;
fn first(&self) -> Option<&T>;
fn last(&self) -> Option<&T>;
fn iter(&self) -> SliceIter<T>;
fn clone_to<Target>(&self, target: &mut Target) -> Result<(), BufferTooSmall> where Target: ArrayMut<T>, T: Clone;
}
pub trait ArrayMut<T>: ArrayRef<T> {
fn as_slice_mut(&mut self) -> &mut [T];
fn get_mut(&mut self, index: usize) -> Option<&mut T>;
fn get_n_mut<Range>(&mut self, range: Range) -> Option<Array<&mut [T]>> where Range: RangeBounds<usize>;
fn first_mut(&mut self) -> Option<&mut T>;
fn last_mut(&mut self) -> Option<&mut T>;
fn iter_mut(&mut self) -> SliceIterMut<T>;
fn rotate_left(&mut self, count: usize);
fn rotate_right(&mut self, count: usize);
fn reverse(&mut self);
}
pub trait ArrayAlloc<T>: ArrayMut<T> + Sized {
type Error: Debug;
fn alloc_new() -> Result<Self, Self::Error>;
fn alloc_clone<Source>(source: &Source) -> Result<Self, Self::Error> where Source: ArrayRef<T>, T: Clone;
fn grow_with(&mut self, len: usize, init: impl FnMut() -> T) -> Result<(), Self::Error>;
fn grow(&mut self, len: usize) -> Result<(), Self::Error> where T: Default;
fn shrink(&mut self, len: usize) -> Result<(), Self::Error>;
fn push_front(&mut self, element: T) -> Result<(), Self::Error>;
fn push_n_front<Source>(&mut self, elements: &Source) -> Result<(), Self::Error>
where Source: ArrayRef<T>, T: Clone;
fn push_back(&mut self, element: T) -> Result<(), Self::Error>;
fn push_n_back<Source>(&mut self, elements: &Source) -> Result<(), Self::Error> where Source: ArrayRef<T>, T: Clone;
fn pop_front(&mut self) -> Result<Option<T>, Self::Error>;
fn pop_n_front(&mut self, len: usize) -> Result<Option<Self>, Self::Error>;
fn pop_back(&mut self) -> Result<Option<T>, Self::Error>;
fn pop_n_back(&mut self, len: usize) -> Result<Option<Self>, Self::Error>;
}
pub trait ArrayAllocPanic<T>: ArrayMut<T> + Sized {
fn alloc_new() -> Self;
fn alloc_clone<Source>(source: &Source) -> Self where Source: ArrayRef<T>, T: Clone;
fn grow_with(&mut self, len: usize, init: impl FnMut() -> T);
fn grow(&mut self, len: usize) where T: Default;
fn shrink(&mut self, len: usize);
fn push_front(&mut self, element: T);
fn push_n_front<Source>(&mut self, elements: &Source) where Source: ArrayRef<T>, T: Clone;
fn push_back(&mut self, element: T);
fn push_n_back<Source>(&mut self, elements: &Source) where Source: ArrayRef<T>, T: Clone;
fn pop_front(&mut self) -> Option<T>;
fn pop_n_front(&mut self, len: usize) -> Option<Self>;
fn pop_back(&mut self) -> Option<T>;
fn pop_n_back(&mut self, len: usize) -> Option<Self>;
}
impl<T, Array> ArrayAllocPanic<T> for Array where Array: ArrayAlloc<T> {
fn alloc_new() -> Self {
<Self as ArrayAlloc<T>>::alloc_new().expect("Allocation error")
}
fn alloc_clone<Source>(elements: &Source) -> Self where Source: ArrayRef<T>, T: Clone {
<Self as ArrayAlloc<T>>::alloc_clone(elements).expect("Allocation error")
}
fn grow_with(&mut self, len: usize, init: impl FnMut() -> T) {
<Self as ArrayAlloc<T>>::grow_with(self, len, init).expect("Allocation error")
}
fn grow(&mut self, len: usize) where T: Default {
<Self as ArrayAlloc<T>>::grow(self, len).expect("Allocation error")
}
fn shrink(&mut self, len: usize) {
<Self as ArrayAlloc<T>>::shrink(self, len).expect("Allocation error")
}
fn push_front(&mut self, element: T) {
<Self as ArrayAlloc<T>>::push_front(self, element).expect("Allocation error")
}
fn push_n_front<Source>(&mut self, elements: &Source) where Source: ArrayRef<T>, T: Clone {
<Self as ArrayAlloc<T>>::push_n_front(self, elements).expect("Allocation error")
}
fn push_back(&mut self, element: T) {
<Self as ArrayAlloc<T>>::push_back(self, element).expect("Allocation error")
}
fn push_n_back<Source>(&mut self, elements: &Source) where Source: ArrayRef<T>, T: Clone {
<Self as ArrayAlloc<T>>::push_n_back(self, elements).expect("Allocation error")
}
fn pop_front(&mut self) -> Option<T> {
<Self as ArrayAlloc<T>>::pop_front(self).expect("Allocation error")
}
fn pop_n_front(&mut self, len: usize) -> Option<Self> {
<Self as ArrayAlloc<T>>::pop_n_front(self, len).expect("Allocation error")
}
fn pop_back(&mut self) -> Option<T> {
<Self as ArrayAlloc<T>>::pop_back(self).expect("Allocation error")
}
fn pop_n_back(&mut self, len: usize) -> Option<Self> {
<Self as ArrayAlloc<T>>::pop_n_back(self, len).expect("Allocation error")
}
}
pub trait CanAlloc<T>: Sized {
type Error: Debug;
fn alloc_new() -> Result<Self, Self::Error>;
fn push(&mut self, element: T) -> Result<(), Self::Error>;
fn pop(&mut self) -> Result<Option<T>, Self::Error>;
}
#[cfg(feature = "std")]
impl<T> CanAlloc<T> for Vec<T> {
type Error = WillPanic;
fn alloc_new() -> Result<Self, Self::Error> {
Ok(Self::new())
}
fn push(&mut self, element: T) -> Result<(), Self::Error> {
Ok(self.push(element))
}
fn pop(&mut self) -> Result<Option<T>, Self::Error> {
Ok(self.pop())
}
}