Skip to content

Commit ca55778

Browse files
committed
&self support for getter/setter
1 parent c3d5f6c commit ca55778

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

vm/src/obj/objgetset.rs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
*/
44
use super::objtype::PyClassRef;
5-
use crate::function::OptionalArg;
5+
use crate::function::{OptionalArg, OwnedParam, RefParam};
66
use crate::pyobject::{
77
IntoPyObject, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject,
88
};
@@ -16,7 +16,7 @@ pub trait IntoPyGetterFunc<T, R> {
1616
fn into_getter(self) -> PyGetterFunc;
1717
}
1818

19-
impl<F, T, R> IntoPyGetterFunc<T, R> for F
19+
impl<F, T, R> IntoPyGetterFunc<OwnedParam<T>, R> for F
2020
where
2121
F: Fn(T, &VirtualMachine) -> R + 'static,
2222
T: TryFromObject,
@@ -30,11 +30,25 @@ where
3030
}
3131
}
3232

33+
impl<F, S, R> IntoPyGetterFunc<RefParam<S>, R> for F
34+
where
35+
F: Fn(&S, &VirtualMachine) -> R + 'static,
36+
S: PyValue,
37+
R: IntoPyObject,
38+
{
39+
fn into_getter(self) -> PyGetterFunc {
40+
Box::new(move |vm, obj| {
41+
let zelf = PyRef::<S>::try_from_object(vm, obj)?;
42+
(self)(&zelf, vm).into_pyobject(vm)
43+
})
44+
}
45+
}
46+
3347
pub trait IntoPySetterFunc<T, V> {
3448
fn into_setter(self) -> PySetterFunc;
3549
}
3650

37-
impl<F, T, V> IntoPySetterFunc<T, V> for F
51+
impl<F, T, V> IntoPySetterFunc<OwnedParam<T>, V> for F
3852
where
3953
F: Fn(T, V, &VirtualMachine) -> PyResult<()> + 'static,
4054
T: TryFromObject,
@@ -49,6 +63,21 @@ where
4963
}
5064
}
5165

66+
impl<F, S, V> IntoPySetterFunc<RefParam<S>, V> for F
67+
where
68+
F: Fn(&S, V, &VirtualMachine) -> PyResult<()> + 'static,
69+
S: PyValue,
70+
V: TryFromObject,
71+
{
72+
fn into_setter(self) -> PySetterFunc {
73+
Box::new(move |vm, obj, value| {
74+
let zelf = PyRef::<S>::try_from_object(vm, obj)?;
75+
let value = V::try_from_object(vm, value)?;
76+
(self)(&zelf, value, vm)
77+
})
78+
}
79+
}
80+
5281
#[pyclass]
5382
pub struct PyGetSet {
5483
name: String,

0 commit comments

Comments
 (0)