Skip to content

Commit 09602a2

Browse files
committed
Change RustPyFunc from a fn pointer to a Fn trait
1 parent 946df53 commit 09602a2

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

vm/src/frame.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ impl Frame {
560560
bytecode::Instruction::LoadBuildClass => {
561561
let rustfunc = PyObject::new(
562562
PyObjectKind::RustFunction {
563-
function: builtins::builtin_build_class_,
563+
function: Box::new(builtins::builtin_build_class_),
564564
},
565565
vm.ctx.type_type(),
566566
);

vm/src/pyobject.rs

+26-7
Original file line numberDiff line numberDiff line change
@@ -452,9 +452,24 @@ impl PyContext {
452452
)
453453
}
454454

455-
pub fn new_rustfunc(&self, function: RustPyFunc) -> PyObjectRef {
455+
pub fn new_rustfunc<F: 'static + Fn(&mut VirtualMachine, PyFuncArgs) -> PyResult>(
456+
&self,
457+
function: F,
458+
) -> PyObjectRef {
456459
PyObject::new(
457-
PyObjectKind::RustFunction { function: function },
460+
PyObjectKind::RustFunction {
461+
function: Box::new(function),
462+
},
463+
self.function_type(),
464+
)
465+
}
466+
467+
pub fn new_rustfunc_from_box(
468+
&self,
469+
function: Box<Fn(&mut VirtualMachine, PyFuncArgs) -> PyResult>,
470+
) -> PyObjectRef {
471+
PyObject::new(
472+
PyObjectKind::RustFunction { function },
458473
self.function_type(),
459474
)
460475
}
@@ -463,7 +478,10 @@ impl PyContext {
463478
PyObject::new(PyObjectKind::Frame { frame: frame }, self.frame_type())
464479
}
465480

466-
pub fn new_property(&self, function: RustPyFunc) -> PyObjectRef {
481+
pub fn new_property<F: 'static + Fn(&mut VirtualMachine, PyFuncArgs) -> PyResult>(
482+
&self,
483+
function: F,
484+
) -> PyObjectRef {
467485
let fget = self.new_rustfunc(function);
468486
let py_obj = PyObject::new(
469487
PyObjectKind::Instance {
@@ -505,7 +523,10 @@ impl PyContext {
505523
)
506524
}
507525

508-
pub fn new_member_descriptor(&self, function: RustPyFunc) -> PyObjectRef {
526+
pub fn new_member_descriptor<F: 'static + Fn(&mut VirtualMachine, PyFuncArgs) -> PyResult>(
527+
&self,
528+
function: F,
529+
) -> PyObjectRef {
509530
let dict = self.new_dict();
510531
self.set_item(&dict, "function", self.new_rustfunc(function));
511532
self.new_instance(dict, self.member_descriptor_type())
@@ -772,8 +793,6 @@ impl PyFuncArgs {
772793
}
773794
}
774795

775-
type RustPyFunc = fn(vm: &mut VirtualMachine, PyFuncArgs) -> PyResult;
776-
777796
/// Rather than determining the type of a python object, this enum is more
778797
/// a holder for the rust payload of a python object. It is more a carrier
779798
/// of rust data for a particular python object. Determine the python type
@@ -850,7 +869,7 @@ pub enum PyObjectKind {
850869
dict: PyObjectRef,
851870
},
852871
RustFunction {
853-
function: RustPyFunc,
872+
function: Box<Fn(&mut VirtualMachine, PyFuncArgs) -> PyResult>,
854873
},
855874
}
856875

vm/src/vm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ impl VirtualMachine {
239239
pub fn invoke(&mut self, func_ref: PyObjectRef, args: PyFuncArgs) -> PyResult {
240240
trace!("Invoke: {:?} {:?}", func_ref, args);
241241
match func_ref.borrow().kind {
242-
PyObjectKind::RustFunction { function } => function(self, args),
242+
PyObjectKind::RustFunction { ref function } => function(self, args),
243243
PyObjectKind::Function {
244244
ref code,
245245
ref scope,

0 commit comments

Comments
 (0)