scsys_traits/cont/
container.rs

1/*
2    Appellation: container <module>
3    Contrib: @FL03
4*/
5use super::GetMut;
6
7/// [`RawContainer`] defines a standard interface for all _containers_ that are used to store
8/// other entities.
9pub trait RawContainer {
10    type Item;
11}
12
13pub trait KeyedContainer<T>: Container<T>
14where
15    Self::Cont<T>: GetMut<T, Key = Self::Key>,
16{
17    type Key;
18}
19
20/// The [`Container`] trait is a higher-level abstraction over [`RawContainer`].
21pub trait Container<T> {
22    type Cont<V>: RawContainer<Item = V> + ?Sized;
23}
24
25/*
26 ************* Implementations *************
27*/
28impl<T> RawContainer for [T] {
29    type Item = T;
30}
31
32impl<T> RawContainer for &[T] {
33    type Item = T;
34}
35
36impl<T> RawContainer for &mut [T] {
37    type Item = T;
38}
39
40impl<T> Container<T> for [T] {
41    type Cont<V> = [V];
42}
43
44macro_rules! impl_container {
45    ($($($t:ident)::*<$T:ident>),* $(,)?) => {
46        $(
47            impl_container!(@impl $($t)::*<$T>);
48        )*
49    };
50    (@impl $($t:ident)::*<$T:ident>) => {
51        impl_container!(@raw $($t)::*<$T>);
52        impl_container!(@cont $($t)::*<$T>);
53    };
54    (@raw $($t:ident)::*<$lt:lifetime, $T:ident>) => {
55        impl<$T> RawContainer for $($t)::*<$lt, $T> {
56            type Item = $T;
57        }
58    };
59    (@raw $($t:ident)::*<$T:ident>) => {
60        impl<$T> RawContainer for $($t)::*<$T> {
61            type Item = $T;
62        }
63    };
64    (@cont $($t:ident)::*<$T:ident>) => {
65        impl<$T> Container<$T> for $($t)::*<$T> {
66            type Cont<DType_> = $($t)::*<DType_>;
67        }
68    };
69}
70
71impl_container! {
72    Option<T>
73}
74
75#[cfg(feature = "alloc")]
76impl_container! {
77    alloc::vec::Vec<T>,
78    alloc::boxed::Box<T>,
79    alloc::rc::Rc<T>,
80    alloc::rc::Weak<T>,
81    alloc::sync::Arc<T>,
82    alloc::collections::BinaryHeap<T>,
83    alloc::collections::BTreeSet<T>,
84    alloc::collections::LinkedList<T>,
85    alloc::collections::VecDeque<T>,
86}
87
88#[cfg(feature = "std")]
89impl_container! {
90    std::cell::Cell<T>,
91    std::cell::OnceCell<T>,
92    std::cell::RefCell<T>,
93    std::sync::Mutex<T>,
94    std::sync::RwLock<T>,
95    std::sync::LazyLock<T>,
96    std::collections::HashSet<V>,
97}