@@ -290,6 +290,69 @@ impl PyItertoolsTakewhile {
290
290
}
291
291
}
292
292
293
+ #[ pyclass]
294
+ #[ derive( Debug ) ]
295
+ struct PyItertoolsDropwhile {
296
+ predicate : PyObjectRef ,
297
+ iterable : PyObjectRef ,
298
+ start_flag : RefCell < bool > ,
299
+ }
300
+
301
+ impl PyValue for PyItertoolsDropwhile {
302
+ fn class ( vm : & VirtualMachine ) -> PyClassRef {
303
+ vm. class ( "itertools" , "dropwhile" )
304
+ }
305
+ }
306
+
307
+ #[ pyimpl]
308
+ impl PyItertoolsDropwhile {
309
+ #[ pymethod( name = "__new__" ) ]
310
+ #[ allow( clippy:: new_ret_no_self) ]
311
+ fn new (
312
+ _cls : PyClassRef ,
313
+ predicate : PyObjectRef ,
314
+ iterable : PyObjectRef ,
315
+ vm : & VirtualMachine ,
316
+ ) -> PyResult {
317
+ let iter = get_iter ( vm, & iterable) ?;
318
+
319
+ Ok ( PyItertoolsDropwhile {
320
+ predicate,
321
+ iterable : iter,
322
+ start_flag : RefCell :: new ( false ) ,
323
+ }
324
+ . into_ref ( vm)
325
+ . into_object ( ) )
326
+ }
327
+
328
+ #[ pymethod( name = "__next__" ) ]
329
+ fn next ( & self , vm : & VirtualMachine ) -> PyResult {
330
+ let predicate = & self . predicate ;
331
+ let iterable = & self . iterable ;
332
+
333
+ if !* self . start_flag . borrow_mut ( ) {
334
+ loop {
335
+ let obj = call_next ( vm, iterable) ?;
336
+ let pred_value = vm. invoke ( predicate, vec ! [ obj. clone( ) ] ) ?;
337
+ if !objbool:: boolval ( vm, pred_value) ? {
338
+ * self . start_flag . borrow_mut ( ) = true ;
339
+ return Ok ( obj) ;
340
+ }
341
+ }
342
+ }
343
+
344
+ loop {
345
+ let obj = call_next ( vm, iterable) ?;
346
+ return Ok ( obj) ;
347
+ }
348
+ }
349
+
350
+ #[ pymethod( name = "__iter__" ) ]
351
+ fn iter ( zelf : PyRef < Self > , _vm : & VirtualMachine ) -> PyRef < Self > {
352
+ zelf
353
+ }
354
+ }
355
+
293
356
#[ pyclass( name = "islice" ) ]
294
357
#[ derive( Debug ) ]
295
358
struct PyItertoolsIslice {
@@ -484,6 +547,9 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
484
547
let count = ctx. new_class ( "count" , ctx. object ( ) ) ;
485
548
PyItertoolsCount :: extend_class ( ctx, & count) ;
486
549
550
+ let dropwhile = ctx. new_class ( "dropwhile" , ctx. object ( ) ) ;
551
+ PyItertoolsDropwhile :: extend_class ( ctx, & dropwhile) ;
552
+
487
553
let repeat = ctx. new_class ( "repeat" , ctx. object ( ) ) ;
488
554
PyItertoolsRepeat :: extend_class ( ctx, & repeat) ;
489
555
@@ -500,6 +566,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
500
566
py_module ! ( vm, "itertools" , {
501
567
"chain" => chain,
502
568
"count" => count,
569
+ "dropwhile" => dropwhile,
503
570
"repeat" => repeat,
504
571
"starmap" => starmap,
505
572
"takewhile" => takewhile,
0 commit comments