-
Notifications
You must be signed in to change notification settings - Fork 791
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add compile_error test for PyClass: Send
- Loading branch information
1 parent
7a72713
commit 6e42c2a
Showing
3 changed files
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use pyo3::prelude::*; | ||
use std::rc::Rc; | ||
|
||
#[pyclass] | ||
struct NotThreadSafe { | ||
data: Rc<i32> | ||
} | ||
|
||
fn main() { | ||
let gil = Python::acquire_gil(); | ||
let py = gil.python(); | ||
|
||
let obj = PyCell::new(py, NotThreadSafe { data: Rc::new(5) }).unwrap().to_object(py); | ||
drop(gil); | ||
|
||
std::thread::spawn(move || { | ||
let gil = Python::acquire_gil(); | ||
let py = gil.python(); | ||
|
||
// Uh oh, moved Rc to a new thread! | ||
let c: &PyCell<NotThreadSafe> = obj.as_ref(py).downcast().unwrap(); | ||
|
||
assert_eq!(*c.borrow().data, 5); | ||
}).join().unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
error[E0277]: `std::rc::Rc<i32>` cannot be sent between threads safely | ||
--> $DIR/pyclass_send.rs:4:1 | ||
| | ||
4 | #[pyclass] | ||
| ^^^^^^^^^^ `std::rc::Rc<i32>` cannot be sent between threads safely | ||
| | ||
::: $WORKSPACE/src/pyclass.rs:76:76 | ||
| | ||
76 | PyTypeInfo<Layout = PyCell<Self>> + Sized + PyClassAlloc + PyMethods + Send | ||
| ---- required by this bound in `pyo3::pyclass::PyClass` | ||
| | ||
= help: within `NotThreadSafe`, the trait `std::marker::Send` is not implemented for `std::rc::Rc<i32>` | ||
= note: required because it appears within the type `NotThreadSafe` | ||
= note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info) |