@@ -3,7 +3,7 @@ use crate::iter::adapters::{
3
3
zip:: try_get_unchecked, SourceIter , TrustedRandomAccess , TrustedRandomAccessNoCoerce ,
4
4
} ;
5
5
use crate :: iter:: { FusedIterator , InPlaceIterable , TrustedLen } ;
6
- use crate :: ops:: Try ;
6
+ use crate :: ops:: { FromResidual , Try } ;
7
7
8
8
/// An iterator that maps the values of `iter` with `f`.
9
9
///
@@ -65,7 +65,7 @@ pub struct Map<I, F> {
65
65
}
66
66
67
67
impl < I , F > Map < I , F > {
68
- pub ( in crate :: iter) fn new ( iter : I , f : F ) -> Map < I , F > {
68
+ pub ( in crate :: iter) const fn new ( iter : I , f : F ) -> Map < I , F > {
69
69
Map { iter, f }
70
70
}
71
71
}
@@ -77,6 +77,33 @@ impl<I: fmt::Debug, F> fmt::Debug for Map<I, F> {
77
77
}
78
78
}
79
79
80
+ struct MapFold < F , G > ( F , G ) ;
81
+ struct MapTryFold < F , G > ( F , G ) ;
82
+
83
+ impl_const_closure ! {
84
+ impl <F , T , B , G , Acc > const FnMut for MapFold <F , G >
85
+ where
86
+ F : ~const FnMut ( T ) -> B ,
87
+ F : ~const Drop ,
88
+ G : ~const FnMut ( Acc , B ) -> Acc ,
89
+ G : ~const Drop ,
90
+ = |& mut self , acc: Acc , elt: T | -> Acc {
91
+ self . 1 ( acc, self . 0 ( elt) )
92
+ } ;
93
+ }
94
+
95
+ impl_const_closure ! {
96
+ impl <F , T , B , G , R , Acc > const FnMut for MapTryFold <F , G >
97
+ where
98
+ F : ~const FnMut ( T ) -> B ,
99
+ F : ~const Drop ,
100
+ G : ~const FnMut ( Acc , B ) -> R ,
101
+ G : ~const Drop ,
102
+ = |& mut self , acc: Acc , elt: T | -> R {
103
+ self . 1 ( acc, self . 0 ( elt) )
104
+ } ;
105
+ }
106
+
80
107
fn map_fold < T , B , Acc > (
81
108
mut f : impl FnMut ( T ) -> B ,
82
109
mut g : impl FnMut ( Acc , B ) -> Acc ,
@@ -92,9 +119,10 @@ fn map_try_fold<'a, T, B, Acc, R>(
92
119
}
93
120
94
121
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
95
- impl < B , I : Iterator , F > Iterator for Map < I , F >
122
+ #[ rustc_const_unstable( feature = "const_iter" , issue = "none" ) ]
123
+ impl < B , I : ~const Iterator , F > const Iterator for Map < I , F >
96
124
where
97
- F : FnMut ( I :: Item ) -> B ,
125
+ F : ~ const FnMut ( I :: Item ) -> B + ~ const Drop ,
98
126
{
99
127
type Item = B ;
100
128
@@ -111,17 +139,21 @@ where
111
139
fn try_fold < Acc , G , R > ( & mut self , init : Acc , g : G ) -> R
112
140
where
113
141
Self : Sized ,
114
- G : FnMut ( Acc , Self :: Item ) -> R ,
115
- R : Try < Output = Acc > ,
142
+ G : ~const FnMut ( Acc , B ) -> R ,
143
+ G : ~const Drop ,
144
+ R : ~const Try < Output = Acc > ,
145
+ R : ~const FromResidual ,
116
146
{
117
- self . iter . try_fold ( init, map_try_fold ( & mut self . f , g) )
147
+ self . iter . try_fold ( init, MapTryFold ( & mut self . f , g) )
118
148
}
119
149
120
150
fn fold < Acc , G > ( self , init : Acc , g : G ) -> Acc
121
151
where
122
- G : FnMut ( Acc , Self :: Item ) -> Acc ,
152
+ I : ~const Drop ,
153
+ G : ~const FnMut ( Acc , B ) -> Acc ,
154
+ G : ~const Drop ,
123
155
{
124
- self . iter . fold ( init, map_fold ( self . f , g) )
156
+ self . iter . fold ( init, MapFold ( self . f , g) )
125
157
}
126
158
127
159
#[ doc( hidden) ]
0 commit comments