@@ -148,6 +148,8 @@ use std::os::raw::c_int;
148
148
///
149
149
/// ```compile_fail
150
150
/// # use pyo3::prelude::*;
151
+ /// use std::rc::Rc;
152
+ ///
151
153
/// Python::with_gil(|py| {
152
154
/// let rc = Rc::new(42);
153
155
///
@@ -157,7 +159,8 @@ use std::os::raw::c_int;
157
159
/// });
158
160
/// ```
159
161
///
160
- /// This also implies that one can circumvent this protection using e.g. the [`send_wrapper`](https://docs.rs/send_wrapper/) crate:
162
+ /// This also implies that the interplay between `with_gil` and `allow_threads` is unsound, for example
163
+ /// one can circumvent this protection using the [`send_wrapper`](https://docs.rs/send_wrapper/) crate:
161
164
///
162
165
/// ```no_run
163
166
/// # use pyo3::prelude::*;
@@ -176,6 +179,9 @@ use std::os::raw::c_int;
176
179
/// });
177
180
/// });
178
181
/// ```
182
+ ///
183
+ /// Fixing this loophole on stable Rust has significant ergonomic issues, but it is fixed when using
184
+ /// nightly Rust and the `nightly` feature, c.f. [#2141](https://github.com/PyO3/pyo3/issues/2141).
179
185
#[ cfg_attr( docsrs, doc( cfg( all( ) ) ) ) ] // Hide the cfg flag
180
186
#[ cfg( not( feature = "nightly" ) ) ]
181
187
pub unsafe trait Ungil { }
@@ -240,6 +246,22 @@ unsafe impl<T: Send> Ungil for T {}
240
246
/// });
241
247
/// });
242
248
/// ```
249
+ ///
250
+ /// This also enables using non-[`Send`] types in `allow_threads`,
251
+ /// at least if they are not also bound to the GIL:
252
+ ///
253
+ /// ```rust
254
+ /// # use pyo3::prelude::*;
255
+ /// use std::rc::Rc;
256
+ ///
257
+ /// Python::with_gil(|py| {
258
+ /// let rc = Rc::new(42);
259
+ ///
260
+ /// py.allow_threads(|| {
261
+ /// println!("{:?}", rc);
262
+ /// });
263
+ /// });
264
+ /// ```
243
265
#[ cfg( feature = "nightly" ) ]
244
266
pub unsafe auto trait Ungil { }
245
267
0 commit comments