Skip to content

Commit e2e3353

Browse files
Add factory impl for boxed fn and remove extra constructor
1 parent 990c294 commit e2e3353

File tree

2 files changed

+9
-13
lines changed

2 files changed

+9
-13
lines changed

vm/src/pyobject.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -588,16 +588,6 @@ impl PyContext {
588588
)
589589
}
590590

591-
pub fn new_rustfunc_from_box(
592-
&self,
593-
function: Box<Fn(&mut VirtualMachine, PyFuncArgs) -> PyResult>,
594-
) -> PyObjectRef {
595-
PyObject::new(
596-
PyObjectPayload::RustFunction { function },
597-
self.builtin_function_or_method_type(),
598-
)
599-
}
600-
601591
pub fn new_frame(&self, code: PyObjectRef, scope: PyObjectRef) -> PyObjectRef {
602592
PyObject::new(
603593
PyObjectPayload::Frame {
@@ -1242,7 +1232,7 @@ tuple_from_py_func_args!(A, B, C);
12421232
tuple_from_py_func_args!(A, B, C, D);
12431233
tuple_from_py_func_args!(A, B, C, D, E);
12441234

1245-
pub type PyNativeFunc = Box<dyn Fn(&mut VirtualMachine, PyFuncArgs) -> PyResult>;
1235+
pub type PyNativeFunc = Box<dyn Fn(&mut VirtualMachine, PyFuncArgs) -> PyResult + 'static>;
12461236

12471237
pub trait PyNativeFuncFactory<T, R> {
12481238
fn create(self) -> PyNativeFunc;
@@ -1257,6 +1247,12 @@ where
12571247
}
12581248
}
12591249

1250+
impl PyNativeFuncFactory<PyFuncArgs, PyResult> for PyNativeFunc {
1251+
fn create(self) -> PyNativeFunc {
1252+
self
1253+
}
1254+
}
1255+
12601256
macro_rules! py_native_func_factory_tuple {
12611257
($(($n:tt, $T:ident)),+) => {
12621258
impl<F, $($T,)+ R> PyNativeFuncFactory<($($T,)+), R> for F
@@ -1378,7 +1374,7 @@ pub enum PyObjectPayload {
13781374
dict: RefCell<PyAttributes>,
13791375
},
13801376
RustFunction {
1381-
function: Box<Fn(&mut VirtualMachine, PyFuncArgs) -> PyResult>,
1377+
function: PyNativeFunc,
13821378
},
13831379
Socket {
13841380
socket: RefCell<Socket>,

wasm/lib/src/vm_class.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ impl WASMVirtualMachine {
275275
.into());
276276
};
277277
vm.ctx
278-
.set_attr(scope, "print", vm.ctx.new_rustfunc_from_box(print_fn));
278+
.set_attr(scope, "print", vm.ctx.new_rustfunc(print_fn));
279279
Ok(())
280280
},
281281
)?

0 commit comments

Comments
 (0)