Skip to content

Commit d1f9cb4

Browse files
committed
PySetResult and IntoPySetResult
1 parent ca55778 commit d1f9cb4

File tree

2 files changed

+37
-15
lines changed

2 files changed

+37
-15
lines changed

vm/src/obj/objgetset.rs

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,36 +44,57 @@ where
4444
}
4545
}
4646

47-
pub trait IntoPySetterFunc<T, V> {
47+
pub trait IntoPyNoResult {
48+
fn into_noresult(self) -> PyResult<()>;
49+
}
50+
51+
impl IntoPyNoResult for () {
52+
fn into_noresult(self) -> PyResult<()> {
53+
Ok(())
54+
}
55+
}
56+
57+
impl IntoPyNoResult for PyResult<()> {
58+
fn into_noresult(self) -> PyResult<()> {
59+
self
60+
}
61+
}
62+
63+
pub trait IntoPySetterFunc<T, V, R>
64+
where
65+
R: IntoPyNoResult,
66+
{
4867
fn into_setter(self) -> PySetterFunc;
4968
}
5069

51-
impl<F, T, V> IntoPySetterFunc<OwnedParam<T>, V> for F
70+
impl<F, T, V, R> IntoPySetterFunc<OwnedParam<T>, V, R> for F
5271
where
53-
F: Fn(T, V, &VirtualMachine) -> PyResult<()> + 'static,
72+
F: Fn(T, V, &VirtualMachine) -> R + 'static,
5473
T: TryFromObject,
5574
V: TryFromObject,
75+
R: IntoPyNoResult,
5676
{
5777
fn into_setter(self) -> PySetterFunc {
5878
Box::new(move |vm, obj, value| {
5979
let obj = T::try_from_object(vm, obj)?;
6080
let value = V::try_from_object(vm, value)?;
61-
(self)(obj, value, vm)
81+
(self)(obj, value, vm).into_noresult()
6282
})
6383
}
6484
}
6585

66-
impl<F, S, V> IntoPySetterFunc<RefParam<S>, V> for F
86+
impl<F, S, V, R> IntoPySetterFunc<RefParam<S>, V, R> for F
6787
where
68-
F: Fn(&S, V, &VirtualMachine) -> PyResult<()> + 'static,
88+
F: Fn(&S, V, &VirtualMachine) -> R + 'static,
6989
S: PyValue,
7090
V: TryFromObject,
91+
R: IntoPyNoResult,
7192
{
7293
fn into_setter(self) -> PySetterFunc {
7394
Box::new(move |vm, obj, value| {
7495
let zelf = PyRef::<S>::try_from_object(vm, obj)?;
7596
let value = V::try_from_object(vm, value)?;
76-
(self)(&zelf, value, vm)
97+
(self)(&zelf, value, vm).into_noresult()
7798
})
7899
}
79100
}
@@ -145,10 +166,11 @@ impl PyGetSet {
145166
}
146167
}
147168

148-
pub fn with_get_set<G, S, GT, GR, ST, SV>(name: String, getter: G, setter: S) -> Self
169+
pub fn with_get_set<G, S, GT, GR, ST, SV, SR>(name: String, getter: G, setter: S) -> Self
149170
where
150171
G: IntoPyGetterFunc<GT, GR>,
151-
S: IntoPySetterFunc<ST, SV>,
172+
S: IntoPySetterFunc<ST, SV, SR>,
173+
SR: IntoPyNoResult,
152174
{
153175
Self {
154176
name,

vm/src/obj/objproperty.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -259,11 +259,6 @@ pub struct PropertyBuilder<'a> {
259259
setter: Option<PyObjectRef>,
260260
}
261261

262-
pub trait PropertySetterResult {}
263-
264-
impl PropertySetterResult for PyResult<()> {}
265-
impl PropertySetterResult for () {}
266-
267262
impl<'a> PropertyBuilder<'a> {
268263
pub fn new(ctx: &'a PyContext) -> Self {
269264
Self {
@@ -282,7 +277,12 @@ impl<'a> PropertyBuilder<'a> {
282277
}
283278
}
284279

285-
pub fn add_setter<I, V, VM, F: IntoPyNativeFunc<(I, V), impl PropertySetterResult, VM>>(
280+
pub fn add_setter<
281+
I,
282+
V,
283+
VM,
284+
F: IntoPyNativeFunc<(I, V), impl super::objgetset::IntoPyNoResult, VM>,
285+
>(
286286
self,
287287
func: F,
288288
) -> Self {

0 commit comments

Comments
 (0)