rspace_traits/
container.rs

1/*
2    Appellation: container <module>
3    Created At: 2025.12.29:14:39:20
4    Contrib: @FL03
5*/
6use crate::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 ContainerIndex<T, Idx>: Container<T>
18where
19    Self::Cont<T>: RawSpace<Elem = T> + core::ops::Index<Idx, Output = T>,
20{
21    type Output;
22
23    fn get(&self, index: Idx) -> Option<&Self::Output>;
24}
25
26/// The [`ContainerIter`] trait extends the [`Container`] trait to provide an interface
27/// for obtaining iterators over the elements of the container.
28pub trait ContainerIter<T>: Container<T>
29where
30    Self::Cont<T>: RawSpace<Elem = T>,
31{
32    type Iter<'a, U>: Iterator<Item = &'a U>
33    where
34        Self: 'a,
35        U: 'a;
36
37    fn iter(&self) -> Self::Iter<'_, T>;
38}
39
40pub trait ContainerIterMut<T>: Container<T>
41where
42    Self::Cont<T>: RawSpace<Elem = T>,
43{
44    type Iter<'a, U>: Iterator<Item = &'a mut U>
45    where
46        Self: 'a,
47        U: 'a;
48
49    fn iter_mut(&mut self) -> Self::Iter<'_, T>;
50}
51
52impl<T> ContainerIter<T> for [T] {
53    type Iter<'a, U>
54        = core::slice::Iter<'a, U>
55    where
56        Self: 'a,
57        U: 'a;
58
59    fn iter(&self) -> Self::Iter<'_, T> {
60        <[T]>::iter(self)
61    }
62}
63
64impl<T> ContainerIterMut<T> for [T] {
65    type Iter<'a, U>
66        = core::slice::IterMut<'a, U>
67    where
68        Self: 'a,
69        U: 'a;
70
71    fn iter_mut(&mut self) -> Self::Iter<'_, T> {
72        <[T]>::iter_mut(self)
73    }
74}
75
76impl<const N: usize, T> ContainerIter<T> for [T; N] {
77    type Iter<'a, U>
78        = core::slice::Iter<'a, U>
79    where
80        Self: 'a,
81        U: 'a;
82
83    fn iter(&self) -> Self::Iter<'_, T> {
84        <[T]>::iter(self)
85    }
86}
87
88impl<const N: usize, T> ContainerIterMut<T> for [T; N] {
89    type Iter<'a, U>
90        = core::slice::IterMut<'a, U>
91    where
92        Self: 'a,
93        U: 'a;
94
95    fn iter_mut(&mut self) -> Self::Iter<'_, T> {
96        <[T]>::iter_mut(self)
97    }
98}
99
100impl<T> ContainerIter<T> for &[T] {
101    type Iter<'a, U>
102        = core::slice::Iter<'a, U>
103    where
104        Self: 'a,
105        U: 'a;
106
107    fn iter(&self) -> Self::Iter<'_, T> {
108        <[T]>::iter(self)
109    }
110}
111
112impl<T> ContainerIterMut<T> for &mut [T] {
113    type Iter<'a, U>
114        = core::slice::IterMut<'a, U>
115    where
116        Self: 'a,
117        U: 'a;
118
119    fn iter_mut(&mut self) -> Self::Iter<'_, T> {
120        <[T]>::iter_mut(self)
121    }
122}