Allow to skip VM param for IntoPyNativeFunc functions

This commit is contained in:
Jeong YunWon
2019-12-28 12:21:53 +09:00
parent 31d3fc6e6f
commit 92ba35d1b1
5 changed files with 38 additions and 18 deletions

View File

@@ -83,7 +83,7 @@ impl PyBaseException {
}
#[pyproperty(name = "__traceback__", setter)]
fn setter_traceback(&self, traceback: Option<PyTracebackRef>, _vm: &VirtualMachine) {
pub fn set_traceback(&self, traceback: Option<PyTracebackRef>) {
self.traceback.replace(traceback);
}
@@ -154,9 +154,6 @@ impl PyBaseException {
pub fn traceback(&self) -> Option<PyTracebackRef> {
self.traceback.borrow().clone()
}
pub fn set_traceback(&self, tb: Option<PyTracebackRef>) {
self.traceback.replace(tb);
}
pub fn cause(&self) -> Option<PyBaseExceptionRef> {
self.cause.borrow().clone()

View File

@@ -498,11 +498,11 @@ pub type PyNativeFunc = Box<dyn Fn(&VirtualMachine, PyFuncArgs) -> PyResult + 's
///
/// A bare `PyNativeFunc` also implements this trait, allowing the above to be
/// done manually, for rare situations that don't fit into this model.
pub trait IntoPyNativeFunc<T, R> {
pub trait IntoPyNativeFunc<T, R, VM> {
fn into_func(self) -> PyNativeFunc;
}
impl<F> IntoPyNativeFunc<PyFuncArgs, PyResult> for F
impl<F> IntoPyNativeFunc<PyFuncArgs, PyResult, VirtualMachine> for F
where
F: Fn(&VirtualMachine, PyFuncArgs) -> PyResult + 'static,
{
@@ -520,7 +520,7 @@ pub struct RefParam<T>(std::marker::PhantomData<T>);
// Note that this could be done without a macro - it is simply to avoid repetition.
macro_rules! into_py_native_func_tuple {
($(($n:tt, $T:ident)),*) => {
impl<F, $($T,)* R> IntoPyNativeFunc<($(OwnedParam<$T>,)*), R> for F
impl<F, $($T,)* R> IntoPyNativeFunc<($(OwnedParam<$T>,)*), R, VirtualMachine> for F
where
F: Fn($($T,)* &VirtualMachine) -> R + 'static,
$($T: FromArgs,)*
@@ -535,7 +535,7 @@ macro_rules! into_py_native_func_tuple {
}
}
impl<F, S, $($T,)* R> IntoPyNativeFunc<(RefParam<S>, $(OwnedParam<$T>,)*), R> for F
impl<F, S, $($T,)* R> IntoPyNativeFunc<(RefParam<S>, $(OwnedParam<$T>,)*), R, VirtualMachine> for F
where
F: Fn(&S, $($T,)* &VirtualMachine) -> R + 'static,
S: PyValue,
@@ -550,6 +550,29 @@ macro_rules! into_py_native_func_tuple {
})
}
}
impl<F, $($T,)* R> IntoPyNativeFunc<($(OwnedParam<$T>,)*), R, ()> for F
where
F: Fn($($T,)*) -> R + 'static,
$($T: FromArgs,)*
R: IntoPyObject,
{
fn into_func(self) -> PyNativeFunc {
IntoPyNativeFunc::into_func(move |$($n,)* _vm: &VirtualMachine| (self)($($n,)*))
}
}
impl<F, S, $($T,)* R> IntoPyNativeFunc<(RefParam<S>, $(OwnedParam<$T>,)*), R, ()> for F
where
F: Fn(&S, $($T,)*) -> R + 'static,
S: PyValue,
$($T: FromArgs,)*
R: IntoPyObject,
{
fn into_func(self) -> PyNativeFunc {
IntoPyNativeFunc::into_func(move |zelf: &S, $($n,)* _vm: &VirtualMachine| (self)(zelf, $($n,)*))
}
}
};
}

View File

@@ -265,7 +265,7 @@ impl<'a> PropertyBuilder<'a> {
}
}
pub fn add_getter<I, V, F: IntoPyNativeFunc<I, V>>(self, func: F) -> Self {
pub fn add_getter<I, V, VM, F: IntoPyNativeFunc<I, V, VM>>(self, func: F) -> Self {
let func = self.ctx.new_rustfunc(func);
Self {
ctx: self.ctx,
@@ -274,7 +274,7 @@ impl<'a> PropertyBuilder<'a> {
}
}
pub fn add_setter<I, V, F: IntoPyNativeFunc<(I, V), impl PropertySetterResult>>(
pub fn add_setter<I, V, VM, F: IntoPyNativeFunc<(I, V), impl PropertySetterResult, VM>>(
self,
func: F,
) -> Self {

View File

@@ -460,9 +460,9 @@ impl PyContext {
PyObject::new(PyNamespace, self.namespace_type(), Some(self.new_dict()))
}
pub fn new_rustfunc<F, T, R>(&self, f: F) -> PyObjectRef
pub fn new_rustfunc<F, T, R, VM>(&self, f: F) -> PyObjectRef
where
F: IntoPyNativeFunc<T, R>,
F: IntoPyNativeFunc<T, R, VM>,
{
PyObject::new(
PyBuiltinFunction::new(f.into_func()),
@@ -471,9 +471,9 @@ impl PyContext {
)
}
pub fn new_classmethod<F, T, R>(&self, f: F) -> PyObjectRef
pub fn new_classmethod<F, T, R, VM>(&self, f: F) -> PyObjectRef
where
F: IntoPyNativeFunc<T, R>,
F: IntoPyNativeFunc<T, R, VM>,
{
PyObject::new(
PyClassMethod {
@@ -484,9 +484,9 @@ impl PyContext {
)
}
pub fn new_property<F, I, V>(&self, f: F) -> PyObjectRef
pub fn new_property<F, I, V, VM>(&self, f: F) -> PyObjectRef
where
F: IntoPyNativeFunc<I, V>,
F: IntoPyNativeFunc<I, V, VM>,
{
PropertyBuilder::new(self).add_getter(f).create()
}

View File

@@ -1247,7 +1247,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
follow_symlinks: Option<bool>,
};
impl<'a> SupportFunc<'a> {
fn new<F, T, R>(
fn new<F, T, R, VM>(
vm: &VirtualMachine,
name: &'a str,
func: F,
@@ -1256,7 +1256,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
follow_symlinks: Option<bool>,
) -> Self
where
F: IntoPyNativeFunc<T, R>,
F: IntoPyNativeFunc<T, R, VM>,
{
let func_obj = vm.ctx.new_rustfunc(func);
Self {