@@ -19,8 +19,12 @@ unsafe impl Sync for ModuleDefSlot {}
19
19
pub struct ModuleDef {
20
20
// wrapped in UnsafeCell so that Rust compiler treats this as interior mutability
21
21
ffi_def : UnsafeCell < ffi:: PyModuleDef > ,
22
+ initializer : ModuleInitializer ,
22
23
}
23
24
25
+ /// Wrapper to enable initializer to be used in const fns.
26
+ pub struct ModuleInitializer ( pub for <' py > fn ( Python < ' py > , & PyModule ) -> PyResult < ( ) > ) ;
27
+
24
28
unsafe impl Sync for ModuleDef { }
25
29
26
30
impl ModuleDefSlot {
@@ -39,6 +43,7 @@ impl ModuleDef {
39
43
name : & ' static str ,
40
44
doc : & ' static str ,
41
45
slots : * mut ffi:: PyModuleDef_Slot ,
46
+ initializer : ModuleInitializer ,
42
47
) -> Self {
43
48
const INIT : ffi:: PyModuleDef = ffi:: PyModuleDef {
44
49
m_base : ffi:: PyModuleDef_HEAD_INIT ,
@@ -59,7 +64,10 @@ impl ModuleDef {
59
64
..INIT
60
65
} ) ;
61
66
62
- ModuleDef { ffi_def }
67
+ ModuleDef {
68
+ ffi_def,
69
+ initializer,
70
+ }
63
71
}
64
72
/// Return module def
65
73
pub fn module_def ( & ' static self ) -> * mut ffi:: PyModuleDef {
@@ -68,9 +76,11 @@ impl ModuleDef {
68
76
/// Builds a module using user given initializer. Used for [`#[pymodule]`][crate::pymodule].
69
77
pub fn make_module ( & ' static self , py : Python < ' _ > ) -> PyResult < PyObject > {
70
78
let module = unsafe {
71
- Py :: < PyModule > :: from_owned_ptr_or_err ( py, ffi:: PyModule_Create ( self . ffi_def . get ( ) ) ) ?
79
+ let mod_def = self . ffi_def . get ( ) ;
80
+ ( * mod_def) . m_slots = std:: ptr:: null_mut ( ) ;
81
+ Py :: < PyModule > :: from_owned_ptr_or_err ( py, ffi:: PyModule_Create ( mod_def) ) ?
72
82
} ;
73
- // (self.initializer.0)(py, module.as_ref(py))?;
83
+ ( self . initializer . 0 ) ( py, module. as_ref ( py) ) ?;
74
84
Ok ( module. into ( ) )
75
85
}
76
86
/// Implementation of `PyInit_foo` functions generated in [`#[pymodule]`][crate::pymodule]..
0 commit comments