@@ -167,6 +167,14 @@ impl PyListRef {
167
167
fn reverse ( self , _vm : & VirtualMachine ) {
168
168
self . elements . borrow_mut ( ) . reverse ( ) ;
169
169
}
170
+
171
+ fn reversed ( self , _vm : & VirtualMachine ) -> PyListReverseIterator {
172
+ let final_position = self . elements . borrow ( ) . len ( ) ;
173
+ PyListReverseIterator {
174
+ position : Cell :: new ( final_position) ,
175
+ list : self ,
176
+ }
177
+ }
170
178
171
179
fn getitem ( self , needle : PyObjectRef , vm : & VirtualMachine ) -> PyResult {
172
180
get_item (
@@ -397,15 +405,15 @@ impl PyListRef {
397
405
. collect ( ) ;
398
406
vm. ctx . new_list ( new_elements)
399
407
}
400
-
408
+
401
409
fn imul ( self , counter : isize , _vm : & VirtualMachine ) -> Self {
402
410
let new_elements = seq_mul ( & self . elements . borrow ( ) . as_slice ( ) , counter)
403
411
. cloned ( )
404
412
. collect ( ) ;
405
413
self . elements . replace ( new_elements) ;
406
414
self
407
415
}
408
-
416
+
409
417
fn count ( self , needle : PyObjectRef , vm : & VirtualMachine ) -> PyResult < usize > {
410
418
let mut count: usize = 0 ;
411
419
for element in self . elements . borrow ( ) . iter ( ) {
@@ -813,6 +821,39 @@ impl PyListIterator {
813
821
}
814
822
}
815
823
824
+ #[ pyclass]
825
+ #[ derive( Debug ) ]
826
+ pub struct PyListReverseIterator {
827
+ pub position : Cell < usize > ,
828
+ pub list : PyListRef ,
829
+ }
830
+
831
+ impl PyValue for PyListReverseIterator {
832
+ fn class ( vm : & VirtualMachine ) -> PyClassRef {
833
+ vm. ctx . listreverseiterator_type ( )
834
+ }
835
+ }
836
+
837
+ #[ pyimpl]
838
+ impl PyListReverseIterator {
839
+ #[ pymethod( name = "__next__" ) ]
840
+ fn next ( & self , vm : & VirtualMachine ) -> PyResult {
841
+ if self . position . get ( ) > 0 {
842
+ let position: usize = self . position . get ( ) - 1 ;
843
+ let ret = self . list . elements . borrow ( ) [ position] . clone ( ) ;
844
+ self . position . set ( position) ;
845
+ Ok ( ret)
846
+ } else {
847
+ Err ( objiter:: new_stop_iteration ( vm) )
848
+ }
849
+ }
850
+
851
+ #[ pymethod( name = "__iter__" ) ]
852
+ fn iter ( zelf : PyRef < Self > , _vm : & VirtualMachine ) -> PyRef < Self > {
853
+ zelf
854
+ }
855
+ }
856
+
816
857
#[ rustfmt:: skip] // to avoid line splitting
817
858
pub fn init ( context : & PyContext ) {
818
859
let list_type = & context. list_type ;
@@ -835,6 +876,7 @@ pub fn init(context: &PyContext) {
835
876
"__getitem__" => context. new_rustfunc( PyListRef :: getitem) ,
836
877
"__iter__" => context. new_rustfunc( PyListRef :: iter) ,
837
878
"__setitem__" => context. new_rustfunc( PyListRef :: setitem) ,
879
+ "__reversed__" => context. new_rustfunc( PyListRef :: reversed) ,
838
880
"__mul__" => context. new_rustfunc( PyListRef :: mul) ,
839
881
"__imul__" => context. new_rustfunc( PyListRef :: imul) ,
840
882
"__len__" => context. new_rustfunc( PyListRef :: len) ,
@@ -856,4 +898,5 @@ pub fn init(context: &PyContext) {
856
898
} ) ;
857
899
858
900
PyListIterator :: extend_class ( context, & context. listiterator_type ) ;
901
+ PyListReverseIterator :: extend_class ( context, & context. listreverseiterator_type ) ;
859
902
}
0 commit comments