|
3 | 3 | */
|
4 | 4 | use super::objtype::PyClassRef;
|
5 | 5 | use crate::function::OptionalArg;
|
6 |
| -use crate::pyobject::{PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue}; |
| 6 | +use crate::pyobject::{ |
| 7 | + IntoPyObject, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, |
| 8 | +}; |
7 | 9 | use crate::slots::PyBuiltinDescriptor;
|
8 | 10 | use crate::vm::VirtualMachine;
|
9 | 11 |
|
10 |
| -pub type PyGetterFunc = dyn Fn(PyObjectRef, &VirtualMachine) -> PyResult; |
11 |
| -pub type PySetterFunc = dyn Fn(PyObjectRef, PyObjectRef, &VirtualMachine) -> PyResult<()>; |
| 12 | +pub type PyGetterFunc = Box<dyn Fn(PyObjectRef, &VirtualMachine) -> PyResult>; |
| 13 | +pub type PySetterFunc = Box<dyn Fn(PyObjectRef, PyObjectRef, &VirtualMachine) -> PyResult<()>>; |
| 14 | + |
| 15 | +pub trait IntoPyGetterFunc<T, R> { |
| 16 | + fn into_getter(self) -> PyGetterFunc; |
| 17 | +} |
| 18 | + |
| 19 | +impl<F, T, R> IntoPyGetterFunc<T, R> for F |
| 20 | +where |
| 21 | + F: Fn(T, &VirtualMachine) -> R + 'static, |
| 22 | + T: TryFromObject, |
| 23 | + R: IntoPyObject, |
| 24 | +{ |
| 25 | + fn into_getter(self) -> PyGetterFunc { |
| 26 | + Box::new(move |obj, vm| { |
| 27 | + let obj = T::try_from_object(vm, obj)?; |
| 28 | + (self)(obj, vm).into_pyobject(vm) |
| 29 | + }) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +pub trait IntoPySetterFunc<T, V, R> { |
| 34 | + fn into_setter(self) -> PySetterFunc; |
| 35 | +} |
| 36 | + |
| 37 | +impl<F, T, V> IntoPySetterFunc<T, V, PyResult<()>> for F |
| 38 | +where |
| 39 | + F: Fn(T, V, &VirtualMachine) -> PyResult<()> + 'static, |
| 40 | + T: TryFromObject, |
| 41 | + V: TryFromObject, |
| 42 | +{ |
| 43 | + fn into_setter(self) -> PySetterFunc { |
| 44 | + Box::new(move |obj, value, vm| { |
| 45 | + let obj = T::try_from_object(vm, obj)?; |
| 46 | + let value = V::try_from_object(vm, value)?; |
| 47 | + (self)(obj, value, vm) |
| 48 | + }) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +impl<F, T, V> IntoPySetterFunc<T, V, ()> for F |
| 53 | +where |
| 54 | + F: Fn(T, V, &VirtualMachine) -> () + 'static, |
| 55 | + T: TryFromObject, |
| 56 | + V: TryFromObject, |
| 57 | +{ |
| 58 | + fn into_setter(self) -> PySetterFunc { |
| 59 | + Box::new(move |obj, value, vm| { |
| 60 | + let obj = T::try_from_object(vm, obj)?; |
| 61 | + let value = V::try_from_object(vm, value)?; |
| 62 | + Ok((self)(obj, value, vm)) |
| 63 | + }) |
| 64 | + } |
| 65 | +} |
12 | 66 |
|
13 | 67 | #[pyclass]
|
14 | 68 | pub struct PyGetSet {
|
15 | 69 | name: String,
|
16 |
| - getter: Option<Box<PyGetterFunc>>, |
17 |
| - setter: Option<Box<PySetterFunc>>, |
| 70 | + getter: Option<PyGetterFunc>, |
| 71 | + setter: Option<PySetterFunc>, |
18 | 72 | // doc: Option<String>,
|
19 | 73 | }
|
20 | 74 |
|
@@ -66,23 +120,26 @@ impl PyBuiltinDescriptor for PyGetSet {
|
66 | 120 | }
|
67 | 121 |
|
68 | 122 | impl PyGetSet {
|
69 |
| - pub fn new( |
70 |
| - name: String, |
71 |
| - getter: Option<&'static PyGetterFunc>, |
72 |
| - setter: Option<&'static PySetterFunc>, |
73 |
| - ) -> Self { |
| 123 | + pub fn with_get<G, T, R>(name: String, getter: G) -> Self |
| 124 | + where |
| 125 | + G: IntoPyGetterFunc<T, R>, |
| 126 | + { |
74 | 127 | Self {
|
75 | 128 | name,
|
76 |
| - getter: if let Some(f) = getter { |
77 |
| - Some(Box::new(f)) |
78 |
| - } else { |
79 |
| - None |
80 |
| - }, |
81 |
| - setter: if let Some(f) = setter { |
82 |
| - Some(Box::new(f)) |
83 |
| - } else { |
84 |
| - None |
85 |
| - }, |
| 129 | + getter: Some(getter.into_getter()), |
| 130 | + setter: None, |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + pub fn with_get_set<G, S, GT, GR, ST, SV, SR>(name: String, getter: G, setter: S) -> Self |
| 135 | + where |
| 136 | + G: IntoPyGetterFunc<GT, GR>, |
| 137 | + S: IntoPySetterFunc<ST, SV, SR>, |
| 138 | + { |
| 139 | + Self { |
| 140 | + name, |
| 141 | + getter: Some(getter.into_getter()), |
| 142 | + setter: Some(setter.into_setter()), |
86 | 143 | }
|
87 | 144 | }
|
88 | 145 | }
|
|
0 commit comments