diff --git a/vm/src/obj/objgetset.rs b/vm/src/obj/objgetset.rs index c3c8d12f3..1e257f189 100644 --- a/vm/src/obj/objgetset.rs +++ b/vm/src/obj/objgetset.rs @@ -44,36 +44,57 @@ where } } -pub trait IntoPySetterFunc { +pub trait IntoPyNoResult { + fn into_noresult(self) -> PyResult<()>; +} + +impl IntoPyNoResult for () { + fn into_noresult(self) -> PyResult<()> { + Ok(()) + } +} + +impl IntoPyNoResult for PyResult<()> { + fn into_noresult(self) -> PyResult<()> { + self + } +} + +pub trait IntoPySetterFunc +where + R: IntoPyNoResult, +{ fn into_setter(self) -> PySetterFunc; } -impl IntoPySetterFunc, V> for F +impl IntoPySetterFunc, V, R> for F where - F: Fn(T, V, &VirtualMachine) -> PyResult<()> + 'static, + F: Fn(T, V, &VirtualMachine) -> R + 'static, T: TryFromObject, V: TryFromObject, + R: IntoPyNoResult, { fn into_setter(self) -> PySetterFunc { Box::new(move |vm, obj, value| { let obj = T::try_from_object(vm, obj)?; let value = V::try_from_object(vm, value)?; - (self)(obj, value, vm) + (self)(obj, value, vm).into_noresult() }) } } -impl IntoPySetterFunc, V> for F +impl IntoPySetterFunc, V, R> for F where - F: Fn(&S, V, &VirtualMachine) -> PyResult<()> + 'static, + F: Fn(&S, V, &VirtualMachine) -> R + 'static, S: PyValue, V: TryFromObject, + R: IntoPyNoResult, { fn into_setter(self) -> PySetterFunc { Box::new(move |vm, obj, value| { let zelf = PyRef::::try_from_object(vm, obj)?; let value = V::try_from_object(vm, value)?; - (self)(&zelf, value, vm) + (self)(&zelf, value, vm).into_noresult() }) } } @@ -145,10 +166,11 @@ impl PyGetSet { } } - pub fn with_get_set(name: String, getter: G, setter: S) -> Self + pub fn with_get_set(name: String, getter: G, setter: S) -> Self where G: IntoPyGetterFunc, - S: IntoPySetterFunc, + S: IntoPySetterFunc, + SR: IntoPyNoResult, { Self { name, diff --git a/vm/src/obj/objproperty.rs b/vm/src/obj/objproperty.rs index d25feb227..52645e185 100644 --- a/vm/src/obj/objproperty.rs +++ b/vm/src/obj/objproperty.rs @@ -259,11 +259,6 @@ pub struct PropertyBuilder<'a> { setter: Option, } -pub trait PropertySetterResult {} - -impl PropertySetterResult for PyResult<()> {} -impl PropertySetterResult for () {} - impl<'a> PropertyBuilder<'a> { pub fn new(ctx: &'a PyContext) -> Self { Self { @@ -282,7 +277,12 @@ impl<'a> PropertyBuilder<'a> { } } - pub fn add_setter>( + pub fn add_setter< + I, + V, + VM, + F: IntoPyNativeFunc<(I, V), impl super::objgetset::IntoPyNoResult, VM>, + >( self, func: F, ) -> Self {