use std::slice::SliceIndex;
pub trait Indexed<T> {
type Element: ?Sized;
#[must_use]
fn get(&self, i: T) -> Option<&Self::Element>;
}
pub trait Array {
type Element;
#[must_use]
fn iter<'i>(&'i self) -> Box<dyn Iterator<Item = &Self::Element> + 'i>;
#[must_use]
fn len(&self) -> usize;
#[must_use]
fn is_empty(&self) -> bool {
self.len() == 0
}
}
pub trait IndexedMut<T> {
type Element: ?Sized;
#[must_use]
fn get_mut(&mut self, i: T) -> Option<&mut Self::Element>;
}
pub trait ArrayMut {
type Element;
#[must_use]
fn pop(&mut self) -> Option<Self::Element>;
fn push(&mut self, e: Self::Element);
}
impl<T, I> Indexed<I> for Vec<T>
where
I: SliceIndex<[T]>,
{
type Element = <I as SliceIndex<[T]>>::Output;
#[inline]
fn get(&self, i: I) -> Option<&Self::Element> {
<[T]>::get(self, i)
}
}
impl<T> Array for Vec<T> {
type Element = T;
fn iter<'i>(&'i self) -> Box<dyn Iterator<Item = &T> + 'i> {
Box::new(<[T]>::iter(self))
}
#[inline]
fn len(&self) -> usize {
self.len()
}
}
impl<T, I> IndexedMut<I> for Vec<T>
where
I: SliceIndex<[T]>,
{
type Element = <I as SliceIndex<[T]>>::Output;
#[inline]
fn get_mut(&mut self, i: I) -> Option<&mut Self::Element> {
<[T]>::get_mut(self, i)
}
}
impl<T> ArrayMut for Vec<T> {
type Element = T;
#[inline]
fn pop(&mut self) -> Option<T> {
Vec::pop(self)
}
#[inline]
fn push(&mut self, e: T) {
Vec::push(self, e);
}
}
#[cfg(feature = "c-abi")]
mod cabi {
use super::{Array, ArrayMut, Indexed, IndexedMut, SliceIndex};
use abi_stable::std_types::RVec;
impl<T, I> Indexed<I> for RVec<T>
where
I: SliceIndex<[T]>,
{
type Element = <I as SliceIndex<[T]>>::Output;
#[inline]
fn get(&self, i: I) -> Option<&Self::Element> {
<[T]>::get(self, i)
}
}
impl<T> Array for RVec<T> {
type Element = T;
fn iter<'i>(&'i self) -> Box<dyn Iterator<Item = &T> + 'i> {
Box::new(<[T]>::iter(self))
}
#[inline]
fn len(&self) -> usize {
self.len()
}
}
impl<T, I> IndexedMut<I> for RVec<T>
where
I: SliceIndex<[T]>,
{
type Element = <I as SliceIndex<[T]>>::Output;
#[inline]
fn get_mut(&mut self, i: I) -> Option<&mut Self::Element> {
<[T]>::get_mut(self, i)
}
}
#[cfg(feature = "c-abi")]
impl<T> ArrayMut for RVec<T> {
type Element = T;
#[inline]
fn pop(&mut self) -> Option<T> {
abi_stable::std_types::RVec::pop(self)
}
#[inline]
fn push(&mut self, e: T) {
abi_stable::std_types::RVec::push(self, e);
}
}
}