@@ -91,8 +91,96 @@ impl<'a> Iterator for WordListIter<'a> {
91
91
}
92
92
}
93
93
94
+ impl ExactSizeIterator for WordListIter < ' _ > {
95
+ fn len ( & self ) -> usize {
96
+ self . 0 . len ( )
97
+ }
98
+ }
99
+
100
+ impl DoubleEndedIterator for WordListIter < ' _ > {
101
+ fn next_back ( & mut self ) -> Option < Self :: Item > {
102
+ self . 0 . next_back ( ) . map ( String :: as_ref)
103
+ }
104
+ }
105
+
94
106
#[ derive( Debug , Error ) ]
95
107
pub enum WordListError {
96
108
#[ error( "failed to read from {}: {}" , . 0 . display( ) , . 1 ) ]
97
109
FailedToRead ( PathBuf , io:: Error ) ,
98
110
}
111
+
112
+ #[ cfg( feature = "rayon" ) ]
113
+ pub ( crate ) mod rayon {
114
+ use rayon:: iter:: {
115
+ plumbing:: {
116
+ bridge, Consumer , Producer , ProducerCallback , UnindexedConsumer ,
117
+ } ,
118
+ IndexedParallelIterator , ParallelIterator ,
119
+ } ;
120
+
121
+ use super :: { WordList , WordListIter } ;
122
+
123
+ #[ derive( Debug ) ]
124
+ pub struct ParWordListIter < ' a > ( & ' a [ String ] ) ;
125
+
126
+ impl < ' a > ParallelIterator for ParWordListIter < ' a > {
127
+ type Item = & ' a str ;
128
+
129
+ fn drive_unindexed < C > ( self , consumer : C ) -> C :: Result
130
+ where
131
+ C : UnindexedConsumer < Self :: Item > ,
132
+ {
133
+ bridge ( self , consumer)
134
+ }
135
+
136
+ fn opt_len ( & self ) -> Option < usize > {
137
+ Some ( self . 0 . len ( ) )
138
+ }
139
+ }
140
+
141
+ impl < ' a > Producer for ParWordListIter < ' a > {
142
+ type IntoIter = WordListIter < ' a > ;
143
+ type Item = & ' a str ;
144
+
145
+ fn into_iter ( self ) -> Self :: IntoIter {
146
+ WordListIter ( self . 0 . iter ( ) )
147
+ }
148
+
149
+ fn split_at ( self , index : usize ) -> ( Self , Self ) {
150
+ let ( left, right) = self . 0 . split_at ( index) ;
151
+ ( ParWordListIter ( left) , ParWordListIter ( right) )
152
+ }
153
+ }
154
+
155
+ impl IndexedParallelIterator for ParWordListIter < ' _ > {
156
+ fn len ( & self ) -> usize {
157
+ self . 0 . len ( )
158
+ }
159
+
160
+ fn drive < C : Consumer < Self :: Item > > ( self , consumer : C ) -> C :: Result {
161
+ bridge ( self , consumer)
162
+ }
163
+
164
+ fn with_producer < CB > ( self , callback : CB ) -> CB :: Output
165
+ where
166
+ CB : ProducerCallback < Self :: Item > ,
167
+ {
168
+ callback. callback ( self )
169
+ }
170
+ }
171
+
172
+ impl < ' a > rayon:: iter:: IntoParallelIterator for & ' a WordList {
173
+ type Item = & ' a str ;
174
+ type Iter = ParWordListIter < ' a > ;
175
+
176
+ fn into_par_iter ( self ) -> Self :: Iter {
177
+ ParWordListIter ( & self . words )
178
+ }
179
+ }
180
+
181
+ impl WordList {
182
+ pub fn par_iter ( & self ) -> ParWordListIter {
183
+ ParWordListIter ( & self . words )
184
+ }
185
+ }
186
+ }
0 commit comments