1
- use std:: cell:: RefCell ;
1
+ use std:: cell:: { Cell , RefCell } ;
2
2
use std:: cmp:: Ordering ;
3
3
use std:: ops:: { AddAssign , SubAssign } ;
4
4
@@ -12,7 +12,7 @@ use crate::obj::objint::{PyInt, PyIntRef};
12
12
use crate :: obj:: objiter:: { call_next, get_iter, new_stop_iteration} ;
13
13
use crate :: obj:: objtype;
14
14
use crate :: obj:: objtype:: PyClassRef ;
15
- use crate :: pyobject:: { IdProtocol , PyClassImpl , PyObjectRef , PyRef , PyResult , PyValue } ;
15
+ use crate :: pyobject:: { IdProtocol , PyCallable , PyClassImpl , PyObjectRef , PyRef , PyResult , PyValue } ;
16
16
use crate :: vm:: VirtualMachine ;
17
17
18
18
#[ pyclass( name = "chain" ) ]
@@ -293,9 +293,9 @@ impl PyItertoolsTakewhile {
293
293
#[ pyclass]
294
294
#[ derive( Debug ) ]
295
295
struct PyItertoolsDropwhile {
296
- predicate : PyObjectRef ,
296
+ predicate : PyCallable ,
297
297
iterable : PyObjectRef ,
298
- start_flag : RefCell < bool > ,
298
+ start_flag : Cell < bool > ,
299
299
}
300
300
301
301
impl PyValue for PyItertoolsDropwhile {
@@ -304,47 +304,45 @@ impl PyValue for PyItertoolsDropwhile {
304
304
}
305
305
}
306
306
307
+ type PyItertoolsDropwhileRef = PyRef < PyItertoolsDropwhile > ;
308
+
307
309
#[ pyimpl]
308
310
impl PyItertoolsDropwhile {
309
311
#[ pymethod( name = "__new__" ) ]
310
312
#[ allow( clippy:: new_ret_no_self) ]
311
313
fn new (
312
- _cls : PyClassRef ,
313
- predicate : PyObjectRef ,
314
+ cls : PyClassRef ,
315
+ predicate : PyCallable ,
314
316
iterable : PyObjectRef ,
315
317
vm : & VirtualMachine ,
316
- ) -> PyResult {
318
+ ) -> PyResult < PyItertoolsDropwhileRef > {
317
319
let iter = get_iter ( vm, & iterable) ?;
318
320
319
- Ok ( PyItertoolsDropwhile {
321
+ PyItertoolsDropwhile {
320
322
predicate,
321
323
iterable : iter,
322
- start_flag : RefCell :: new ( false ) ,
324
+ start_flag : Cell :: new ( false ) ,
323
325
}
324
- . into_ref ( vm)
325
- . into_object ( ) )
326
+ . into_ref_with_type ( vm, cls)
326
327
}
327
328
328
329
#[ pymethod( name = "__next__" ) ]
329
330
fn next ( & self , vm : & VirtualMachine ) -> PyResult {
330
331
let predicate = & self . predicate ;
331
332
let iterable = & self . iterable ;
332
333
333
- if !* self . start_flag . borrow_mut ( ) {
334
+ if !self . start_flag . get ( ) {
334
335
loop {
335
336
let obj = call_next ( vm, iterable) ?;
336
- let pred_value = vm. invoke ( predicate, vec ! [ obj. clone( ) ] ) ?;
337
+ let pred = predicate. clone ( ) ;
338
+ let pred_value = vm. invoke ( & pred. into_object ( ) , vec ! [ obj. clone( ) ] ) ?;
337
339
if !objbool:: boolval ( vm, pred_value) ? {
338
- * self . start_flag . borrow_mut ( ) = true ;
340
+ self . start_flag . set ( true ) ;
339
341
return Ok ( obj) ;
340
342
}
341
343
}
342
344
}
343
-
344
- loop {
345
- let obj = call_next ( vm, iterable) ?;
346
- return Ok ( obj) ;
347
- }
345
+ call_next ( vm, iterable)
348
346
}
349
347
350
348
#[ pymethod( name = "__iter__" ) ]
0 commit comments