pub trait CollectionBase {
type Item;
fn item_count(&self) -> usize;
#[inline]
fn is_empty(&self) -> bool {
self.item_count() == 0
}
fn push(&mut self, elem: Self::Item);
fn clear(&mut self);
}
pub trait CollectionBytes: CollectionBase {
fn byte_len(&self) -> usize;
fn item_byte_len(item: &Self::Item) -> usize;
}
pub trait Collection:
CollectionBase + Default + FromIterator<Self::Item> + Extend<Self::Item>
{
fn with_capacity(capacity: usize) -> Self;
}
pub trait CollectionAlloc: CollectionBase + Extend<Self::Item> + Sized {
type Allocator;
fn with_capacity_in(capacity: usize, allocator: Self::Allocator) -> Self;
fn new_in(allocator: Self::Allocator) -> Self {
Self::with_capacity_in(0, allocator)
}
fn allocator(&self) -> &Self::Allocator;
}
pub trait CollectionCapacity: CollectionBase {
type ReserveError;
fn capacity(&self) -> usize;
fn reserve(&mut self, additional: usize);
fn try_reserve(&mut self, additional: usize) -> Result<(), Self::ReserveError>;
fn try_push(&mut self, elem: Self::Item) -> Result<(), Self::Item> {
if self.try_reserve(1).is_err() {
return Err(elem);
}
self.push(elem);
Ok(())
}
}
pub trait CollectionMap: CollectionBase<Item = (Self::Key, Self::Value)> {
type Key;
type Value;
fn contains_key(&self, key: &Self::Key) -> bool;
fn get(&self, key: &Self::Key) -> Option<&Self::Value>;
fn get_mut(&mut self, key: &Self::Key) -> Option<&mut Self::Value>;
fn iter(&self) -> impl Iterator<Item = (&Self::Key, &Self::Value)>;
fn keys(&self) -> impl Iterator<Item = &Self::Key> {
self.iter().map(|(key, _)| key)
}
fn values(&self) -> impl Iterator<Item = &Self::Value> {
self.iter().map(|(_, value)| value)
}
fn iter_mut(&mut self) -> impl Iterator<Item = (&Self::Key, &mut Self::Value)>;
fn values_mut(&mut self) -> impl Iterator<Item = &mut Self::Value>;
fn insert(&mut self, key: Self::Key, value: Self::Value) -> Option<Self::Value>;
fn remove(&mut self, key: &Self::Key) -> Option<Self::Value>;
fn retain(&mut self, f: impl FnMut(&Self::Key, &mut Self::Value) -> bool);
}
pub trait CollectionSet: CollectionBase {
fn contains(&self, item: &Self::Item) -> bool;
fn get(&self, item: &Self::Item) -> Option<&Self::Item>;
fn iter(&self) -> impl Iterator<Item = &Self::Item>;
fn insert(&mut self, item: Self::Item) -> bool;
fn replace(&mut self, item: Self::Item) -> Option<Self::Item>;
fn remove(&mut self, item: &Self::Item) -> bool;
fn take(&mut self, item: &Self::Item) -> Option<Self::Item>;
fn retain(&mut self, f: impl FnMut(&Self::Item) -> bool);
}
pub trait CollectionEntry: CollectionMap {
type Entry<'a>: MapEntry<'a, Key = Self::Key, Value = Self::Value>
where
Self: 'a;
fn entry(&mut self, key: Self::Key) -> Self::Entry<'_>;
}
pub trait MapEntry<'a>: Sized {
type Key;
type Value: 'a;
fn key(&self) -> &Self::Key;
fn or_insert(self, default: Self::Value) -> &'a mut Self::Value;
fn or_insert_with<F: FnOnce() -> Self::Value>(self, default: F) -> &'a mut Self::Value;
fn or_default(self) -> &'a mut Self::Value
where
Self::Value: Default;
fn and_modify<F: FnOnce(&mut Self::Value)>(self, f: F) -> Self;
}