Skip to content

Commit 226a2a6

Browse files
committed
VM polymorphism for getter and setter
1 parent d1f9cb4 commit 226a2a6

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
@@ -12,11 +12,11 @@ use crate::vm::VirtualMachine;
1212
pub type PyGetterFunc = Box<dyn Fn(&VirtualMachine, PyObjectRef) -> PyResult>;
1313
pub type PySetterFunc = Box<dyn Fn(&VirtualMachine, PyObjectRef, PyObjectRef) -> PyResult<()>>;
1414

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

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

33-
impl<F, S, R> IntoPyGetterFunc<RefParam<S>, R> for F
33+
impl<F, S, R> IntoPyGetterFunc<(RefParam<S>, R, VirtualMachine)> for F
3434
where
3535
F: Fn(&S, &VirtualMachine) -> R + 'static,
3636
S: PyValue,
@@ -44,6 +44,28 @@ where
4444
}
4545
}
4646

47+
impl<F, T, R> IntoPyGetterFunc<(OwnedParam<T>, R)> for F
48+
where
49+
F: Fn(T) -> R + 'static,
50+
T: TryFromObject,
51+
R: IntoPyObject,
52+
{
53+
fn into_getter(self) -> PyGetterFunc {
54+
IntoPyGetterFunc::into_getter(move |obj, _vm: &VirtualMachine| (self)(obj))
55+
}
56+
}
57+
58+
impl<F, S, R> IntoPyGetterFunc<(RefParam<S>, R)> for F
59+
where
60+
F: Fn(&S) -> R + 'static,
61+
S: PyValue,
62+
R: IntoPyObject,
63+
{
64+
fn into_getter(self) -> PyGetterFunc {
65+
IntoPyGetterFunc::into_getter(move |zelf: &S, _vm: &VirtualMachine| (self)(zelf))
66+
}
67+
}
68+
4769
pub trait IntoPyNoResult {
4870
fn into_noresult(self) -> PyResult<()>;
4971
}
@@ -60,14 +82,11 @@ impl IntoPyNoResult for PyResult<()> {
6082
}
6183
}
6284

63-
pub trait IntoPySetterFunc<T, V, R>
64-
where
65-
R: IntoPyNoResult,
66-
{
85+
pub trait IntoPySetterFunc<T> {
6786
fn into_setter(self) -> PySetterFunc;
6887
}
6988

70-
impl<F, T, V, R> IntoPySetterFunc<OwnedParam<T>, V, R> for F
89+
impl<F, T, V, R> IntoPySetterFunc<(OwnedParam<T>, V, R, VirtualMachine)> for F
7190
where
7291
F: Fn(T, V, &VirtualMachine) -> R + 'static,
7392
T: TryFromObject,
@@ -83,7 +102,7 @@ where
83102
}
84103
}
85104

86-
impl<F, S, V, R> IntoPySetterFunc<RefParam<S>, V, R> for F
105+
impl<F, S, V, R> IntoPySetterFunc<(RefParam<S>, V, R, VirtualMachine)> for F
87106
where
88107
F: Fn(&S, V, &VirtualMachine) -> R + 'static,
89108
S: PyValue,
@@ -99,6 +118,30 @@ where
99118
}
100119
}
101120

121+
impl<F, T, V, R> IntoPySetterFunc<(OwnedParam<T>, V, R)> for F
122+
where
123+
F: Fn(T, V) -> R + 'static,
124+
T: TryFromObject,
125+
V: TryFromObject,
126+
R: IntoPyNoResult,
127+
{
128+
fn into_setter(self) -> PySetterFunc {
129+
IntoPySetterFunc::into_setter(move |obj, v, _vm: &VirtualMachine| (self)(obj, v))
130+
}
131+
}
132+
133+
impl<F, S, V, R> IntoPySetterFunc<(RefParam<S>, V, R)> for F
134+
where
135+
F: Fn(&S, V) -> R + 'static,
136+
S: PyValue,
137+
V: TryFromObject,
138+
R: IntoPyNoResult,
139+
{
140+
fn into_setter(self) -> PySetterFunc {
141+
IntoPySetterFunc::into_setter(move |zelf: &S, v, _vm: &VirtualMachine| (self)(zelf, v))
142+
}
143+
}
144+
102145
#[pyclass]
103146
pub struct PyGetSet {
104147
name: String,
@@ -155,9 +198,9 @@ impl PyBuiltinDescriptor for PyGetSet {
155198
}
156199

157200
impl PyGetSet {
158-
pub fn with_get<G, T, R>(name: String, getter: G) -> Self
201+
pub fn with_get<G, X>(name: String, getter: G) -> Self
159202
where
160-
G: IntoPyGetterFunc<T, R>,
203+
G: IntoPyGetterFunc<X>,
161204
{
162205
Self {
163206
name,
@@ -166,11 +209,10 @@ impl PyGetSet {
166209
}
167210
}
168211

169-
pub fn with_get_set<G, S, GT, GR, ST, SV, SR>(name: String, getter: G, setter: S) -> Self
212+
pub fn with_get_set<G, S, X, Y>(name: String, getter: G, setter: S) -> Self
170213
where
171-
G: IntoPyGetterFunc<GT, GR>,
172-
S: IntoPySetterFunc<ST, SV, SR>,
173-
SR: IntoPyNoResult,
214+
G: IntoPyGetterFunc<X>,
215+
S: IntoPySetterFunc<Y>,
174216
{
175217
Self {
176218
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)