rspace_traits/impls/
impl_map.rs1use crate::ops::map::{MapInto, MapTo};
7
8impl<F, X, Y> MapInto<F, Y> for Option<X>
9where
10 F: FnOnce(X) -> Y,
11{
12 type Cont<U> = Option<U>;
13 type Elem = X;
14
15 fn map_into(self, f: F) -> Self::Cont<Y> {
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 map_into(self, f: F) -> Self::Cont<Y> {
28 self.as_ref().map(f)
29 }
30}
31
32impl<F, X, Y> MapTo<F, Y> for Option<X>
33where
34 F: FnOnce(&X) -> Y,
35{
36 type Cont<U> = Option<U>;
37 type Elem = X;
38
39 fn map_to(&self, f: F) -> Self::Cont<Y> {
40 self.as_ref().map(f)
41 }
42}
43
44#[cfg(all(feature = "alloc", feature = "nightly"))]
45mod impl_alloc {
46 use crate::ops::map::{MapInto, MapTo};
47 use alloc::alloc::Allocator;
48 use alloc::vec::Vec;
49
50 impl<X, A, F, Y> MapInto<F, Y> for Vec<X, A>
51 where
52 A: Allocator,
53 F: FnMut(X) -> Y,
54 Vec<Y, A>: FromIterator<Y>,
55 {
56 type Cont<U> = Vec<U, A>;
57 type Elem = X;
58
59 fn map_into(self, f: F) -> Self::Cont<Y> {
60 self.into_iter().map(f).collect()
61 }
62 }
63
64 impl<X, A, F, Y> MapTo<F, Y> for Vec<X, A>
65 where
66 A: Allocator,
67 F: FnMut(&X) -> Y,
68 Vec<Y, A>: FromIterator<Y>,
69 {
70 type Cont<U> = Vec<U, A>;
71 type Elem = X;
72
73 fn map_to(&self, f: F) -> Self::Cont<Y> {
74 self.iter().map(f).collect()
75 }
76 }
77}
78
79#[cfg(all(feature = "alloc", not(feature = "nightly")))]
80mod impl_alloc {
81 use crate::ops::map::{MapInto, MapTo};
82 use alloc::vec::Vec;
83
84 impl<F, X, Y> MapInto<F, Y> for Vec<X>
85 where
86 F: FnMut(X) -> Y,
87 Vec<Y>: FromIterator<Y>,
88 {
89 type Cont<U> = Vec<U>;
90 type Elem = X;
91
92 fn map_into(self, f: F) -> Self::Cont<Y> {
93 self.into_iter().map(f).collect()
94 }
95 }
96
97 impl<F, X, Y> MapTo<F, Y> for Vec<X>
98 where
99 F: FnMut(&X) -> Y,
100 Vec<Y>: FromIterator<Y>,
101 {
102 type Cont<U> = Vec<U>;
103 type Elem = X;
104
105 fn map_to(&self, f: F) -> Self::Cont<Y> {
106 self.iter().map(f).collect()
107 }
108 }
109}