@@ -99,20 +99,25 @@ macro_rules! def_array_enum {
9999 fn count( & self , obj: PyObjectRef , vm: & VirtualMachine ) -> PyResult <usize > {
100100 match self {
101101 $( ArrayContentType :: $n( v) => {
102- let val = $t:: try_from_object( vm, obj) ?;
103- Ok ( v. iter( ) . filter( |&&a| a == val) . count( ) )
102+ Ok ( <Option <$t>>:: try_from_object( vm, obj) ?. map_or( 0 , |val| {
103+ v. iter( ) . filter( |&&a| a == val) . count( )
104+ } ) )
104105 } ) *
105106 }
106107 }
107108
108109 fn remove( & mut self , obj: PyObjectRef , vm: & VirtualMachine ) -> PyResult <( ) >{
109110 match self {
110111 $( ArrayContentType :: $n( v) => {
111- let val = $t:: try_from_object( vm, obj) ?;
112- if let Some ( pos) = v. iter( ) . position( |& a| a == val) {
113- v. remove( pos) ;
114- } else {
115- return Err ( vm. new_value_error( "array.remove(x): x not in array" . to_owned( ) ) ) ;
112+ let pos = <Option <$t>>:: try_from_object( vm, obj) ?. map_or( None , |val| {
113+ v. iter( ) . position( |& a| a == val)
114+ } ) ;
115+
116+ match pos {
117+ Some ( x) => {
118+ v. remove( x) ;
119+ } ,
120+ None => return Err ( vm. new_value_error( "array.remove(x): x not in array" . to_owned( ) ) )
116121 }
117122 } ) *
118123 }
@@ -147,8 +152,9 @@ macro_rules! def_array_enum {
147152 fn index( & self , x: PyObjectRef , vm: & VirtualMachine ) -> PyResult <Option <usize >> {
148153 match self {
149154 $( ArrayContentType :: $n( v) => {
150- let val = $t:: try_from_object( vm, x) ?;
151- Ok ( v. iter( ) . position( |& a| a == val) )
155+ Ok ( <Option <$t>>:: try_from_object( vm, x) ?. map_or( None , |val| {
156+ v. iter( ) . position( |& a| a == val)
157+ } ) )
152158 } ) *
153159 }
154160 }
0 commit comments