Skip to content

Commit 53f54e4

Browse files
Merge pull request RustPython#546 from rickyhan/master
Use closure instead of function pointer for StdlibInitFunc
2 parents f8b9d19 + 432b9df commit 53f54e4

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

vm/src/stdlib/mod.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,33 @@ mod os;
2121

2222
use crate::pyobject::{PyContext, PyObjectRef};
2323

24-
pub type StdlibInitFunc = fn(&PyContext) -> PyObjectRef;
24+
pub type StdlibInitFunc = Box<dyn Fn(&PyContext) -> PyObjectRef>;
2525

2626
pub fn get_module_inits() -> HashMap<String, StdlibInitFunc> {
2727
let mut modules = HashMap::new();
28-
modules.insert("ast".to_string(), ast::mk_module as StdlibInitFunc);
29-
modules.insert("dis".to_string(), dis::mk_module as StdlibInitFunc);
30-
modules.insert("json".to_string(), json::mk_module as StdlibInitFunc);
31-
modules.insert("keyword".to_string(), keyword::mk_module as StdlibInitFunc);
32-
modules.insert("math".to_string(), math::mk_module as StdlibInitFunc);
33-
modules.insert("re".to_string(), re::mk_module as StdlibInitFunc);
34-
modules.insert("random".to_string(), random::mk_module as StdlibInitFunc);
35-
modules.insert("string".to_string(), string::mk_module as StdlibInitFunc);
36-
modules.insert("struct".to_string(), pystruct::mk_module as StdlibInitFunc);
37-
modules.insert("time".to_string(), time_module::mk_module as StdlibInitFunc);
3828
modules.insert(
39-
"tokenize".to_string(),
40-
tokenize::mk_module as StdlibInitFunc,
29+
"ast".to_string(),
30+
Box::new(ast::mk_module) as StdlibInitFunc,
4131
);
42-
modules.insert("types".to_string(), types::mk_module as StdlibInitFunc);
43-
modules.insert("_weakref".to_string(), weakref::mk_module as StdlibInitFunc);
32+
modules.insert("dis".to_string(), Box::new(dis::mk_module));
33+
modules.insert("json".to_string(), Box::new(json::mk_module));
34+
modules.insert("keyword".to_string(), Box::new(keyword::mk_module));
35+
modules.insert("math".to_string(), Box::new(math::mk_module));
36+
modules.insert("re".to_string(), Box::new(re::mk_module));
37+
modules.insert("random".to_string(), Box::new(random::mk_module));
38+
modules.insert("string".to_string(), Box::new(string::mk_module));
39+
modules.insert("struct".to_string(), Box::new(pystruct::mk_module));
40+
modules.insert("time".to_string(), Box::new(time_module::mk_module));
41+
modules.insert("tokenize".to_string(), Box::new(tokenize::mk_module));
42+
modules.insert("types".to_string(), Box::new(types::mk_module));
43+
modules.insert("_weakref".to_string(), Box::new(weakref::mk_module));
4444

4545
// disable some modules on WASM
4646
#[cfg(not(target_arch = "wasm32"))]
4747
{
48-
modules.insert("io".to_string(), io::mk_module as StdlibInitFunc);
49-
modules.insert("os".to_string(), os::mk_module as StdlibInitFunc);
50-
modules.insert("socket".to_string(), socket::mk_module as StdlibInitFunc);
48+
modules.insert("io".to_string(), Box::new(io::mk_module));
49+
modules.insert("os".to_string(), Box::new(os::mk_module));
50+
modules.insert("socket".to_string(), Box::new(socket::mk_module));
5151
}
5252

5353
modules

wasm/lib/src/browser_module.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,5 +134,6 @@ pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
134134
}
135135

136136
pub fn setup_browser_module(vm: &mut VirtualMachine) {
137-
vm.stdlib_inits.insert(BROWSER_NAME.to_string(), mk_module);
137+
vm.stdlib_inits
138+
.insert(BROWSER_NAME.to_string(), Box::new(mk_module));
138139
}

0 commit comments

Comments
 (0)