@@ -121,14 +121,24 @@ pub trait DerefToPyAny {
121
121
// Implementations core to all native types
122
122
#[ doc( hidden) ]
123
123
#[ macro_export]
124
+ #[ cfg_attr( docsrs, doc( cfg( all( ) ) ) ) ]
125
+ #[ cfg( not( feature = "gil-refs" ) ) ]
126
+ macro_rules! pyobject_native_type_base(
127
+ // empty implementation on non-gil-refs
128
+ ( $name: ty $( ; $generics: ident) * ) => { } ;
129
+ ) ;
130
+
131
+ // Implementations core to all native types
132
+ #[ doc( hidden) ]
133
+ #[ macro_export]
134
+ #[ cfg_attr( docsrs, doc( cfg( all( ) ) ) ) ]
135
+ #[ cfg( feature = "gil-refs" ) ]
124
136
macro_rules! pyobject_native_type_base(
125
137
( $name: ty $( ; $generics: ident) * ) => {
126
- #[ cfg( feature = "gil-refs" ) ]
127
138
unsafe impl <$( $generics, ) * > $crate:: PyNativeType for $name {
128
139
type AsRefSource = Self ;
129
140
}
130
141
131
- #[ cfg( feature = "gil-refs" ) ]
132
142
impl <$( $generics, ) * > :: std:: fmt:: Debug for $name {
133
143
fn fmt( & self , f: & mut :: std:: fmt:: Formatter <' _>)
134
144
-> :: std:: result:: Result <( ) , :: std:: fmt:: Error >
@@ -139,7 +149,6 @@ macro_rules! pyobject_native_type_base(
139
149
}
140
150
}
141
151
142
- #[ cfg( feature = "gil-refs" ) ]
143
152
impl <$( $generics, ) * > :: std:: fmt:: Display for $name {
144
153
fn fmt( & self , f: & mut :: std:: fmt:: Formatter <' _>)
145
154
-> :: std:: result:: Result <( ) , :: std:: fmt:: Error >
@@ -157,7 +166,6 @@ macro_rules! pyobject_native_type_base(
157
166
}
158
167
}
159
168
160
- #[ cfg( feature = "gil-refs" ) ]
161
169
impl <$( $generics, ) * > $crate:: ToPyObject for $name
162
170
{
163
171
#[ inline]
@@ -172,6 +180,43 @@ macro_rules! pyobject_native_type_base(
172
180
// make sense on PyAny / have different implementations).
173
181
#[ doc( hidden) ]
174
182
#[ macro_export]
183
+ #[ cfg_attr( docsrs, doc( cfg( all( ) ) ) ) ]
184
+ #[ cfg( not( feature = "gil-refs" ) ) ]
185
+ macro_rules! pyobject_native_type_named (
186
+ ( $name: ty $( ; $generics: ident) * ) => {
187
+
188
+ impl <$( $generics, ) * > :: std:: convert:: AsRef <$crate:: PyAny > for $name {
189
+ #[ inline]
190
+ fn as_ref( & self ) -> & $crate:: PyAny {
191
+ & self . 0
192
+ }
193
+ }
194
+
195
+ impl <$( $generics, ) * > :: std:: ops:: Deref for $name {
196
+ type Target = $crate:: PyAny ;
197
+
198
+ #[ inline]
199
+ fn deref( & self ) -> & $crate:: PyAny {
200
+ & self . 0
201
+ }
202
+ }
203
+
204
+ unsafe impl <$( $generics, ) * > $crate:: AsPyPointer for $name {
205
+ /// Gets the underlying FFI pointer, returns a borrowed pointer.
206
+ #[ inline]
207
+ fn as_ptr( & self ) -> * mut $crate:: ffi:: PyObject {
208
+ self . 0 . as_ptr( )
209
+ }
210
+ }
211
+
212
+ impl $crate:: types:: DerefToPyAny for $name { }
213
+ } ;
214
+ ) ;
215
+
216
+ #[ doc( hidden) ]
217
+ #[ macro_export]
218
+ #[ cfg_attr( docsrs, doc( cfg( all( ) ) ) ) ]
219
+ #[ cfg( feature = "gil-refs" ) ]
175
220
macro_rules! pyobject_native_type_named (
176
221
( $name: ty $( ; $generics: ident) * ) => {
177
222
$crate:: pyobject_native_type_base!( $name $( ; $generics) * ) ;
@@ -202,7 +247,6 @@ macro_rules! pyobject_native_type_named (
202
247
203
248
// FIXME https://github.com/PyO3/pyo3/issues/3903
204
249
#[ allow( unknown_lints, non_local_definitions) ]
205
- #[ cfg( feature = "gil-refs" ) ]
206
250
impl <$( $generics, ) * > $crate:: IntoPy <$crate:: Py <$name>> for & ' _ $name {
207
251
#[ inline]
208
252
fn into_py( self , py: $crate:: Python <' _>) -> $crate:: Py <$name> {
@@ -212,7 +256,6 @@ macro_rules! pyobject_native_type_named (
212
256
213
257
// FIXME https://github.com/PyO3/pyo3/issues/3903
214
258
#[ allow( unknown_lints, non_local_definitions) ]
215
- #[ cfg( feature = "gil-refs" ) ]
216
259
impl <$( $generics, ) * > :: std:: convert:: From <& ' _ $name> for $crate:: Py <$name> {
217
260
#[ inline]
218
261
fn from( other: & $name) -> Self {
@@ -223,7 +266,6 @@ macro_rules! pyobject_native_type_named (
223
266
224
267
// FIXME https://github.com/PyO3/pyo3/issues/3903
225
268
#[ allow( unknown_lints, non_local_definitions) ]
226
- #[ cfg( feature = "gil-refs" ) ]
227
269
impl <' a, $( $generics, ) * > :: std:: convert:: From <& ' a $name> for & ' a $crate:: PyAny {
228
270
fn from( ob: & ' a $name) -> Self {
229
271
unsafe { & * ( ob as * const $name as * const $crate:: PyAny ) }
@@ -279,11 +321,23 @@ macro_rules! pyobject_native_type_info(
279
321
// because rust-numpy has a special implementation.
280
322
#[ doc( hidden) ]
281
323
#[ macro_export]
324
+ #[ cfg_attr( docsrs, doc( cfg( all( ) ) ) ) ]
325
+ #[ cfg( not( feature = "gil-refs" ) ) ]
326
+ macro_rules! pyobject_native_type_extract {
327
+ // no body for non-gil-refs
328
+ ( $name: ty $( ; $generics: ident) * ) => { } ;
329
+ }
330
+
331
+ // NOTE: This macro is not included in pyobject_native_type_base!
332
+ // because rust-numpy has a special implementation.
333
+ #[ doc( hidden) ]
334
+ #[ macro_export]
335
+ #[ cfg_attr( docsrs, doc( cfg( all( ) ) ) ) ]
336
+ #[ cfg( feature = "gil-refs" ) ]
282
337
macro_rules! pyobject_native_type_extract {
283
338
( $name: ty $( ; $generics: ident) * ) => {
284
339
// FIXME https://github.com/PyO3/pyo3/issues/3903
285
340
#[ allow( unknown_lints, non_local_definitions) ]
286
- #[ cfg( feature = "gil-refs" ) ]
287
341
impl <' py, $( $generics, ) * > $crate:: FromPyObject <' py> for & ' py $name {
288
342
#[ inline]
289
343
fn extract_bound( obj: & $crate:: Bound <' py, $crate:: PyAny >) -> $crate:: PyResult <Self > {
0 commit comments