#![deny(missing_docs)]
#![cfg_attr(feature = "nightly", feature(
deque_extras,
))]
mod impls;
pub use map::Map;
pub use set::Set;
use std::ops::{Range, RangeFrom, RangeFull, RangeTo};
pub trait Mutate {}
pub trait AddRemove {}
pub trait Collection {
type Item;
fn len(&self) -> usize;
fn is_empty(&self) -> bool {
self.len() == 0
}
fn capacity(&self) -> usize;
fn append(&mut self, other: &mut Self) where Self: Sized + AddRemove {
self.extend_object(&mut other.drain());
}
fn extend_object(&mut self, items: &mut Iterator<Item = Self::Item>) where Self: AddRemove;
fn clear(&mut self) where Self: AddRemove {
self.drain();
}
fn drain<'a>(&'a mut self) -> Box<Iterator<Item = Self::Item> + 'a> where Self: AddRemove;
fn reserve(&mut self, additional: usize) where Self: AddRemove;
fn shrink_to_fit(&mut self) where Self: AddRemove;
fn with_capacity(capacity: usize) -> Self where Self: Sized + Default {
let _ = capacity;
unimplemented!()
}
fn into_vec(self) -> Vec<Self::Item> where Self: Sized {
unimplemented!()
}
}
pub trait Iter: Collection {
fn iter<'a>(&'a self) -> Box<Iterator<Item = &'a Self::Item> + 'a>;
fn iter_mut<'a>(&'a mut self) -> Box<Iterator<Item = &'a mut Self::Item> + 'a>
where Self: Mutate;
}
pub trait DrainRange<R>: Collection {
fn drain_range<'a>(&'a mut self, range: R) -> Box<Iterator<Item = Self::Item> + 'a>
where Self: AddRemove;
}
pub trait List:
Collection +
Iter +
DrainRange<Range<usize>> +
DrainRange<RangeFrom<usize>> +
DrainRange<RangeTo<usize>> +
DrainRange<RangeFull>
{
fn get(&self, index: usize) -> Option<&Self::Item>;
fn get_mut(&mut self, index: usize) -> Option<&mut Self::Item> where Self: Mutate;
fn swap(&mut self, i: usize, j: usize) where Self: Mutate;
fn reverse(&mut self) where Self: Mutate {
let len = self.len();
for i in 0..len / 2 {
self.swap(i, len - i - 1);
}
}
fn first(&self) -> Option<&Self::Item> {
self.get(0)
}
fn first_mut(&mut self) -> Option<&mut Self::Item> where Self: Mutate {
self.get_mut(0)
}
fn last(&self) -> Option<&Self::Item> {
self.get(self.len().wrapping_sub(1))
}
fn last_mut(&mut self) -> Option<&mut Self::Item> where Self: Mutate {
let len = self.len();
self.get_mut(len.wrapping_sub(1))
}
fn push(&mut self, item: Self::Item) where Self: AddRemove {
let len = self.len();
self.insert(len, item);
}
fn insert(&mut self, index: usize, item: Self::Item) where Self: AddRemove;
fn pop(&mut self) -> Option<Self::Item> where Self: AddRemove {
let len = self.len();
self.remove(len.wrapping_sub(1))
}
fn remove(&mut self, index: usize) -> Option<Self::Item> where Self: AddRemove;
fn swap_remove(&mut self, index: usize) -> Option<Self::Item> where Self: AddRemove;
fn truncate(&mut self, len: usize) where Self: AddRemove {
if len == 0 {
self.clear();
} else {
self.drain_range(len..);
}
}
fn split_off(&mut self, index: usize) -> Self where Self: Sized + AddRemove {
let _ = index;
unimplemented!()
}
}
impl<L: ?Sized + List> DrainRange<RangeFrom<usize>> for L {
fn drain_range<'a>(&'a mut self, range: RangeFrom<usize>) -> Box<Iterator<Item = L::Item> + 'a>
where L: AddRemove
{
let len = self.len();
self.drain_range(range.start..len)
}
}
impl<L: ?Sized + List> DrainRange<RangeTo<usize>> for L {
fn drain_range<'a>(&'a mut self, range: RangeTo<usize>) -> Box<Iterator<Item = L::Item> + 'a>
where L: AddRemove
{
self.drain_range(0..range.end)
}
}
impl<L: ?Sized + List> DrainRange<RangeFull> for L {
fn drain_range<'a>(&'a mut self, _range: RangeFull) -> Box<Iterator<Item = L::Item> + 'a>
where L: AddRemove
{
self.drain()
}
}
pub mod map {
use super::*;
pub trait Base: Collection<Item = (<Self as Base>::Key, <Self as Base>::Value)> {
type Key;
type Value;
fn iter<'a>(&'a self) -> Box<Iterator<Item = (&'a Self::Key, &'a Self::Value)> + 'a>;
fn iter_mut<'a>(&'a mut self)
-> Box<Iterator<Item = (&'a Self::Key, &'a mut Self::Value)> + 'a> where Self: Mutate;
fn insert(&mut self, key: Self::Key, value: Self::Value) -> Option<Self::Value>
where Self: AddRemove;
fn entry<'a>(&'a mut self, key: Self::Key) -> Entry<'a, Self::Key, Self::Value>
where Self: AddRemove;
}
pub trait Map<Q: ?Sized = <Self as Base>::Key>: Base {
fn contains_key(&self, key: &Q) -> bool {
self.get(key).is_some()
}
fn get(&self, key: &Q) -> Option<&Self::Value>;
fn get_mut(&mut self, key: &Q) -> Option<&mut Self::Value> where Self: Mutate;
fn remove(&mut self, key: &Q) -> Option<Self::Value> where Self: AddRemove;
}
pub enum Entry<'a, K: 'a, V: 'a> {
Occupied(Box<OccupiedEntry<Key = K, Value = V, MutValue = &'a mut V> + 'a>),
Vacant(Box<VacantEntry<Key = K, Value = V, MutValue = &'a mut V> + 'a>),
}
impl<'a, K: 'a, V: 'a> Entry<'a, K, V> {
pub fn or_insert(self, value: V) -> &'a mut V {
match self {
Entry::Occupied(e) => e.into_mut(),
Entry::Vacant(e) => e.insert(value),
}
}
pub fn or_insert_with<F: FnOnce() -> V>(self, f: F) -> &'a mut V {
match self {
Entry::Occupied(e) => e.into_mut(),
Entry::Vacant(e) => e.insert(f()),
}
}
}
pub trait OccupiedEntry {
type Key;
type Value;
type MutValue;
fn get(&self) -> &Self::Value;
fn get_mut(&mut self) -> &mut Self::Value;
fn into_mut(self: Box<Self>) -> Self::MutValue;
fn remove(self: Box<Self>) -> Self::Value;
}
pub trait VacantEntry {
type Key;
type Value;
type MutValue;
fn insert(self: Box<Self>, value: Self::Value) -> Self::MutValue;
}
}
pub mod set {
use super::*;
pub trait Base: Collection + Iter {
fn is_disjoint(&self, other: &Self) -> bool where Self: Sized;
fn is_subset(&self, other: &Self) -> bool where Self: Sized;
fn is_superset(&self, other: &Self) -> bool where Self: Sized {
other.is_subset(self)
}
fn insert(&mut self, item: Self::Item) -> bool where Self: AddRemove;
fn replace(&mut self, item: Self::Item) -> Option<Self::Item> where Self: AddRemove;
}
pub trait Set<Q: ?Sized = <Self as Collection>::Item>: Base {
fn contains(&self, item: &Q) -> bool {
self.get(item).is_some()
}
fn get(&self, item: &Q) -> Option<&Self::Item>;
fn remove(&mut self, item: &Q) -> bool where Self: AddRemove {
self.take(item).is_some()
}
fn take(&mut self, item: &Q) -> Option<Self::Item> where Self: AddRemove;
}
}
pub trait Queue: Collection + Iter {
fn push(&mut self, item: Self::Item) where Self: AddRemove;
fn front(&self) -> Option<&Self::Item>;
fn pop_front(&mut self) -> Option<Self::Item> where Self: AddRemove;
}
pub trait FifoQueue: Queue {
fn front_mut(&mut self) -> Option<&mut Self::Item> where Self: Mutate;
}
pub trait PrioQueue: Queue {
fn push_pop_front(&mut self, item: Self::Item) -> Self::Item where Self: AddRemove {
self.push(item);
self.pop_front().expect("queue was empty after a `push`")
}
fn replace_front(&mut self, item: Self::Item) -> Option<Self::Item> where Self: AddRemove {
let front = self.pop_front();
self.push(item);
front
}
}
pub trait Deque: Queue {
fn back(&self) -> Option<&Self::Item>;
fn pop_back(&mut self) -> Option<Self::Item> where Self: AddRemove;
}
pub trait FifoDeque: FifoQueue + Deque {
fn push_front(&mut self, item: Self::Item) where Self: AddRemove;
fn back_mut(&mut self) -> Option<&mut Self::Item> where Self: Mutate;
}
pub trait PrioDeque: PrioQueue + Deque {
fn push_pop_back(&mut self, item: Self::Item) -> Self::Item where Self: AddRemove {
self.push(item);
self.pop_back().expect("deque was empty after a `push`")
}
fn replace_back(&mut self, item: Self::Item) -> Option<Self::Item> where Self: AddRemove {
let back = self.pop_back();
self.push(item);
back
}
}
#[allow(dead_code)]
fn assert_object_safe() {
let _: &Mutate;
let _: &AddRemove;
let _: &Collection<Item = String>;
let _: &List<Item = String>;
let _: &Map<str, Item = (String, i32), Key = String, Value = i32>;
let _: &map::OccupiedEntry<Key = String, Value = i32, MutValue = &mut i32>;
let _: &map::VacantEntry<Key = String, Value = i32, MutValue = &mut i32>;
let _: &Set<str, Item = String>;
let _: &Queue<Item = String>;
let _: &Deque<Item = String>;
let _: &FifoQueue<Item = String>;
let _: &FifoDeque<Item = String>;
let _: &PrioQueue<Item = String>;
let _: &PrioDeque<Item = String>;
}