Skip to content

Commit 451fc24

Browse files
committed
VM polymorphism for getter and setter
1 parent a12a3d4 commit 451fc24

File tree

2 files changed

+58
-16
lines changed

2 files changed

+58
-16
lines changed

vm/src/obj/objgetset.rs

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ use crate::vm::VirtualMachine;
1313
pub type PyGetterFunc = Box<dyn Fn(PyObjectRef, &VirtualMachine) -> PyResult>;
1414
pub type PySetterFunc = Box<dyn Fn(PyObjectRef, PyObjectRef, &VirtualMachine) -> PySetResult>;
1515

16-
pub trait IntoPyGetterFunc<T, R> {
16+
pub trait IntoPyGetterFunc<T> {
1717
fn into_getter(self) -> PyGetterFunc;
1818
}
1919

20-
impl<F, T, R> IntoPyGetterFunc<OwnedParam<T>, R> for F
20+
impl<F, T, R> IntoPyGetterFunc<(OwnedParam<T>, R, VirtualMachine)> for F
2121
where
2222
F: Fn(T, &VirtualMachine) -> R + 'static,
2323
T: TryFromObject,
@@ -31,28 +31,47 @@ where
3131
}
3232
}
3333

34-
impl<F, S, R> IntoPyGetterFunc<RefParam<S>, R> for F
34+
impl<F, S, R> IntoPyGetterFunc<(RefParam<S>, R, VirtualMachine)> for F
3535
where
3636
F: Fn(&S, &VirtualMachine) -> R + 'static,
3737
S: PyValue,
3838
R: IntoPyObject,
3939
{
4040
fn into_getter(self) -> PyGetterFunc {
4141
Box::new(move |obj, vm| {
42-
let zelf = PyRef::<S>::try_from_object(vm, obj)?;
43-
(self)(&zelf, vm).into_pyobject(vm)
42+
let zelf = &PyRef::<S>::try_from_object(vm, obj)?;
43+
(self)(zelf, vm).into_pyobject(vm)
4444
})
4545
}
4646
}
4747

48-
pub trait IntoPySetterFunc<T, V, R>
48+
impl<F, T, R> IntoPyGetterFunc<(OwnedParam<T>, R)> for F
4949
where
50-
R: IntoPySetResult,
50+
F: Fn(T) -> R + 'static,
51+
T: TryFromObject,
52+
R: IntoPyObject,
5153
{
54+
fn into_getter(self) -> PyGetterFunc {
55+
IntoPyGetterFunc::into_getter(move |obj, _vm: &VirtualMachine| (self)(obj))
56+
}
57+
}
58+
59+
impl<F, S, R> IntoPyGetterFunc<(RefParam<S>, R)> for F
60+
where
61+
F: Fn(&S) -> R + 'static,
62+
S: PyValue,
63+
R: IntoPyObject,
64+
{
65+
fn into_getter(self) -> PyGetterFunc {
66+
IntoPyGetterFunc::into_getter(move |zelf: &S, _vm: &VirtualMachine| (self)(zelf))
67+
}
68+
}
69+
70+
pub trait IntoPySetterFunc<T> {
5271
fn into_setter(self) -> PySetterFunc;
5372
}
5473

55-
impl<F, T, V, R> IntoPySetterFunc<OwnedParam<T>, V, R> for F
74+
impl<F, T, V, R> IntoPySetterFunc<(OwnedParam<T>, V, R, VirtualMachine)> for F
5675
where
5776
F: Fn(T, V, &VirtualMachine) -> R + 'static,
5877
T: TryFromObject,
@@ -68,7 +87,7 @@ where
6887
}
6988
}
7089

71-
impl<F, S, V, R> IntoPySetterFunc<RefParam<S>, V, R> for F
90+
impl<F, S, V, R> IntoPySetterFunc<(RefParam<S>, V, R, VirtualMachine)> for F
7291
where
7392
F: Fn(&S, V, &VirtualMachine) -> R + 'static,
7493
S: PyValue,
@@ -84,6 +103,30 @@ where
84103
}
85104
}
86105

106+
impl<F, T, V, R> IntoPySetterFunc<(OwnedParam<T>, V, R)> for F
107+
where
108+
F: Fn(T, V) -> R + 'static,
109+
T: TryFromObject,
110+
V: TryFromObject,
111+
R: IntoPySetResult,
112+
{
113+
fn into_setter(self) -> PySetterFunc {
114+
IntoPySetterFunc::into_setter(move |obj, v, _vm: &VirtualMachine| (self)(obj, v))
115+
}
116+
}
117+
118+
impl<F, S, V, R> IntoPySetterFunc<(RefParam<S>, V, R)> for F
119+
where
120+
F: Fn(&S, V) -> R + 'static,
121+
S: PyValue,
122+
V: TryFromObject,
123+
R: IntoPySetResult,
124+
{
125+
fn into_setter(self) -> PySetterFunc {
126+
IntoPySetterFunc::into_setter(move |zelf: &S, v, _vm: &VirtualMachine| (self)(zelf, v))
127+
}
128+
}
129+
87130
#[pyclass]
88131
pub struct PyGetSet {
89132
name: String,
@@ -140,9 +183,9 @@ impl PyBuiltinDescriptor for PyGetSet {
140183
}
141184

142185
impl PyGetSet {
143-
pub fn with_get<G, T, R>(name: String, getter: G) -> Self
186+
pub fn with_get<G, X>(name: String, getter: G) -> Self
144187
where
145-
G: IntoPyGetterFunc<T, R>,
188+
G: IntoPyGetterFunc<X>,
146189
{
147190
Self {
148191
name,
@@ -151,11 +194,10 @@ impl PyGetSet {
151194
}
152195
}
153196

154-
pub fn with_get_set<G, S, GT, GR, ST, SV, SR>(name: String, getter: G, setter: S) -> Self
197+
pub fn with_get_set<G, S, X, Y>(name: String, getter: G, setter: S) -> Self
155198
where
156-
G: IntoPyGetterFunc<GT, GR>,
157-
S: IntoPySetterFunc<ST, SV, SR>,
158-
SR: IntoPySetResult,
199+
G: IntoPyGetterFunc<X>,
200+
S: IntoPySetterFunc<Y>,
159201
{
160202
Self {
161203
name,

vm/src/obj/objproperty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ impl<'a> PropertyBuilder<'a> {
268268
}
269269
}
270270

271-
pub fn add_getter<I, V, VM, F: IntoPyNativeFunc<I, V, VM>>(self, func: F) -> Self {
271+
pub fn add_getter<I, R, VM, F: IntoPyNativeFunc<I, R, VM>>(self, func: F) -> Self {
272272
let func = self.ctx.new_method(func);
273273
Self {
274274
ctx: self.ctx,

0 commit comments

Comments
 (0)