@@ -133,94 +133,109 @@ pub trait PyObjectRichcmpProtocol<'p>: PyObjectProtocol<'p> {
133
133
type Result : IntoPyCallbackOutput < PyObject > ;
134
134
}
135
135
136
- /// All FFI functions for basic protocols.
137
- #[ derive( Default ) ]
138
- pub struct PyObjectMethods {
139
- pub tp_str : Option < ffi:: reprfunc > ,
140
- pub tp_repr : Option < ffi:: reprfunc > ,
141
- pub tp_hash : Option < ffi:: hashfunc > ,
142
- pub tp_getattro : Option < ffi:: getattrofunc > ,
143
- pub tp_richcompare : Option < ffi:: richcmpfunc > ,
144
- pub tp_setattro : Option < ffi:: setattrofunc > ,
145
- pub nb_bool : Option < ffi:: inquiry > ,
146
- }
147
-
136
+ /// Extension trait for proc-macro backend.
148
137
#[ doc( hidden) ]
149
- impl PyObjectMethods {
150
- // Set functions used by `#[pyproto]`.
151
- pub fn set_str < T > ( & mut self )
138
+ pub trait PyBasicSlots {
139
+ fn get_str ( ) -> ffi:: PyType_Slot
152
140
where
153
- T : for < ' p > PyObjectStrProtocol < ' p > ,
141
+ Self : for < ' p > PyObjectStrProtocol < ' p > ,
154
142
{
155
- self . tp_str = py_unary_func ! ( PyObjectStrProtocol , T :: __str__) ;
143
+ ffi:: PyType_Slot {
144
+ slot : ffi:: Py_tp_str ,
145
+ pfunc : py_unary_func ! ( PyObjectStrProtocol , Self :: __str__) as _ ,
146
+ }
156
147
}
157
- pub fn set_repr < T > ( & mut self )
148
+
149
+ fn get_repr ( ) -> ffi:: PyType_Slot
158
150
where
159
- T : for < ' p > PyObjectReprProtocol < ' p > ,
151
+ Self : for < ' p > PyObjectReprProtocol < ' p > ,
160
152
{
161
- self . tp_repr = py_unary_func ! ( PyObjectReprProtocol , T :: __repr__) ;
153
+ ffi:: PyType_Slot {
154
+ slot : ffi:: Py_tp_repr ,
155
+ pfunc : py_unary_func ! ( PyObjectReprProtocol , Self :: __repr__) as _ ,
156
+ }
162
157
}
163
- pub fn set_hash < T > ( & mut self )
158
+
159
+ fn get_hash ( ) -> ffi:: PyType_Slot
164
160
where
165
- T : for < ' p > PyObjectHashProtocol < ' p > ,
161
+ Self : for < ' p > PyObjectHashProtocol < ' p > ,
166
162
{
167
- self . tp_hash = py_unary_func ! ( PyObjectHashProtocol , T :: __hash__, ffi:: Py_hash_t ) ;
163
+ ffi:: PyType_Slot {
164
+ slot : ffi:: Py_tp_hash ,
165
+ pfunc : py_unary_func ! ( PyObjectHashProtocol , Self :: __hash__, ffi:: Py_hash_t ) as _ ,
166
+ }
168
167
}
169
- pub fn set_getattr < T > ( & mut self )
168
+
169
+ fn get_getattr ( ) -> ffi:: PyType_Slot
170
170
where
171
- T : for < ' p > PyObjectGetAttrProtocol < ' p > ,
171
+ Self : for < ' p > PyObjectGetAttrProtocol < ' p > ,
172
172
{
173
- self . tp_getattro = tp_getattro :: < T > ( ) ;
173
+ ffi:: PyType_Slot {
174
+ slot : ffi:: Py_tp_getattro ,
175
+ pfunc : tp_getattro :: < Self > ( ) as _ ,
176
+ }
174
177
}
175
- pub fn set_richcompare < T > ( & mut self )
178
+
179
+ fn get_richcompare ( ) -> ffi:: PyType_Slot
176
180
where
177
- T : for < ' p > PyObjectRichcmpProtocol < ' p > ,
181
+ Self : for < ' p > PyObjectRichcmpProtocol < ' p > ,
178
182
{
179
- self . tp_richcompare = tp_richcompare :: < T > ( ) ;
183
+ ffi:: PyType_Slot {
184
+ slot : ffi:: Py_tp_getattro ,
185
+ pfunc : tp_richcompare :: < Self > ( ) as _ ,
186
+ }
180
187
}
181
- pub fn set_setattr < T > ( & mut self )
188
+
189
+ fn get_setattr ( ) -> ffi:: PyType_Slot
182
190
where
183
- T : for < ' p > PyObjectSetAttrProtocol < ' p > ,
191
+ Self : for < ' p > PyObjectSetAttrProtocol < ' p > ,
184
192
{
185
- self . tp_setattro = py_func_set ! ( PyObjectSetAttrProtocol , T , __setattr__) ;
193
+ ffi:: PyType_Slot {
194
+ slot : ffi:: Py_tp_setattro ,
195
+ pfunc : py_func_set ! ( PyObjectSetAttrProtocol , Self :: __setattr__) as _ ,
196
+ }
186
197
}
187
- pub fn set_delattr < T > ( & mut self )
198
+
199
+ fn get_delattr ( ) -> ffi:: PyType_Slot
188
200
where
189
- T : for < ' p > PyObjectDelAttrProtocol < ' p > ,
201
+ Self : for < ' p > PyObjectDelAttrProtocol < ' p > ,
190
202
{
191
- self . tp_setattro = py_func_del ! ( PyObjectDelAttrProtocol , T , __delattr__) ;
203
+ ffi:: PyType_Slot {
204
+ slot : ffi:: Py_tp_setattro ,
205
+ pfunc : py_func_del ! ( PyObjectDelAttrProtocol , Self :: __delattr__) as _ ,
206
+ }
192
207
}
193
- pub fn set_setdelattr < T > ( & mut self )
208
+
209
+ fn get_setdelattr ( ) -> ffi:: PyType_Slot
194
210
where
195
- T : for < ' p > PyObjectSetAttrProtocol < ' p > + for < ' p > PyObjectDelAttrProtocol < ' p > ,
211
+ Self : for < ' p > PyObjectSetAttrProtocol < ' p > + for < ' p > PyObjectDelAttrProtocol < ' p > ,
196
212
{
197
- self . tp_setattro = py_func_set_del ! (
198
- PyObjectSetAttrProtocol ,
199
- PyObjectDelAttrProtocol ,
200
- T ,
201
- __setattr__,
202
- __delattr__
203
- )
213
+ ffi:: PyType_Slot {
214
+ slot : ffi:: Py_tp_setattro ,
215
+ pfunc : py_func_set_del ! (
216
+ PyObjectSetAttrProtocol ,
217
+ PyObjectDelAttrProtocol ,
218
+ Self ,
219
+ __setattr__,
220
+ __delattr__
221
+ ) as _ ,
222
+ }
204
223
}
205
- pub fn set_bool < T > ( & mut self )
224
+
225
+ fn get_bool ( ) -> ffi:: PyType_Slot
206
226
where
207
- T : for < ' p > PyObjectBoolProtocol < ' p > ,
227
+ Self : for < ' p > PyObjectBoolProtocol < ' p > ,
208
228
{
209
- self . nb_bool = py_unary_func ! ( PyObjectBoolProtocol , T :: __bool__, c_int) ;
210
- }
211
-
212
- pub ( crate ) fn update_slots ( & self , slots : & mut crate :: pyclass:: TypeSlots ) {
213
- slots. maybe_push ( ffi:: Py_tp_str , self . tp_str . map ( |v| v as _ ) ) ;
214
- slots. maybe_push ( ffi:: Py_tp_repr , self . tp_repr . map ( |v| v as _ ) ) ;
215
- slots. maybe_push ( ffi:: Py_tp_hash , self . tp_hash . map ( |v| v as _ ) ) ;
216
- slots. maybe_push ( ffi:: Py_tp_getattro , self . tp_getattro . map ( |v| v as _ ) ) ;
217
- slots. maybe_push ( ffi:: Py_tp_richcompare , self . tp_richcompare . map ( |v| v as _ ) ) ;
218
- slots. maybe_push ( ffi:: Py_tp_setattro , self . tp_setattro . map ( |v| v as _ ) ) ;
219
- slots. maybe_push ( ffi:: Py_nb_bool , self . nb_bool . map ( |v| v as _ ) ) ;
229
+ ffi:: PyType_Slot {
230
+ slot : ffi:: Py_nb_bool ,
231
+ pfunc : py_unary_func ! ( PyObjectBoolProtocol , Self :: __bool__, c_int) as _ ,
232
+ }
220
233
}
221
234
}
222
235
223
- fn tp_getattro < T > ( ) -> Option < ffi:: binaryfunc >
236
+ impl < ' p , T > PyBasicSlots for T where T : PyObjectProtocol < ' p > { }
237
+
238
+ fn tp_getattro < T > ( ) -> ffi:: binaryfunc
224
239
where
225
240
T : for < ' p > PyObjectGetAttrProtocol < ' p > ,
226
241
{
@@ -247,10 +262,10 @@ where
247
262
call_ref!( slf, __getattr__, arg) . convert( py)
248
263
} )
249
264
}
250
- Some ( wrap :: < T > )
265
+ wrap :: < T >
251
266
}
252
267
253
- fn tp_richcompare < T > ( ) -> Option < ffi:: richcmpfunc >
268
+ fn tp_richcompare < T > ( ) -> ffi:: richcmpfunc
254
269
where
255
270
T : for < ' p > PyObjectRichcmpProtocol < ' p > ,
256
271
{
@@ -283,5 +298,5 @@ where
283
298
slf. try_borrow( ) ?. __richcmp__( arg, op) . convert( py)
284
299
} )
285
300
}
286
- Some ( wrap :: < T > )
301
+ wrap :: < T >
287
302
}
0 commit comments