@@ -385,10 +385,10 @@ fn repr_wrapper(zelf: &PyObject, vm: &VirtualMachine) -> PyResult<PyStrRef> {
385
385
386
386
fn hash_wrapper ( zelf : & PyObject , vm : & VirtualMachine ) -> PyResult < PyHash > {
387
387
let hash_obj = vm. call_special_method ( zelf. to_owned ( ) , identifier ! ( vm, __hash__) , ( ) ) ?;
388
- match hash_obj . payload_if_subclass :: < PyInt > ( vm ) {
389
- Some ( py_int ) => Ok ( rustpython_common :: hash :: hash_bigint ( py_int . as_bigint ( ) ) ) ,
390
- None => Err ( vm. new_type_error ( "__hash__ method should return an integer" . to_owned ( ) ) ) ,
391
- }
388
+ let py_int = hash_obj
389
+ . payload_if_subclass :: < PyInt > ( vm )
390
+ . ok_or_else ( || vm. new_type_error ( "__hash__ method should return an integer" . to_owned ( ) ) ) ? ;
391
+ Ok ( rustpython_common :: hash :: hash_bigint ( py_int . as_bigint ( ) ) )
392
392
}
393
393
394
394
/// Marks a type as unhashable. Similar to PyObject_HashNotImplemented in CPython
@@ -884,11 +884,10 @@ pub trait Destructor: PyPayload {
884
884
#[ inline] // for __del__
885
885
#[ pyslot]
886
886
fn slot_del ( zelf : & PyObject , vm : & VirtualMachine ) -> PyResult < ( ) > {
887
- if let Some ( zelf) = zelf. downcast_ref ( ) {
888
- Self :: del ( zelf, vm)
889
- } else {
890
- Err ( vm. new_type_error ( "unexpected payload for __del__" . to_owned ( ) ) )
891
- }
887
+ let zelf = zelf
888
+ . downcast_ref ( )
889
+ . ok_or_else ( || vm. new_type_error ( "unexpected payload for __del__" . to_owned ( ) ) ) ?;
890
+ Self :: del ( zelf, vm)
892
891
}
893
892
894
893
#[ pymethod]
@@ -906,11 +905,10 @@ pub trait Callable: PyPayload {
906
905
#[ inline]
907
906
#[ pyslot]
908
907
fn slot_call ( zelf : & PyObject , args : FuncArgs , vm : & VirtualMachine ) -> PyResult {
909
- if let Some ( zelf) = zelf. downcast_ref ( ) {
910
- Self :: call ( zelf, args. bind ( vm) ?, vm)
911
- } else {
912
- Err ( vm. new_type_error ( "unexpected payload for __call__" . to_owned ( ) ) )
913
- }
908
+ let zelf = zelf
909
+ . downcast_ref ( )
910
+ . ok_or_else ( || vm. new_type_error ( "unexpected payload for __call__" . to_owned ( ) ) ) ?;
911
+ Self :: call ( zelf, args. bind ( vm) ?, vm)
914
912
}
915
913
916
914
#[ inline]
@@ -994,11 +992,10 @@ pub trait Hashable: PyPayload {
994
992
#[ inline]
995
993
#[ pyslot]
996
994
fn slot_hash ( zelf : & PyObject , vm : & VirtualMachine ) -> PyResult < PyHash > {
997
- if let Some ( zelf) = zelf. downcast_ref ( ) {
998
- Self :: hash ( zelf, vm)
999
- } else {
1000
- Err ( vm. new_type_error ( "unexpected payload for __hash__" . to_owned ( ) ) )
1001
- }
995
+ let zelf = zelf
996
+ . downcast_ref ( )
997
+ . ok_or_else ( || vm. new_type_error ( "unexpected payload for __hash__" . to_owned ( ) ) ) ?;
998
+ Self :: hash ( zelf, vm)
1002
999
}
1003
1000
1004
1001
#[ inline]
@@ -1015,11 +1012,10 @@ pub trait Representable: PyPayload {
1015
1012
#[ inline]
1016
1013
#[ pyslot]
1017
1014
fn slot_repr ( zelf : & PyObject , vm : & VirtualMachine ) -> PyResult < PyStrRef > {
1018
- if let Some ( zelf) = zelf. downcast_ref ( ) {
1019
- Self :: repr ( zelf, vm)
1020
- } else {
1021
- Err ( vm. new_type_error ( "unexpected payload for __repr__" . to_owned ( ) ) )
1022
- }
1015
+ let zelf = zelf
1016
+ . downcast_ref ( )
1017
+ . ok_or_else ( || vm. new_type_error ( "unexpected payload for __repr__" . to_owned ( ) ) ) ?;
1018
+ Self :: repr ( zelf, vm)
1023
1019
}
1024
1020
1025
1021
#[ inline]
@@ -1028,6 +1024,7 @@ pub trait Representable: PyPayload {
1028
1024
Self :: slot_repr ( & zelf, vm)
1029
1025
}
1030
1026
1027
+ #[ inline]
1031
1028
fn repr ( zelf : & Py < Self > , vm : & VirtualMachine ) -> PyResult < PyStrRef > {
1032
1029
let repr = Self :: repr_str ( zelf, vm) ?;
1033
1030
Ok ( vm. ctx . new_str ( repr) )
@@ -1046,14 +1043,13 @@ pub trait Comparable: PyPayload {
1046
1043
op : PyComparisonOp ,
1047
1044
vm : & VirtualMachine ,
1048
1045
) -> PyResult < Either < PyObjectRef , PyComparisonValue > > {
1049
- if let Some ( zelf) = zelf. downcast_ref ( ) {
1050
- Self :: cmp ( zelf, other, op, vm) . map ( Either :: B )
1051
- } else {
1052
- Err ( vm. new_type_error ( format ! (
1046
+ let zelf = zelf. downcast_ref ( ) . ok_or_else ( || {
1047
+ vm. new_type_error ( format ! (
1053
1048
"unexpected payload for {}" ,
1054
1049
op. method_name( & vm. ctx) . as_str( )
1055
- ) ) )
1056
- }
1050
+ ) )
1051
+ } ) ?;
1052
+ Self :: cmp ( zelf, other, op, vm) . map ( Either :: B )
1057
1053
}
1058
1054
1059
1055
fn cmp (
@@ -1212,23 +1208,18 @@ impl PyComparisonOp {
1212
1208
Self :: Ne => false ,
1213
1209
_ => return None ,
1214
1210
} ;
1215
- if f ( ) {
1216
- Some ( eq)
1217
- } else {
1218
- None
1219
- }
1211
+ f ( ) . then_some ( eq)
1220
1212
}
1221
1213
}
1222
1214
1223
1215
#[ pyclass]
1224
1216
pub trait GetAttr : PyPayload {
1225
1217
#[ pyslot]
1226
1218
fn slot_getattro ( obj : & PyObject , name : PyStrRef , vm : & VirtualMachine ) -> PyResult {
1227
- if let Some ( zelf) = obj. downcast_ref :: < Self > ( ) {
1228
- Self :: getattro ( zelf, name, vm)
1229
- } else {
1230
- Err ( vm. new_type_error ( "unexpected payload for __getattribute__" . to_owned ( ) ) )
1231
- }
1219
+ let zelf = obj. downcast_ref ( ) . ok_or_else ( || {
1220
+ vm. new_type_error ( "unexpected payload for __getattribute__" . to_owned ( ) )
1221
+ } ) ?;
1222
+ Self :: getattro ( zelf, name, vm)
1232
1223
}
1233
1224
1234
1225
fn getattro ( zelf : & Py < Self > , name : PyStrRef , vm : & VirtualMachine ) -> PyResult ;
@@ -1250,11 +1241,10 @@ pub trait SetAttr: PyPayload {
1250
1241
value : PySetterValue ,
1251
1242
vm : & VirtualMachine ,
1252
1243
) -> PyResult < ( ) > {
1253
- if let Some ( zelf) = obj. downcast_ref :: < Self > ( ) {
1254
- Self :: setattro ( zelf, name, value, vm)
1255
- } else {
1256
- Err ( vm. new_type_error ( "unexpected payload for __setattr__" . to_owned ( ) ) )
1257
- }
1244
+ let zelf = obj
1245
+ . downcast_ref :: < Self > ( )
1246
+ . ok_or_else ( || vm. new_type_error ( "unexpected payload for __setattr__" . to_owned ( ) ) ) ?;
1247
+ Self :: setattro ( zelf, name, value, vm)
1258
1248
}
1259
1249
1260
1250
fn setattro (
@@ -1423,11 +1413,10 @@ pub trait Iterable: PyPayload {
1423
1413
#[ pyslot]
1424
1414
#[ pymethod( name = "__iter__" ) ]
1425
1415
fn slot_iter ( zelf : PyObjectRef , vm : & VirtualMachine ) -> PyResult {
1426
- if let Ok ( zelf) = zelf. downcast ( ) {
1427
- Self :: iter ( zelf, vm)
1428
- } else {
1429
- Err ( vm. new_type_error ( "unexpected payload for __iter__" . to_owned ( ) ) )
1430
- }
1416
+ let zelf = zelf
1417
+ . downcast ( )
1418
+ . map_err ( |_| vm. new_type_error ( "unexpected payload for __iter__" . to_owned ( ) ) ) ?;
1419
+ Self :: iter ( zelf, vm)
1431
1420
}
1432
1421
1433
1422
fn iter ( zelf : PyRef < Self > , vm : & VirtualMachine ) -> PyResult ;
@@ -1438,11 +1427,10 @@ pub trait Iterable: PyPayload {
1438
1427
pub trait IterNext : PyPayload + Iterable {
1439
1428
#[ pyslot]
1440
1429
fn slot_iternext ( zelf : & PyObject , vm : & VirtualMachine ) -> PyResult < PyIterReturn > {
1441
- if let Some ( zelf) = zelf. downcast_ref ( ) {
1442
- Self :: next ( zelf, vm)
1443
- } else {
1444
- Err ( vm. new_type_error ( "unexpected payload for __next__" . to_owned ( ) ) )
1445
- }
1430
+ let zelf = zelf
1431
+ . downcast_ref ( )
1432
+ . ok_or_else ( || vm. new_type_error ( "unexpected payload for __next__" . to_owned ( ) ) ) ?;
1433
+ Self :: next ( zelf, vm)
1446
1434
}
1447
1435
1448
1436
fn next ( zelf : & Py < Self > , vm : & VirtualMachine ) -> PyResult < PyIterReturn > ;
0 commit comments