@@ -153,6 +153,23 @@ pub fn list_extend(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
153
153
Ok ( vm. get_none ( ) )
154
154
}
155
155
156
+ fn list_index ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
157
+ trace ! ( "list.index called with: {:?}" , args) ;
158
+ arg_check ! (
159
+ vm,
160
+ args,
161
+ required = [ ( list, Some ( vm. ctx. list_type( ) ) ) , ( needle, None ) ]
162
+ ) ;
163
+ for ( index, element) in get_elements ( list) . iter ( ) . enumerate ( ) {
164
+ let py_equal = vm. call_method ( needle, "__eq__" , vec ! [ element. clone( ) ] ) ?;
165
+ if objbool:: get_value ( & py_equal) {
166
+ return Ok ( vm. context ( ) . new_int ( index. to_bigint ( ) . unwrap ( ) ) ) ;
167
+ }
168
+ }
169
+ let needle_str = objstr:: get_value ( & vm. to_str ( needle) . unwrap ( ) ) ;
170
+ Err ( vm. new_value_error ( format ! ( "'{}' is not in list" , needle_str) ) )
171
+ }
172
+
156
173
fn list_len ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
157
174
trace ! ( "list.len called with: {:?}" , args) ;
158
175
arg_check ! ( vm, args, required = [ ( list, Some ( vm. ctx. list_type( ) ) ) ] ) ;
@@ -283,6 +300,7 @@ pub fn init(context: &PyContext) {
283
300
context. set_attr ( & list_type, "clear" , context. new_rustfunc ( list_clear) ) ;
284
301
context. set_attr ( & list_type, "count" , context. new_rustfunc ( list_count) ) ;
285
302
context. set_attr ( & list_type, "extend" , context. new_rustfunc ( list_extend) ) ;
303
+ context. set_attr ( & list_type, "index" , context. new_rustfunc ( list_index) ) ;
286
304
context. set_attr ( & list_type, "reverse" , context. new_rustfunc ( list_reverse) ) ;
287
305
context. set_attr ( & list_type, "sort" , context. new_rustfunc ( list_sort) ) ;
288
306
}
0 commit comments