forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetset.rs
More file actions
192 lines (172 loc) · 5.39 KB
/
getset.rs
File metadata and controls
192 lines (172 loc) · 5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*! Python `attribute` descriptor class. (PyGetSet)
*/
use crate::{
convert::ToPyResult,
function::{OwnedParam, RefParam},
object::PyThreadingConstraint,
PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject, VirtualMachine,
};
#[derive(result_like::OptionLike, is_macro::Is, Debug)]
pub enum PySetterValue<T = PyObjectRef> {
Assign(T),
Delete,
}
impl PySetterValue {
pub fn unwrap_or_none(self, vm: &VirtualMachine) -> PyObjectRef {
match self {
Self::Assign(value) => value,
Self::Delete => vm.ctx.none(),
}
}
}
trait FromPySetterValue
where
Self: Sized,
{
fn from_setter_value(vm: &VirtualMachine, obj: PySetterValue) -> PyResult<Self>;
}
impl<T> FromPySetterValue for T
where
T: Sized + TryFromObject,
{
#[inline]
fn from_setter_value(vm: &VirtualMachine, obj: PySetterValue) -> PyResult<Self> {
let obj = obj.ok_or_else(|| vm.new_type_error("can't delete attribute".to_owned()))?;
T::try_from_object(vm, obj)
}
}
impl<T> FromPySetterValue for PySetterValue<T>
where
T: Sized + TryFromObject,
{
#[inline]
fn from_setter_value(vm: &VirtualMachine, obj: PySetterValue) -> PyResult<Self> {
obj.map(|obj| T::try_from_object(vm, obj)).transpose()
}
}
pub type PyGetterFunc = Box<py_dyn_fn!(dyn Fn(&VirtualMachine, PyObjectRef) -> PyResult)>;
pub type PySetterFunc =
Box<py_dyn_fn!(dyn Fn(&VirtualMachine, PyObjectRef, PySetterValue) -> PyResult<()>)>;
pub trait IntoPyGetterFunc<T>: PyThreadingConstraint + Sized + 'static {
fn get(&self, obj: PyObjectRef, vm: &VirtualMachine) -> PyResult;
fn into_getter(self) -> PyGetterFunc {
Box::new(move |vm, obj| self.get(obj, vm))
}
}
impl<F, T, R> IntoPyGetterFunc<(OwnedParam<T>, R, VirtualMachine)> for F
where
F: Fn(T, &VirtualMachine) -> R + 'static + Send + Sync,
T: TryFromObject,
R: ToPyResult,
{
fn get(&self, obj: PyObjectRef, vm: &VirtualMachine) -> PyResult {
let obj = T::try_from_object(vm, obj)?;
(self)(obj, vm).to_pyresult(vm)
}
}
impl<F, S, R> IntoPyGetterFunc<(RefParam<S>, R, VirtualMachine)> for F
where
F: Fn(&S, &VirtualMachine) -> R + 'static + Send + Sync,
S: PyPayload,
R: ToPyResult,
{
fn get(&self, obj: PyObjectRef, vm: &VirtualMachine) -> PyResult {
let zelf = PyRef::<S>::try_from_object(vm, obj)?;
(self)(&zelf, vm).to_pyresult(vm)
}
}
impl<F, T, R> IntoPyGetterFunc<(OwnedParam<T>, R)> for F
where
F: Fn(T) -> R + 'static + Send + Sync,
T: TryFromObject,
R: ToPyResult,
{
fn get(&self, obj: PyObjectRef, vm: &VirtualMachine) -> PyResult {
let obj = T::try_from_object(vm, obj)?;
(self)(obj).to_pyresult(vm)
}
}
impl<F, S, R> IntoPyGetterFunc<(RefParam<S>, R)> for F
where
F: Fn(&S) -> R + 'static + Send + Sync,
S: PyPayload,
R: ToPyResult,
{
fn get(&self, obj: PyObjectRef, vm: &VirtualMachine) -> PyResult {
let zelf = PyRef::<S>::try_from_object(vm, obj)?;
(self)(&zelf).to_pyresult(vm)
}
}
pub trait IntoPyNoResult {
fn into_noresult(self) -> PyResult<()>;
}
impl IntoPyNoResult for () {
#[inline]
fn into_noresult(self) -> PyResult<()> {
Ok(())
}
}
impl IntoPyNoResult for PyResult<()> {
#[inline]
fn into_noresult(self) -> PyResult<()> {
self
}
}
pub trait IntoPySetterFunc<T>: PyThreadingConstraint + Sized + 'static {
fn set(&self, obj: PyObjectRef, value: PySetterValue, vm: &VirtualMachine) -> PyResult<()>;
fn into_setter(self) -> PySetterFunc {
Box::new(move |vm, obj, value| self.set(obj, value, vm))
}
}
impl<F, T, V, R> IntoPySetterFunc<(OwnedParam<T>, V, R, VirtualMachine)> for F
where
F: Fn(T, V, &VirtualMachine) -> R + 'static + Send + Sync,
T: TryFromObject,
V: FromPySetterValue,
R: IntoPyNoResult,
{
fn set(&self, obj: PyObjectRef, value: PySetterValue, vm: &VirtualMachine) -> PyResult<()> {
let obj = T::try_from_object(vm, obj)?;
let value = V::from_setter_value(vm, value)?;
(self)(obj, value, vm).into_noresult()
}
}
impl<F, S, V, R> IntoPySetterFunc<(RefParam<S>, V, R, VirtualMachine)> for F
where
F: Fn(&S, V, &VirtualMachine) -> R + 'static + Send + Sync,
S: PyPayload,
V: FromPySetterValue,
R: IntoPyNoResult,
{
fn set(&self, obj: PyObjectRef, value: PySetterValue, vm: &VirtualMachine) -> PyResult<()> {
let zelf = PyRef::<S>::try_from_object(vm, obj)?;
let value = V::from_setter_value(vm, value)?;
(self)(&zelf, value, vm).into_noresult()
}
}
impl<F, T, V, R> IntoPySetterFunc<(OwnedParam<T>, V, R)> for F
where
F: Fn(T, V) -> R + 'static + Send + Sync,
T: TryFromObject,
V: FromPySetterValue,
R: IntoPyNoResult,
{
fn set(&self, obj: PyObjectRef, value: PySetterValue, vm: &VirtualMachine) -> PyResult<()> {
let obj = T::try_from_object(vm, obj)?;
let value = V::from_setter_value(vm, value)?;
(self)(obj, value).into_noresult()
}
}
impl<F, S, V, R> IntoPySetterFunc<(RefParam<S>, V, R)> for F
where
F: Fn(&S, V) -> R + 'static + Send + Sync,
S: PyPayload,
V: FromPySetterValue,
R: IntoPyNoResult,
{
fn set(&self, obj: PyObjectRef, value: PySetterValue, vm: &VirtualMachine) -> PyResult<()> {
let zelf = PyRef::<S>::try_from_object(vm, obj)?;
let value = V::from_setter_value(vm, value)?;
(self)(&zelf, value).into_noresult()
}
}