@@ -385,10 +385,10 @@ fn repr_wrapper(zelf: &PyObject, vm: &VirtualMachine) -> PyResult<PyStrRef> {
385385
386386fn hash_wrapper ( zelf : & PyObject , vm : & VirtualMachine ) -> PyResult < PyHash > {
387387 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 ( ) ) )
392392}
393393
394394/// Marks a type as unhashable. Similar to PyObject_HashNotImplemented in CPython
@@ -884,11 +884,10 @@ pub trait Destructor: PyPayload {
884884 #[ inline] // for __del__
885885 #[ pyslot]
886886 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)
892891 }
893892
894893 #[ pymethod]
@@ -906,11 +905,10 @@ pub trait Callable: PyPayload {
906905 #[ inline]
907906 #[ pyslot]
908907 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)
914912 }
915913
916914 #[ inline]
@@ -994,11 +992,10 @@ pub trait Hashable: PyPayload {
994992 #[ inline]
995993 #[ pyslot]
996994 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)
1002999 }
10031000
10041001 #[ inline]
@@ -1015,11 +1012,10 @@ pub trait Representable: PyPayload {
10151012 #[ inline]
10161013 #[ pyslot]
10171014 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)
10231019 }
10241020
10251021 #[ inline]
@@ -1028,6 +1024,7 @@ pub trait Representable: PyPayload {
10281024 Self :: slot_repr ( & zelf, vm)
10291025 }
10301026
1027+ #[ inline]
10311028 fn repr ( zelf : & Py < Self > , vm : & VirtualMachine ) -> PyResult < PyStrRef > {
10321029 let repr = Self :: repr_str ( zelf, vm) ?;
10331030 Ok ( vm. ctx . new_str ( repr) )
@@ -1046,14 +1043,13 @@ pub trait Comparable: PyPayload {
10461043 op : PyComparisonOp ,
10471044 vm : & VirtualMachine ,
10481045 ) -> 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 ! (
10531048 "unexpected payload for {}" ,
10541049 op. method_name( & vm. ctx) . as_str( )
1055- ) ) )
1056- }
1050+ ) )
1051+ } ) ?;
1052+ Self :: cmp ( zelf, other, op, vm) . map ( Either :: B )
10571053 }
10581054
10591055 fn cmp (
@@ -1212,23 +1208,18 @@ impl PyComparisonOp {
12121208 Self :: Ne => false ,
12131209 _ => return None ,
12141210 } ;
1215- if f ( ) {
1216- Some ( eq)
1217- } else {
1218- None
1219- }
1211+ f ( ) . then_some ( eq)
12201212 }
12211213}
12221214
12231215#[ pyclass]
12241216pub trait GetAttr : PyPayload {
12251217 #[ pyslot]
12261218 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)
12321223 }
12331224
12341225 fn getattro ( zelf : & Py < Self > , name : PyStrRef , vm : & VirtualMachine ) -> PyResult ;
@@ -1250,11 +1241,10 @@ pub trait SetAttr: PyPayload {
12501241 value : PySetterValue ,
12511242 vm : & VirtualMachine ,
12521243 ) -> 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)
12581248 }
12591249
12601250 fn setattro (
@@ -1423,11 +1413,10 @@ pub trait Iterable: PyPayload {
14231413 #[ pyslot]
14241414 #[ pymethod( name = "__iter__" ) ]
14251415 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)
14311420 }
14321421
14331422 fn iter ( zelf : PyRef < Self > , vm : & VirtualMachine ) -> PyResult ;
@@ -1438,11 +1427,10 @@ pub trait Iterable: PyPayload {
14381427pub trait IterNext : PyPayload + Iterable {
14391428 #[ pyslot]
14401429 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)
14461434 }
14471435
14481436 fn next ( zelf : & Py < Self > , vm : & VirtualMachine ) -> PyResult < PyIterReturn > ;
0 commit comments