rspace_traits/impls/
impl_map.rs

1/*
2    Appellation: impl_map <module>
3    Created At: 2026.01.01:21:59:39
4    Contrib: @FL03
5*/
6use crate::ops::{MapInto, MapTo};
7
8impl<U, V, F> MapInto<F, V> for Option<U>
9where
10    F: FnOnce(U) -> V,
11{
12    type Cont<T> = Option<T>;
13    type Elem = U;
14
15    fn apply(self, f: F) -> Self::Cont<V> {
16        self.map(f)
17    }
18}
19
20impl<'a, F, X, Y> MapInto<F, Y> for &'a Option<X>
21where
22    F: FnOnce(&X) -> Y,
23{
24    type Cont<U> = Option<U>;
25    type Elem = &'a X;
26
27    fn apply(self, f: F) -> Self::Cont<Y> {
28        self.as_ref().map(f)
29    }
30}
31
32impl<'a, U, V, F> MapTo<F, V> for Option<&'a U>
33where
34    for<'b> F: FnOnce(&'b U) -> V,
35{
36    type Cont<T> = Option<T>;
37    type Elem = &'a U;
38
39    fn apply(&self, f: F) -> Self::Cont<V> {
40        self.map(f)
41    }
42}
43
44#[cfg(feature = "ndarray")]
45mod impl_ndarray {
46    use super::{MapInto, MapTo};
47    use ndarray::{Array, ArrayBase, Data, Dimension};
48
49    impl<A, B, S, D, F> MapInto<F, B> for ArrayBase<S, D, A>
50    where
51        A: Clone,
52        D: Dimension,
53        S: Data<Elem = A>,
54        F: Fn(A) -> B,
55    {
56        type Cont<V> = Array<V, D>;
57        type Elem = A;
58
59        fn apply(self, f: F) -> Self::Cont<B> {
60            self.mapv(f)
61        }
62    }
63
64    impl<A, B, S, D, F> MapTo<F, B> for ArrayBase<S, D, A>
65    where
66        A: Clone,
67        D: Dimension,
68        S: Data<Elem = A>,
69        F: Fn(A) -> B,
70    {
71        type Cont<V> = Array<V, D>;
72        type Elem = A;
73
74        fn apply(&self, f: F) -> Self::Cont<B> {
75            self.mapv(f)
76        }
77    }
78}
79
80#[cfg(all(feature = "alloc", feature = "nightly"))]
81mod impl_alloc {
82    use super::{MapInto, MapTo};
83    use alloc::alloc::Allocator;
84    use alloc::vec::Vec;
85
86    impl<U, V, A, F> MapInto<F, V> for Vec<U, A>
87    where
88        A: Allocator,
89        F: FnMut(U) -> V,
90        Vec<V, A>: FromIterator<V>,
91    {
92        type Cont<_T> = Vec<_T, A>;
93        type Elem = U;
94
95        fn apply(self, f: F) -> Self::Cont<V> {
96            self.into_iter().map(f).collect()
97        }
98    }
99
100    impl<'a, U, V, A, F> MapTo<F, V> for &'a Vec<U, A>
101    where
102        A: Allocator,
103        F: FnMut(&U) -> V,
104    {
105        type Cont<_T> = Vec<_T, A>;
106        type Elem = &'a U;
107
108        fn apply(&self, f: F) -> Self::Cont<V> {
109            self.iter().map(f).collect()
110        }
111    }
112}
113
114#[cfg(all(feature = "alloc", not(feature = "nightly")))]
115mod impl_alloc {
116    use super::{MapInto, MapTo};
117    use alloc::vec::Vec;
118
119    impl<F, X, Y> MapInto<F, Y> for Vec<X>
120    where
121        F: FnMut(X) -> Y,
122        Vec<Y>: FromIterator<Y>,
123    {
124        type Cont<_U> = Vec<_U>;
125        type Elem = X;
126
127        fn apply(self, f: F) -> Self::Cont<Y> {
128            self.into_iter().map(f).collect()
129        }
130    }
131
132    impl<'a, U, V, F> MapTo<F, V> for &'a Vec<U>
133    where
134        F: FnMut(&U) -> V,
135    {
136        type Cont<_T> = Vec<_T>;
137        type Elem = &'a U;
138
139        fn apply(&self, f: F) -> Self::Cont<V> {
140            self.iter().map(f).collect()
141        }
142    }
143}