rspace_traits/impls/
impl_space.rs

1/*
2    Appellation: impl_raw_space <module>
3    Created At: 2025.12.26:19:20:09
4    Contrib: @FL03
5*/
6use crate::{RawSpace, RawSpaceMut, RawSpaceRef, SliceSpace, SliceSpaceMut};
7
8macro_rules! impl_scalar_space  {
9    (impl $trait:ident for {$($T:ty),* $(,)?}) => {
10        $(impl_scalar_space! { @impl $trait for $T })*
11    };
12    (@impl $trait:ident for $T:ty) => {
13        impl $crate::$trait for $T {
14            type Elem = $T;
15
16        }
17    };
18}
19
20macro_rules! impl_raw_space  {
21    (impl<Elem = $E:ident> $trait:ident for {$(
22        $($cont:ident)::*<$($lt:lifetime,)? $($T:ident),*> $({where $($rest:tt)*})?
23    ),* $(,)?}) => {
24        $(impl_raw_space! {
25            @impl<Elem = $E> $trait for $($cont)::*<$($lt,)? $($T),*> $(where $($rest)*)?
26        })*
27    };
28    (@impl<Elem = $E:ident> $trait:ident for $($cont:ident)::*<$($lt:lifetime,)? $($T:ident),*> $(where $($rest:tt)*)?) => {
29        impl<$($lt,)? $($T),*> $crate::$trait for $($cont)::*<$($lt,)? $($T),*> $(where $($rest)*)? {
30            type Elem = $E;
31        }
32    };
33}
34
35macro_rules! impl_raw_tuple_store {
36    (@impl<Elem = $E:ident> $trait:ident for ($($name:ident),+ $(,)?)) => {
37        impl<$E> $crate::$trait for ($($name),+) {
38            type Elem = $E;
39        }
40    };
41    (impl<Elem = $E:ident> $trait:ident for {$(($($name:ident),+)),* $(,)?}) => {
42        $(impl_raw_tuple_store! { @impl<Elem = $E> $trait for ($($name),+) } )*
43    };
44}
45
46impl_scalar_space! {
47    impl RawSpace for {
48        i8, i16, i32, i64, i128, isize,
49        u8, u16, u32, u64, u128, usize,
50        f32, f64,
51        bool, char
52    }
53}
54
55#[cfg(feature = "alloc")]
56impl_scalar_space! {
57    impl RawSpace for {
58        alloc::string::String
59    }
60}
61
62impl_raw_tuple_store! {
63    impl<Elem = T> RawSpace for {
64        (T, T),
65        (T, T, T),
66        (T, T, T, T),
67        (T, T, T, T, T),
68        (T, T, T, T, T, T),
69        (T, T, T, T, T, T, T),
70        (T, T, T, T, T, T, T, T),
71        (T, T, T, T, T, T, T, T, T),
72        (T, T, T, T, T, T, T, T, T, T),
73        (T, T, T, T, T, T, T, T, T, T, T),
74        (T, T, T, T, T, T, T, T, T, T, T, T),
75    }
76}
77
78impl_raw_space! {
79    impl<Elem = T> RawSpace for {
80        core::option::Option<T>,
81        core::cell::Cell<T>,
82        core::cell::OnceCell<T>,
83        core::cell::RefCell<T>,
84        core::cell::UnsafeCell<T>,
85        core::ops::Range<T>,
86        core::result::Result<T, E>,
87    }
88}
89
90#[cfg(all(feature = "alloc", not(feature = "nightly")))]
91impl_raw_space! {
92    impl<Elem = T> RawSpace for {
93        alloc::boxed::Box<T>,
94        alloc::rc::Rc<T>,
95        alloc::sync::Arc<T>,
96        alloc::collections::BTreeSet<T>,
97        alloc::collections::LinkedList<T>,
98        alloc::collections::VecDeque<T>,
99        alloc::collections::BinaryHeap<T>,
100        alloc::collections::BTreeMap<K, T>,
101        alloc::collections::btree_map::Entry<'a, K, T>,
102        alloc::vec::Vec<T>,
103    }
104}
105
106#[cfg(all(feature = "alloc", feature = "nightly"))]
107impl_raw_space! {
108    impl<Elem = T> RawSpace for {
109        alloc::collections::BTreeSet<T, A> { where A: Clone + alloc::alloc::Allocator },
110        alloc::collections::LinkedList<T, A> { where A: alloc::alloc::Allocator },
111        alloc::collections::VecDeque<T, A> { where A: alloc::alloc::Allocator },
112        alloc::collections::BinaryHeap<T, A> { where A: alloc::alloc::Allocator },
113        alloc::collections::BTreeMap<K, T, A> { where A: Clone + alloc::alloc::Allocator },
114        alloc::collections::btree_map::Entry<'a, K, T, A> { where A: Clone + alloc::alloc::Allocator },
115        alloc::vec::Vec<T, A> { where A: alloc::alloc::Allocator },
116    }
117}
118
119#[cfg(feature = "std")]
120impl_raw_space! {
121    impl<Elem = T> RawSpace for {
122        std::sync::Mutex<T>,
123        std::sync::RwLock<T>,
124        std::sync::LazyLock<T>,
125        std::collections::HashMap<K, T>,
126        std::collections::HashSet<T>,
127    }
128}
129
130#[cfg(feature = "hashbrown")]
131impl_raw_space! {
132    impl<Elem = T> RawSpace for {
133        hashbrown::HashMap<K, T, S>,
134        hashbrown::HashSet<T, S>,
135    }
136}
137
138#[cfg(feature = "ndarray")]
139impl_raw_space! {
140    impl<Elem = T> RawSpace for {
141        ndarray::ArrayBase<S, D, T> { where S: ndarray::Data<Elem = T>, D: ndarray::Dimension },
142    }
143}
144
145#[cfg(feature = "complex")]
146impl<T> RawSpace for num_complex::Complex<T> {
147    type Elem = T;
148}
149
150impl<T> RawSpace for [T] {
151    type Elem = T;
152}
153
154impl<T> RawSpace for &[T] {
155    type Elem = T;
156}
157
158impl<T> RawSpace for &mut [T] {
159    type Elem = T;
160}
161
162impl<const N: usize, T> RawSpace for [T; N] {
163    type Elem = T;
164}
165
166impl<const N: usize, T> RawSpaceRef for [T; N] {
167    fn as_ptr(&self) -> *const Self::Elem {
168        <[T]>::as_ptr(self)
169    }
170}
171
172impl<const N: usize, T> RawSpaceMut for [T; N] {
173    fn as_ptr_mut(&mut self) -> *mut Self::Elem {
174        <[T]>::as_mut_ptr(self)
175    }
176}
177
178impl<const N: usize, T> SliceSpace for [T; N] {
179    fn as_slice(&self) -> &[Self::Elem] {
180        self
181    }
182}
183
184impl<const N: usize, T> SliceSpaceMut for [T; N] {
185    fn as_mut_slice(&mut self) -> &mut [Self::Elem] {
186        self
187    }
188}
189
190impl<T> RawSpaceRef for [T] {
191    fn as_ptr(&self) -> *const Self::Elem {
192        <[T]>::as_ptr(self)
193    }
194}
195
196impl<T> RawSpaceMut for [T] {
197    fn as_ptr_mut(&mut self) -> *mut Self::Elem {
198        <[T]>::as_mut_ptr(self)
199    }
200}
201
202impl<T> SliceSpace for [T] {
203    fn as_slice(&self) -> &[Self::Elem] {
204        self
205    }
206}
207
208impl<T> SliceSpaceMut for [T] {
209    fn as_mut_slice(&mut self) -> &mut [Self::Elem] {
210        self
211    }
212}
213
214impl<T> RawSpaceRef for &[T] {
215    fn as_ptr(&self) -> *const Self::Elem {
216        <[T]>::as_ptr(self)
217    }
218}
219
220impl<T> SliceSpace for &[T] {
221    fn as_slice(&self) -> &[Self::Elem] {
222        self
223    }
224}
225
226impl<T> RawSpaceRef for &mut [T] {
227    fn as_ptr(&self) -> *const Self::Elem {
228        <[T]>::as_ptr(self)
229    }
230}
231
232impl<T> RawSpaceMut for &mut [T] {
233    fn as_ptr_mut(&mut self) -> *mut Self::Elem {
234        <[T]>::as_mut_ptr(self)
235    }
236}
237
238impl<T> SliceSpace for &mut [T] {
239    fn as_slice(&self) -> &[Self::Elem] {
240        self
241    }
242}
243
244impl<T> SliceSpaceMut for &mut [T] {
245    fn as_mut_slice(&mut self) -> &mut [Self::Elem] {
246        self
247    }
248}
249
250#[cfg(all(feature = "alloc", feature = "nightly"))]
251mod impl_alloc {
252    use crate::space::*;
253    use alloc::alloc::Allocator;
254    use alloc::vec::Vec;
255
256    impl<T, A> RawSpaceRef for Vec<T, A>
257    where
258        A: Allocator,
259    {
260        fn as_ptr(&self) -> *const Self::Elem {
261            Vec::as_ptr(self)
262        }
263    }
264
265    impl<T, A> RawSpaceMut for Vec<T, A>
266    where
267        A: Allocator,
268    {
269        fn as_ptr_mut(&mut self) -> *mut Self::Elem {
270            Vec::as_mut_ptr(self)
271        }
272    }
273
274    impl<T, A> SliceSpace for Vec<T, A>
275    where
276        A: Allocator,
277    {
278        fn as_slice(&self) -> &[Self::Elem] {
279            self.as_slice()
280        }
281    }
282
283    impl<T, A> SliceSpaceMut for Vec<T, A>
284    where
285        A: Allocator,
286    {
287        fn as_mut_slice(&mut self) -> &mut [Self::Elem] {
288            self.as_mut_slice()
289        }
290    }
291}
292
293#[cfg(all(feature = "alloc", not(feature = "nightly")))]
294mod impl_alloc {
295    use crate::space::*;
296    use alloc::vec::Vec;
297
298    impl<T> RawSpaceRef for Vec<T> {
299        fn as_ptr(&self) -> *const Self::Elem {
300            Vec::as_ptr(self)
301        }
302    }
303
304    impl<T> RawSpaceMut for Vec<T> {
305        fn as_ptr_mut(&mut self) -> *mut Self::Elem {
306            Vec::as_mut_ptr(self)
307        }
308    }
309
310    impl<T> SliceSpace for Vec<T> {
311        fn as_slice(&self) -> &[Self::Elem] {
312            self.as_slice()
313        }
314    }
315
316    impl<T> SliceSpaceMut for Vec<T> {
317        fn as_mut_slice(&mut self) -> &mut [Self::Elem] {
318            self.as_mut_slice()
319        }
320    }
321}