@@ -9,7 +9,8 @@ use crate::exceptions::PyBaseExceptionRef;
99use crate :: obj:: objtuple:: PyTupleRef ;
1010use crate :: obj:: objtype:: { isinstance, PyClassRef } ;
1111use crate :: pyobject:: {
12- BorrowValue , IntoPyResult , PyObjectRef , PyRef , PyResult , PyValue , TryFromObject , TypeProtocol ,
12+ BorrowValue , IntoPyResult , PyObjectRef , PyRef , PyResult , PyThreadingConstraint , PyValue ,
13+ TryFromObject , TypeProtocol ,
1314} ;
1415use crate :: vm:: VirtualMachine ;
1516
@@ -476,15 +477,8 @@ tuple_from_py_func_args!(A, B, C, D);
476477tuple_from_py_func_args ! ( A , B , C , D , E ) ;
477478tuple_from_py_func_args ! ( A , B , C , D , E , F ) ;
478479
479- // can't use PyThreadingConstraint for this since it's not an auto trait, and therefore we can't
480- // add it ad-hoc to a trait object
481- #[ cfg( not( feature = "threading" ) ) ]
482- pub type NativeFuncObj = dyn Fn ( & VirtualMachine , PyFuncArgs ) -> PyResult + ' static ;
483- #[ cfg( feature = "threading" ) ]
484- pub type NativeFuncObj = dyn Fn ( & VirtualMachine , PyFuncArgs ) -> PyResult + Send + Sync + ' static ;
485-
486480/// A built-in Python function.
487- pub type PyNativeFunc = Box < NativeFuncObj > ;
481+ pub type PyNativeFunc = Box < py_dyn_fn ! ( dyn Fn ( & VirtualMachine , PyFuncArgs ) -> PyResult ) > ;
488482
489483/// Implemented by types that are or can generate built-in functions.
490484///
@@ -500,7 +494,7 @@ pub type PyNativeFunc = Box<NativeFuncObj>;
500494///
501495/// A bare `PyNativeFunc` also implements this trait, allowing the above to be
502496/// done manually, for rare situations that don't fit into this model.
503- pub trait IntoPyNativeFunc < T , R , VM > : Sized + Send + Sync + ' static {
497+ pub trait IntoPyNativeFunc < T , R , VM > : Sized + PyThreadingConstraint + ' static {
504498 fn call ( & self , vm : & VirtualMachine , args : PyFuncArgs ) -> PyResult ;
505499 fn into_func ( self ) -> PyNativeFunc {
506500 Box :: new ( move |vm : & VirtualMachine , args| self . call ( vm, args) )
@@ -509,7 +503,7 @@ pub trait IntoPyNativeFunc<T, R, VM>: Sized + Send + Sync + 'static {
509503
510504impl < F > IntoPyNativeFunc < PyFuncArgs , PyResult , VirtualMachine > for F
511505where
512- F : Fn ( & VirtualMachine , PyFuncArgs ) -> PyResult + ' static + Send + Sync ,
506+ F : Fn ( & VirtualMachine , PyFuncArgs ) -> PyResult + PyThreadingConstraint + ' static ,
513507{
514508 fn call ( & self , vm : & VirtualMachine , args : PyFuncArgs ) -> PyResult {
515509 ( self ) ( vm, args)
@@ -527,7 +521,7 @@ macro_rules! into_py_native_func_tuple {
527521 ( $( ( $n: tt, $T: ident) ) ,* ) => {
528522 impl <F , $( $T, ) * R > IntoPyNativeFunc <( $( OwnedParam <$T>, ) * ) , R , VirtualMachine > for F
529523 where
530- F : Fn ( $( $T, ) * & VirtualMachine ) -> R + ' static + Send + Sync ,
524+ F : Fn ( $( $T, ) * & VirtualMachine ) -> R + PyThreadingConstraint + ' static ,
531525 $( $T: FromArgs , ) *
532526 R : IntoPyResult ,
533527 {
@@ -540,7 +534,7 @@ macro_rules! into_py_native_func_tuple {
540534
541535 impl <F , S , $( $T, ) * R > IntoPyNativeFunc <( RefParam <S >, $( OwnedParam <$T>, ) * ) , R , VirtualMachine > for F
542536 where
543- F : Fn ( & S , $( $T, ) * & VirtualMachine ) -> R + ' static + Send + Sync ,
537+ F : Fn ( & S , $( $T, ) * & VirtualMachine ) -> R + PyThreadingConstraint + ' static ,
544538 S : PyValue ,
545539 $( $T: FromArgs , ) *
546540 R : IntoPyResult ,
@@ -554,7 +548,7 @@ macro_rules! into_py_native_func_tuple {
554548
555549 impl <F , $( $T, ) * R > IntoPyNativeFunc <( $( OwnedParam <$T>, ) * ) , R , ( ) > for F
556550 where
557- F : Fn ( $( $T, ) * ) -> R + ' static + Send + Sync ,
551+ F : Fn ( $( $T, ) * ) -> R + PyThreadingConstraint + ' static ,
558552 $( $T: FromArgs , ) *
559553 R : IntoPyResult ,
560554 {
@@ -567,7 +561,7 @@ macro_rules! into_py_native_func_tuple {
567561
568562 impl <F , S , $( $T, ) * R > IntoPyNativeFunc <( RefParam <S >, $( OwnedParam <$T>, ) * ) , R , ( ) > for F
569563 where
570- F : Fn ( & S , $( $T, ) * ) -> R + ' static + Send + Sync ,
564+ F : Fn ( & S , $( $T, ) * ) -> R + PyThreadingConstraint + ' static ,
571565 S : PyValue ,
572566 $( $T: FromArgs , ) *
573567 R : IntoPyResult ,
@@ -645,18 +639,16 @@ where
645639
646640#[ cfg( test) ]
647641mod tests {
642+ use super :: * ;
643+
648644 #[ test]
649645 fn test_intonativefunc_noalloc ( ) {
650- macro_rules! check_size {
651- ( $f: expr) => {
652- let f = super :: IntoPyNativeFunc :: into_func( py_func) ;
653- assert_eq!( std:: mem:: size_of_val( f. as_ref( ) ) , 0 ) ;
654- } ;
655- }
646+ let check_zst = |f : PyNativeFunc | assert_eq ! ( std:: mem:: size_of_val( f. as_ref( ) ) , 0 ) ;
656647 fn py_func ( _b : bool , _vm : & crate :: VirtualMachine ) -> i32 {
657648 1
658649 }
659- check_size ! ( py_func) ;
660- check_size ! ( || "foo" . to_owned( ) ) ;
650+ check_zst ( py_func. into_func ( ) ) ;
651+ let empty_closure = || "foo" . to_owned ( ) ;
652+ check_zst ( empty_closure. into_func ( ) ) ;
661653 }
662654}
0 commit comments