rspace_traits/
container.rs

1/*
2    Appellation: store <module>
3    Created At: 2025.12.26:14:12:46
4    Contrib: @FL03
5*/
6use crate::space::RawSpace;
7
8/// The [`Container`] trait is a higher-kinded trait used to establish an interface for
9/// defining containers themselves.
10pub trait Container<U>
11where
12    Self::Cont<U>: RawSpace<Elem = U>,
13{
14    type Cont<V>: ?Sized;
15}
16
17pub trait ContainerIter<U>: Container<U>
18where
19    Self::Cont<U>: RawSpace<Elem = U>,
20{
21    type Iter<'a, T>: Iterator<Item = &'a T>
22    where
23        Self: 'a,
24        T: 'a;
25}
26
27/*
28 ************* Implementations *************
29*/
30
31impl<C, T> Container<T> for &C
32where
33    C: Container<T>,
34    C::Cont<T>: RawSpace<Elem = T>,
35{
36    type Cont<U> = <C>::Cont<U>;
37}
38
39impl<C, T> Container<T> for &mut C
40where
41    C: Container<T>,
42    C::Cont<T>: RawSpace<Elem = T>,
43{
44    type Cont<U> = <C>::Cont<U>;
45}
46
47impl<T> Container<T> for [T] {
48    type Cont<U> = [U];
49}
50
51impl<T, const N: usize> Container<T> for [T; N] {
52    type Cont<U> = [U; N];
53}
54
55#[allow(unused_macros)]
56macro_rules! impl_container  {
57    (impl<Elem = $E:ident> $trait:ident for {$(
58        $($cont:ident)::*<$($T:ident),*> $({where $($rest:tt)*})?
59    ),* $(,)?}) => {
60        $(impl_container! {
61            @impl<Elem = $E> $trait for $($cont)::*<$($T),*> $(where $($rest)*)?
62        })*
63    };
64    (@impl<Elem = $E:ident> $trait:ident for $($cont:ident)::*<$($T:ident),*> $(where $($rest:tt)*)?) => {
65        paste::paste! {
66            impl<$($T),*> $trait<$E> for $($cont)::*<$($T),*> $(where $($rest)*)? {
67                type Cont<$([<_ $T>]),*> = $($cont)::*<$([<_ $T>]),*>;
68            }
69        }
70    };
71}
72
73macro_rules! impl_tuple_container {
74    (@impl<Elem = $E:ident> $trait:ident for ($($name:ident),+ $(,)?)) => {
75        paste::paste! {
76            impl<$E> $trait<$E> for ($($name),+) {
77                type Cont<[<_ $E>]> = ($([<_ $name>]),+);
78            }
79        }
80    };
81    (impl<$T:ident> $trait:ident for { $(($($name:ident),+ $(,)?)),* $(,)?}) => {
82        $(impl_tuple_container! { @impl<Elem = $T> $trait for ($($name),+) })*
83    };
84}
85
86impl_tuple_container! {
87    impl<T> Container for {
88        (T, T),
89        (T, T, T),
90        (T, T, T, T),
91        (T, T, T, T, T),
92        (T, T, T, T, T, T),
93        (T, T, T, T, T, T, T),
94        (T, T, T, T, T, T, T, T),
95        (T, T, T, T, T, T, T, T, T),
96        (T, T, T, T, T, T, T, T, T, T),
97        (T, T, T, T, T, T, T, T, T, T, T),
98        (T, T, T, T, T, T, T, T, T, T, T, T),
99    }
100}
101
102// impl_container! {
103//     impl<Elem = T> Container for {
104//         core::option::Option<T>,
105//         core::cell::Cell<T>,
106//         core::cell::OnceCell<T>,
107//         core::cell::RefCell<T>,
108//         core::cell::UnsafeCell<T>,
109//         core::ops::Range<T>,
110//         core::result::Result<T, E>,
111//     }
112// }
113
114#[cfg(feature = "alloc")]
115mod impl_alloc {
116    use super::Container;
117    use alloc::collections::*;
118    use alloc::vec::Vec;
119
120    impl<T> Container<T> for Vec<T> {
121        type Cont<U> = Vec<U>;
122    }
123
124    impl<V> Container<V> for BTreeSet<V> {
125        type Cont<U> = BTreeSet<U>;
126    }
127
128    impl<K, V> Container<V> for BTreeMap<K, V>
129    where
130        K: Ord,
131    {
132        type Cont<U> = BTreeMap<K, U>;
133    }
134
135    impl<V> Container<V> for LinkedList<V> {
136        type Cont<U> = LinkedList<U>;
137    }
138
139    impl<V> Container<V> for VecDeque<V> {
140        type Cont<U> = VecDeque<U>;
141    }
142}