@@ -186,93 +186,86 @@ pub unsafe trait Ungil {}
186
186
unsafe impl < T : Send > Ungil for T { }
187
187
188
188
#[ cfg( feature = "nightly" ) ]
189
- macro_rules! define {
190
- ( $( $tt: tt) * ) => { $( $tt) * }
191
- }
192
- #[ cfg( not( feature = "nightly" ) ) ]
193
- macro_rules! define {
194
- ( $( $tt: tt) * ) => { } ;
195
- }
196
-
197
- define ! {
198
- /// Types that are safe to access while the GIL is not held.
199
- ///
200
- /// # Safety
201
- ///
202
- /// The type must not carry borrowed Python references or, if it does, not allow access to them if
203
- /// the GIL is not held.
204
- ///
205
- /// See the [module-level documentation](self) for more information.
206
- ///
207
- /// # Examples
208
- ///
209
- /// Types which are `Ungil` cannot be used in contexts where the GIL was released, e.g.
210
- ///
211
- /// ```compile_fail
212
- /// # use pyo3::prelude::*;
213
- /// # use pyo3::types::PyString;
214
- /// Python::with_gil(|py| {
215
- /// let string = PyString::new(py, "foo");
216
- ///
217
- /// py.allow_threads(|| {
218
- /// println!("{:?}", string);
219
- /// });
220
- /// });
221
- /// ```
222
- ///
223
- /// This applies to the GIL token `Python` itself as well, e.g.
224
- ///
225
- /// ```compile_fail
226
- /// # use pyo3::prelude::*;
227
- /// Python::with_gil(|py| {
228
- /// py.allow_threads(|| {
229
- /// drop(py);
230
- /// });
231
- /// });
232
- /// ```
233
- ///
234
- /// On nightly Rust, this is not based on the [`Send`] auto trait and hence we are able
235
- /// to prevent incorrectly circumventing it using e.g. the [`send_wrapper`](https://docs.rs/send_wrapper/) crate:
236
- ///
237
- /// ```compile_fail
238
- /// # use pyo3::prelude::*;
239
- /// # use pyo3::types::PyString;
240
- /// use send_wrapper::SendWrapper;
241
- ///
242
- /// Python::with_gil(|py| {
243
- /// let string = PyString::new(py, "foo");
244
- ///
245
- /// let wrapped = SendWrapper::new(string);
246
- ///
247
- /// py.allow_threads(|| {
248
- /// let sneaky: &PyString = *wrapped;
249
- ///
250
- /// println!("{:?}", sneaky);
251
- /// });
252
- /// });
253
- /// ```
254
- ///
255
- /// This also enables using non-[`Send`] types in `allow_threads`,
256
- /// at least if they are not also bound to the GIL:
257
- ///
258
- /// ```rust
259
- /// # use pyo3::prelude::*;
260
- /// use std::rc::Rc;
261
- ///
262
- /// Python::with_gil(|py| {
263
- /// let rc = Rc::new(42);
264
- ///
265
- /// py.allow_threads(|| {
266
- /// println!("{:?}", rc);
267
- /// });
268
- /// });
269
- /// ```
270
- pub unsafe auto trait Ungil { }
271
- }
189
+ mod nightly {
190
+ macro_rules! define {
191
+ ( $( $tt: tt) * ) => { $( $tt) * }
192
+ }
272
193
273
- #[ cfg( feature = "nightly" ) ]
274
- mod negative_impls {
275
- use super :: Ungil ;
194
+ define ! {
195
+ /// Types that are safe to access while the GIL is not held.
196
+ ///
197
+ /// # Safety
198
+ ///
199
+ /// The type must not carry borrowed Python references or, if it does, not allow access to them if
200
+ /// the GIL is not held.
201
+ ///
202
+ /// See the [module-level documentation](self) for more information.
203
+ ///
204
+ /// # Examples
205
+ ///
206
+ /// Types which are `Ungil` cannot be used in contexts where the GIL was released, e.g.
207
+ ///
208
+ /// ```compile_fail
209
+ /// # use pyo3::prelude::*;
210
+ /// # use pyo3::types::PyString;
211
+ /// Python::with_gil(|py| {
212
+ /// let string = PyString::new(py, "foo");
213
+ ///
214
+ /// py.allow_threads(|| {
215
+ /// println!("{:?}", string);
216
+ /// });
217
+ /// });
218
+ /// ```
219
+ ///
220
+ /// This applies to the GIL token `Python` itself as well, e.g.
221
+ ///
222
+ /// ```compile_fail
223
+ /// # use pyo3::prelude::*;
224
+ /// Python::with_gil(|py| {
225
+ /// py.allow_threads(|| {
226
+ /// drop(py);
227
+ /// });
228
+ /// });
229
+ /// ```
230
+ ///
231
+ /// On nightly Rust, this is not based on the [`Send`] auto trait and hence we are able
232
+ /// to prevent incorrectly circumventing it using e.g. the [`send_wrapper`](https://docs.rs/send_wrapper/) crate:
233
+ ///
234
+ /// ```compile_fail
235
+ /// # use pyo3::prelude::*;
236
+ /// # use pyo3::types::PyString;
237
+ /// use send_wrapper::SendWrapper;
238
+ ///
239
+ /// Python::with_gil(|py| {
240
+ /// let string = PyString::new(py, "foo");
241
+ ///
242
+ /// let wrapped = SendWrapper::new(string);
243
+ ///
244
+ /// py.allow_threads(|| {
245
+ /// let sneaky: &PyString = *wrapped;
246
+ ///
247
+ /// println!("{:?}", sneaky);
248
+ /// });
249
+ /// });
250
+ /// ```
251
+ ///
252
+ /// This also enables using non-[`Send`] types in `allow_threads`,
253
+ /// at least if they are not also bound to the GIL:
254
+ ///
255
+ /// ```rust
256
+ /// # use pyo3::prelude::*;
257
+ /// use std::rc::Rc;
258
+ ///
259
+ /// Python::with_gil(|py| {
260
+ /// let rc = Rc::new(42);
261
+ ///
262
+ /// py.allow_threads(|| {
263
+ /// println!("{:?}", rc);
264
+ /// });
265
+ /// });
266
+ /// ```
267
+ pub unsafe auto trait Ungil { }
268
+ }
276
269
277
270
impl !Ungil for crate :: Python < ' _ > { }
278
271
@@ -299,6 +292,9 @@ mod negative_impls {
299
292
impl !Ungil for crate :: ffi:: PyArena { }
300
293
}
301
294
295
+ #[ cfg( feature = "nightly" ) ]
296
+ pub use nightly:: Ungil ;
297
+
302
298
/// A marker token that represents holding the GIL.
303
299
///
304
300
/// It serves three main purposes:
0 commit comments