#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(feature = "nightly", feature(trait_alias))]
#[cfg(feature = "alloc")]
extern crate alloc;
extern crate core;
mod impls;
mod macros;
#[cfg(feature = "nightly")]
mod alias;
#[cfg(feature = "nightly")]
pub use alias::*;
#[cfg(not(feature = "nightly"))]
mod non_alias;
#[cfg(not(feature = "nightly"))]
pub use non_alias::*;
use core::ops::{Deref, DerefMut};
pub trait Collection {
type Item;
}
pub trait CollectionRef: Collection {
type ItemRef<'a>: Clone + Deref<Target = Self::Item>
where
Self: 'a;
fn upcast_item_ref<'short, 'long: 'short>(r: Self::ItemRef<'long>) -> Self::ItemRef<'short>
where
Self: 'long;
}
pub trait SimpleCollectionRef: CollectionRef {
fn into_ref<'r>(r: Self::ItemRef<'r>) -> &'r Self::Item
where
Self: 'r;
}
pub trait CollectionMut: Collection {
type ItemMut<'a>: DerefMut<Target = Self::Item>
where
Self: 'a;
fn upcast_item_mut<'short, 'long: 'short>(r: Self::ItemMut<'long>) -> Self::ItemMut<'short>
where
Self: 'long;
}
pub trait SimpleCollectionMut: CollectionMut {
fn into_mut<'r>(r: Self::ItemMut<'r>) -> &'r mut Self::Item
where
Self: 'r;
}
pub trait Keyed: Collection {
type Key;
}
pub trait KeyedRef: Keyed {
type KeyRef<'a>: Clone + Deref<Target = Self::Key>
where
Self: 'a;
fn upcast_key_ref<'short, 'long: 'short>(r: Self::KeyRef<'long>) -> Self::KeyRef<'short>
where
Self: 'long;
}
pub trait SimpleKeyedRef: KeyedRef {
fn into_ref<'r>(r: Self::KeyRef<'r>) -> &'r Self::Key
where
Self: 'r;
}
pub trait WithCapacity {
fn with_capacity(capacity: usize) -> Self;
}
pub trait Len {
fn len(&self) -> usize;
fn is_empty(&self) -> bool {
self.len() == 0
}
}
pub trait Capacity {
fn capacity(&self) -> usize;
}
pub trait Reserve {
fn reserve(&mut self, additional: usize);
}
pub trait Get<T>: CollectionRef {
fn get(&self, key: T) -> Option<Self::ItemRef<'_>>;
fn contains(&self, key: T) -> bool {
self.get(key).is_some()
}
}
pub trait GetMut<T>: Get<T> + CollectionMut {
fn get_mut(&mut self, key: T) -> Option<Self::ItemMut<'_>>;
}
pub trait GetKeyValue<T>: CollectionRef + KeyedRef {
fn get_key_value(&self, key: T) -> Option<(Self::KeyRef<'_>, Self::ItemRef<'_>)>;
}
pub trait GetKeyValueMut<T>: CollectionMut + KeyedRef {
fn get_key_value_mut(&mut self, key: T) -> Option<(Self::KeyRef<'_>, Self::ItemMut<'_>)>;
}
pub trait Front: CollectionRef {
fn front(&self) -> Option<Self::ItemRef<'_>>;
}
impl<T: Get<usize> + Len> Front for T {
fn front(&self) -> Option<Self::ItemRef<'_>> {
match self.len() {
0 => None,
_ => self.get(0),
}
}
}
pub trait Back: CollectionRef {
fn back(&self) -> Option<Self::ItemRef<'_>>;
}
impl<T: Get<usize> + Len> Back for T {
fn back(&self) -> Option<Self::ItemRef<'_>> {
match self.len() {
0 => None,
l => self.get(l - 1),
}
}
}
pub trait FrontMut: CollectionMut {
fn front_mut(&mut self) -> Option<Self::ItemMut<'_>>;
}
impl<T: GetMut<usize> + Len> FrontMut for T {
fn front_mut(&mut self) -> Option<Self::ItemMut<'_>> {
match self.len() {
0 => None,
_ => self.get_mut(0),
}
}
}
pub trait BackMut: CollectionMut {
fn back_mut(&mut self) -> Option<Self::ItemMut<'_>>;
}
impl<T: GetMut<usize> + Len> BackMut for T {
fn back_mut(&mut self) -> Option<Self::ItemMut<'_>> {
match self.len() {
0 => None,
l => self.get_mut(l - 1),
}
}
}
pub trait Insert: Collection {
type Output;
fn insert(&mut self, element: Self::Item) -> Self::Output;
}
pub trait MapInsert<K>: Collection {
type Output;
fn insert(&mut self, key: K, value: Self::Item) -> Self::Output;
}
pub trait PushFront: Collection {
type Output;
fn push_front(&mut self, element: Self::Item) -> Self::Output;
}
pub trait PushBack: Collection {
type Output;
fn push_back(&mut self, element: Self::Item) -> Self::Output;
}
pub trait Remove<T>: Collection {
fn remove(&mut self, key: T) -> Option<Self::Item>;
}
pub trait PopFront: Collection {
fn pop_front(&mut self) -> Option<Self::Item>;
}
pub trait PopBack: Collection {
fn pop_back(&mut self) -> Option<Self::Item>;
}
pub trait Clear {
fn clear(&mut self);
}
pub trait Iter: CollectionRef {
type Iter<'a>: Iterator<Item = Self::ItemRef<'a>>
where
Self: 'a;
fn iter(&self) -> Self::Iter<'_>;
}
pub trait IterMut: CollectionMut {
type IterMut<'a>: Iterator<Item = Self::ItemMut<'a>>
where
Self: 'a;
fn iter_mut(&mut self) -> Self::IterMut<'_>;
}
pub trait MapIter: KeyedRef + CollectionRef {
type Iter<'a>: Iterator<Item = (Self::KeyRef<'a>, Self::ItemRef<'a>)>
where
Self: 'a;
fn iter(&self) -> Self::Iter<'_>;
}
pub trait MapIterMut: KeyedRef + CollectionMut {
type IterMut<'a>: Iterator<Item = (Self::KeyRef<'a>, Self::ItemMut<'a>)>
where
Self: 'a;
fn iter_mut(&mut self) -> Self::IterMut<'_>;
}