devela/data/collections/traits/
collection.rsuse crate::{
Array,
NotAvailable::{self, NotImplemented, NotSupported},
Storage,
};
#[cfg(feature = "alloc")]
use crate::{BTreeMap, BTreeSet, BinaryHeap, Vec, VecDeque};
#[cfg(all(feature = "alloc", feature = "dep_hashbrown"))]
use crate::{HashMap, HashSet};
type Result<T> = crate::Result<T, NotAvailable>;
#[rustfmt::skip] #[allow(unused_variables)]
pub trait DataCollection {
type Element;
fn collection_capacity(&self) -> Result<usize> { Err(NotImplemented) }
fn collection_len(&self) -> Result<usize> { Err(NotImplemented) }
fn collection_is_empty(&self) -> Result<bool> { Err(NotImplemented) }
fn collection_is_full(&self) -> Result<bool> { Err(NotImplemented) }
fn collection_contains(&self, element: Self::Element) -> Result<bool>
where Self::Element: PartialEq { Err(NotImplemented) }
fn collection_count(&self, element: &Self::Element) -> Result<usize>
where Self::Element: PartialEq { Err(NotImplemented) }
}
#[rustfmt::skip]
impl<T, const LEN: usize, S: Storage> DataCollection for Array<T, LEN, S> {
type Element = T;
fn collection_capacity(&self) -> Result<usize> { Ok(LEN) }
fn collection_len(&self) -> Result<usize> { Ok(LEN) }
fn collection_is_empty(&self) -> Result<bool> { Err(NotSupported) }
fn collection_is_full(&self) -> Result<bool> { Err(NotSupported) }
fn collection_contains(&self, element: Self::Element) -> Result<bool> where T: PartialEq {
Ok(self.contains(&element))
}
fn collection_count(&self, element: &Self::Element) -> Result<usize> where T: PartialEq {
Ok(self.iter().filter(|&e| e == element).count())
}
}
#[rustfmt::skip]
impl<T, const N: usize> DataCollection for [T; N] {
type Element = T;
fn collection_capacity(&self) -> Result<usize> { Ok(N) }
fn collection_len(&self) -> Result<usize> { Ok(N) }
fn collection_is_empty(&self) -> Result<bool> { Err(NotSupported) }
fn collection_is_full(&self) -> Result<bool> { Err(NotSupported) }
fn collection_contains(&self, element: Self::Element) -> Result<bool> where T: PartialEq {
Ok(self.contains(&element))
}
fn collection_count(&self, element: &Self::Element) -> Result<usize> where T: PartialEq {
Ok(self.iter().filter(|&e| e == element).count())
}
}
#[rustfmt::skip]
#[cfg(feature = "alloc")]
impl<T> DataCollection for Vec<T> {
type Element = T;
fn collection_capacity(&self) -> Result<usize> { Ok(self.capacity()) }
fn collection_len(&self) -> Result<usize> { Ok(self.len()) }
fn collection_is_empty(&self) -> Result<bool> { Ok(self.is_empty()) }
fn collection_is_full(&self) -> Result<bool> { Ok(self.len() >= self.capacity()) }
fn collection_contains(&self, element: Self::Element) -> Result<bool> where T: PartialEq {
Ok(self.contains(&element))
}
fn collection_count(&self, element: &Self::Element) -> Result<usize> where T: PartialEq {
Ok(self.iter().filter(|&e| e == element).count())
}
}
#[rustfmt::skip]
#[cfg(feature = "alloc")]
impl<T> DataCollection for VecDeque<T> {
type Element = T;
fn collection_capacity(&self) -> Result<usize> { Ok(self.capacity()) }
fn collection_len(&self) -> Result<usize> { Ok(self.len()) }
fn collection_is_empty(&self) -> Result<bool> { Ok(self.is_empty()) }
fn collection_is_full(&self) -> Result<bool> { Ok(self.len() >= self.capacity()) }
fn collection_contains(&self, element: Self::Element) -> Result<bool> where T: PartialEq {
Ok(self.contains(&element))
}
fn collection_count(&self, element: &Self::Element) -> Result<usize> where T: PartialEq {
Ok(self.iter().filter(|&e| e == element).count())
}
}
#[rustfmt::skip]
#[cfg(feature = "alloc")]
impl<T> DataCollection for BinaryHeap<T> {
type Element = T;
fn collection_capacity(&self) -> Result<usize> { Ok(self.capacity()) }
fn collection_len(&self) -> Result<usize> { Ok(self.len()) }
fn collection_is_empty(&self) -> Result<bool> { Ok(self.is_empty()) }
fn collection_is_full(&self) -> Result<bool> { Ok(self.len() >= self.capacity()) }
fn collection_contains(&self, _: Self::Element) -> Result<bool> { Err(NotSupported) }
fn collection_count(&self, _: &Self::Element) -> Result<usize> { Err(NotSupported) }
}
#[rustfmt::skip]
#[cfg(feature = "alloc")]
impl<K, V> DataCollection for BTreeMap<K, V> {
type Element = V;
fn collection_capacity(&self) -> Result<usize> { Err(NotSupported) }
fn collection_len(&self) -> Result<usize> { Ok(self.len()) }
fn collection_is_empty(&self) -> Result<bool> { Ok(self.is_empty()) }
fn collection_is_full(&self) -> Result<bool> { Err(NotSupported) }
fn collection_contains(&self, element: Self::Element) -> Result<bool> where V: PartialEq {
Ok(self.values().any(|value| *value == element))
}
fn collection_count(&self, element: &Self::Element) -> Result<usize> where V: PartialEq {
Ok(self.values().filter(|&value| value == element).count())
}
}
#[rustfmt::skip]
#[cfg(feature = "alloc")]
impl<V> DataCollection for BTreeSet<V> {
type Element = V;
fn collection_capacity(&self) -> Result<usize> { Err(NotSupported) }
fn collection_len(&self) -> Result<usize> { Ok(self.len()) }
fn collection_is_empty(&self) -> Result<bool> { Ok(self.is_empty()) }
fn collection_is_full(&self) -> Result<bool> { Err(NotSupported) }
fn collection_contains(&self, element: Self::Element) -> Result<bool> where V: PartialEq {
Ok(self.iter().any(|value| *value == element))
}
fn collection_count(&self, element: &Self::Element) -> Result<usize> where V: PartialEq {
Ok(usize::from(self.iter().any(|e| e == element)))
}
}
#[rustfmt::skip]
#[cfg(all(feature = "alloc", feature = "dep_hashbrown"))]
impl<K, V> DataCollection for HashMap<K, V> {
type Element = V;
fn collection_capacity(&self) -> Result<usize> { Err(NotSupported) }
fn collection_len(&self) -> Result<usize> { Ok(self.len()) }
fn collection_is_empty(&self) -> Result<bool> { Ok(self.is_empty()) }
fn collection_is_full(&self) -> Result<bool> { Err(NotSupported) }
fn collection_contains(&self, element: Self::Element) -> Result<bool> where V: PartialEq {
Ok(self.values().any(|value| *value == element))
}
fn collection_count(&self, element: &Self::Element) -> Result<usize> where V: PartialEq {
Ok(self.values().filter(|&value| value == element).count())
}
}
#[rustfmt::skip]
#[cfg(all(feature = "alloc", feature = "dep_hashbrown"))]
impl<V> DataCollection for HashSet<V> {
type Element = V;
fn collection_capacity(&self) -> Result<usize> { Ok(self.capacity()) }
fn collection_len(&self) -> Result<usize> { Ok(self.len()) }
fn collection_is_empty(&self) -> Result<bool> { Ok(self.is_empty()) }
fn collection_is_full(&self) -> Result<bool> { Err(NotSupported) }
fn collection_contains(&self, element: Self::Element) -> Result<bool> where V: PartialEq {
Ok(self.iter().any(|value| *value == element))
}
fn collection_count(&self, element: &Self::Element) -> Result<usize> where V: PartialEq {
Ok(usize::from(self.iter().any(|e| e == element)))
}
}