PySetResult and IntoPySetResult

This commit is contained in:
Jeong YunWon
2020-02-04 03:17:42 +09:00
parent ca557788c8
commit d1f9cb4e58
2 changed files with 37 additions and 15 deletions

View File

@@ -44,36 +44,57 @@ where
}
}
pub trait IntoPySetterFunc<T, V> {
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<T, V, R>
where
R: IntoPyNoResult,
{
fn into_setter(self) -> PySetterFunc;
}
impl<F, T, V> IntoPySetterFunc<OwnedParam<T>, V> for F
impl<F, T, V, R> IntoPySetterFunc<OwnedParam<T>, 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<F, S, V> IntoPySetterFunc<RefParam<S>, V> for F
impl<F, S, V, R> IntoPySetterFunc<RefParam<S>, 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::<S>::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<G, S, GT, GR, ST, SV>(name: String, getter: G, setter: S) -> Self
pub fn with_get_set<G, S, GT, GR, ST, SV, SR>(name: String, getter: G, setter: S) -> Self
where
G: IntoPyGetterFunc<GT, GR>,
S: IntoPySetterFunc<ST, SV>,
S: IntoPySetterFunc<ST, SV, SR>,
SR: IntoPyNoResult,
{
Self {
name,

View File

@@ -259,11 +259,6 @@ pub struct PropertyBuilder<'a> {
setter: Option<PyObjectRef>,
}
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<I, V, VM, F: IntoPyNativeFunc<(I, V), impl PropertySetterResult, VM>>(
pub fn add_setter<
I,
V,
VM,
F: IntoPyNativeFunc<(I, V), impl super::objgetset::IntoPyNoResult, VM>,
>(
self,
func: F,
) -> Self {