@@ -7,7 +7,14 @@ use std::marker::PhantomData;
77
88/// A built-in Python function.
99// PyCFunction in CPython
10- pub type PyNativeFn = py_dyn_fn ! ( dyn Fn ( & VirtualMachine , FuncArgs ) -> PyResult ) ;
10+ pub trait PyNativeFn :
11+ Fn ( & VirtualMachine , FuncArgs ) -> PyResult + PyThreadingConstraint + ' static
12+ {
13+ }
14+ impl < F : Fn ( & VirtualMachine , FuncArgs ) -> PyResult + PyThreadingConstraint + ' static > PyNativeFn
15+ for F
16+ {
17+ }
1118
1219/// Implemented by types that are or can generate built-in functions.
1320///
@@ -34,40 +41,44 @@ pub trait IntoPyNativeFn<Kind>: Sized + PyThreadingConstraint + 'static {
3441 /// `IntoPyNativeFn::into_func()` generates a PyNativeFn that performs the
3542 /// appropriate type and arity checking, any requested conversions, and then if
3643 /// successful calls the function with the extracted parameters.
37- fn into_func ( self ) -> & ' static PyNativeFn {
38- let boxed = Box :: new ( move |vm : & VirtualMachine , args| self . call ( vm, args) ) ;
39- Box :: leak ( boxed)
44+ fn into_func ( self ) -> impl PyNativeFn {
45+ into_func ( self )
4046 }
47+ }
4148
42- /// Equivalent to `into_func()`, but accessible as a constant. This is only
43- /// valid if this function is zero-sized, i.e. that
44- /// `std::mem::size_of::<F>() == 0`. If it isn't, use of this constant will
45- /// raise a compile error.
46- const STATIC_FUNC : & ' static PyNativeFn = {
47- if std:: mem:: size_of :: < Self > ( ) == 0 {
48- & |vm, args| {
49- // SAFETY: we just confirmed that Self is zero-sized, so there
50- // aren't any bytes in it that could be uninit.
51- #[ allow( clippy:: uninit_assumed_init) ]
52- let f = unsafe { std:: mem:: MaybeUninit :: < Self > :: uninit ( ) . assume_init ( ) } ;
53- f. call ( vm, args)
49+ const fn into_func < F : IntoPyNativeFn < Kind > , Kind > ( f : F ) -> impl PyNativeFn {
50+ move |vm : & VirtualMachine , args| f. call ( vm, args)
51+ }
52+
53+ const fn zst_ref_out_of_thin_air < T : ' static > ( x : T ) -> & ' static T {
54+ // if T is zero-sized, there's no issue forgetting it - even if it does have a Drop impl, it
55+ // would never get called anyway if we consider this semantically a Box::leak(Box::new(x))-type
56+ // operation. if T isn't zero-sized, we don't have to worry about it because we'll fail to compile.
57+ std:: mem:: forget ( x) ;
58+ trait Zst : Sized + ' static {
59+ const THIN_AIR : & ' static Self = {
60+ if std:: mem:: size_of :: < Self > ( ) == 0 {
61+ // SAFETY: we just confirmed that Self is zero-sized, so we can
62+ // pull a value of it out of thin air.
63+ unsafe { std:: ptr:: NonNull :: < Self > :: dangling ( ) . as_ref ( ) }
64+ } else {
65+ panic ! ( "can't use a non-zero-sized type here" )
5466 }
55- } else {
56- panic ! ( "function must be zero-sized to access STATIC_FUNC" )
57- }
58- } ;
67+ } ;
68+ }
69+ impl < T : ' static > Zst for T { }
70+ < T as Zst > :: THIN_AIR
5971}
6072
6173/// Get the [`STATIC_FUNC`](IntoPyNativeFn::STATIC_FUNC) of the passed function. The same
6274/// requirements of zero-sizedness apply, see that documentation for details.
75+ ///
76+ /// Equivalent to [`IntoPyNativeFn::into_func()`], but usable in a const context. This is only
77+ /// valid if the function is zero-sized, i.e. that `std::mem::size_of::<F>() == 0`. If you call
78+ /// this function with a non-zero-sized function, it will raise a compile error.
6379#[ inline( always) ]
64- pub const fn static_func < Kind , F : IntoPyNativeFn < Kind > > ( f : F ) -> & ' static PyNativeFn {
65- // if f is zero-sized, there's no issue forgetting it - even if a capture of f does have a Drop
66- // impl, it would never get called anyway. If you passed it to into_func, it would just get
67- // Box::leak'd, and as a 'static reference it'll never be dropped. and if f isn't zero-sized,
68- // we'll never reach this point anyway because we'll fail to compile.
69- std:: mem:: forget ( f) ;
70- F :: STATIC_FUNC
80+ pub const fn static_func < Kind , F : IntoPyNativeFn < Kind > > ( f : F ) -> & ' static dyn PyNativeFn {
81+ zst_ref_out_of_thin_air ( into_func ( f) )
7182}
7283
7384// TODO: once higher-rank trait bounds are stabilized, remove the `Kind` type
@@ -207,16 +218,16 @@ into_py_native_fn_tuple!(
207218#[ cfg( test) ]
208219mod tests {
209220 use super :: * ;
221+ use std:: mem:: size_of_val;
210222
211223 #[ test]
212224 fn test_into_native_fn_noalloc ( ) {
213- let check_zst = |f : & ' static PyNativeFn | assert_eq ! ( std:: mem:: size_of_val( f) , 0 ) ;
214225 fn py_func ( _b : bool , _vm : & crate :: VirtualMachine ) -> i32 {
215226 1
216227 }
217- check_zst ( py_func. into_func ( ) ) ;
228+ assert_eq ! ( size_of_val ( & py_func. into_func( ) ) , 0 ) ;
218229 let empty_closure = || "foo" . to_owned ( ) ;
219- check_zst ( empty_closure. into_func ( ) ) ;
220- check_zst ( static_func ( empty_closure) ) ;
230+ assert_eq ! ( size_of_val ( & empty_closure. into_func( ) ) , 0 ) ;
231+ assert_eq ! ( size_of_val ( static_func( empty_closure) ) , 0 ) ;
221232 }
222233}
0 commit comments