|
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> { |
| 34 | + fn into_setter(self) -> PySetterFunc; |
| 35 | +} |
| 36 | + |
| 37 | +impl<F, T, V> IntoPySetterFunc<T, V> 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 | +} |
12 | 51 |
|
13 | 52 | #[pyclass] |
14 | 53 | pub struct PyGetSet { |
15 | 54 | name: String, |
16 | | - getter: Option<Box<PyGetterFunc>>, |
17 | | - setter: Option<Box<PySetterFunc>>, |
| 55 | + getter: Option<PyGetterFunc>, |
| 56 | + setter: Option<PySetterFunc>, |
18 | 57 | // doc: Option<String>, |
19 | 58 | } |
20 | 59 |
|
@@ -66,23 +105,26 @@ impl PyBuiltinDescriptor for PyGetSet { |
66 | 105 | } |
67 | 106 |
|
68 | 107 | impl PyGetSet { |
69 | | - pub fn new( |
70 | | - name: String, |
71 | | - getter: Option<&'static PyGetterFunc>, |
72 | | - setter: Option<&'static PySetterFunc>, |
73 | | - ) -> Self { |
| 108 | + pub fn with_get<G, T, R>(name: String, getter: G) -> Self |
| 109 | + where |
| 110 | + G: IntoPyGetterFunc<T, R>, |
| 111 | + { |
74 | 112 | Self { |
75 | 113 | 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 | | - }, |
| 114 | + getter: Some(getter.into_getter()), |
| 115 | + setter: None, |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + pub fn with_get_set<G, S, GT, GR, ST, SV>(name: String, getter: G, setter: S) -> Self |
| 120 | + where |
| 121 | + G: IntoPyGetterFunc<GT, GR>, |
| 122 | + S: IntoPySetterFunc<ST, SV>, |
| 123 | + { |
| 124 | + Self { |
| 125 | + name, |
| 126 | + getter: Some(getter.into_getter()), |
| 127 | + setter: Some(setter.into_setter()), |
86 | 128 | } |
87 | 129 | } |
88 | 130 | } |
|
0 commit comments