1use 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<F, X, Y, A> MapInto<F, Y> for Vec<X, A>
87 where
88 A: Allocator,
89 F: FnMut(X) -> Y,
90 Vec<Y, A>: FromIterator<Y>,
91 {
92 type Cont<_T> = Vec<_T, A>;
93 type Elem = X;
94
95 fn apply(self, f: F) -> Self::Cont<Y> {
96 self.into_iter().map(f).collect()
97 }
98 }
99
100 impl<'a, F, X, Y, A> MapTo<F, Y> for &'a Vec<X, A>
101 where
102 A: Allocator,
103 F: FnMut(&X) -> Y,
104 Vec<Y, A>: FromIterator<Y>,
105 {
106 type Cont<_T> = Vec<_T, A>;
107 type Elem = &'a X;
108
109 fn apply(&self, f: F) -> Self::Cont<Y> {
110 self.iter().map(f).collect()
111 }
112 }
113}
114
115#[cfg(all(feature = "alloc", not(feature = "nightly")))]
116mod impl_alloc {
117 use super::{MapInto, MapTo};
118 use alloc::vec::Vec;
119
120 impl<F, X, Y> MapInto<F, Y> for Vec<X>
121 where
122 F: FnMut(X) -> Y,
123 Vec<Y>: FromIterator<Y>,
124 {
125 type Cont<_U> = Vec<_U>;
126 type Elem = X;
127
128 fn apply(self, f: F) -> Self::Cont<Y> {
129 self.into_iter().map(f).collect()
130 }
131 }
132
133 impl<'a, U, V, F> MapTo<F, V> for &'a Vec<U>
134 where
135 F: FnMut(&U) -> V,
136 Vec<V>: FromIterator<V>,
137 {
138 type Cont<_T> = Vec<_T>;
139 type Elem = &'a U;
140
141 fn apply(&self, f: F) -> Self::Cont<V> {
142 self.iter().map(f).collect()
143 }
144 }
145}