@@ -99,20 +99,25 @@ macro_rules! def_array_enum {
99
99
fn count( & self , obj: PyObjectRef , vm: & VirtualMachine ) -> PyResult <usize > {
100
100
match self {
101
101
$( 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
+ } ) )
104
105
} ) *
105
106
}
106
107
}
107
108
108
109
fn remove( & mut self , obj: PyObjectRef , vm: & VirtualMachine ) -> PyResult <( ) >{
109
110
match self {
110
111
$( 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( ) ) )
116
121
}
117
122
} ) *
118
123
}
@@ -147,8 +152,9 @@ macro_rules! def_array_enum {
147
152
fn index( & self , x: PyObjectRef , vm: & VirtualMachine ) -> PyResult <Option <usize >> {
148
153
match self {
149
154
$( 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
+ } ) )
152
158
} ) *
153
159
}
154
160
}
0 commit comments