Skip to content

Instantly share code, notes, and snippets.

@saethlin
Created February 28, 2018 21:21
Show Gist options
  • Save saethlin/36764b3c9c499d137df13ddd0d4a958a to your computer and use it in GitHub Desktop.
Save saethlin/36764b3c9c499d137df13ddd0d4a958a to your computer and use it in GitHub Desktop.
Rust thread safety in a Python extension
#[macro_use]
extern crate cpython;
use std::rc::Rc;
pub struct Simulation {
pub star: Rc<Star>,
pub spots: Vec<Spot>,
}
pub struct Spot {
pub star: Rc<Star>,
}
pub struct Star {}
#[doc(hidden)]
pub use py_interface::PyInit_lather;
#[allow(missing_docs)]
mod py_interface {
use std::cell::RefCell;
py_module_initializer!(lather, initlather, PyInit_lather, |py, m| {
m.add_class::<PySimulation>(py)?;
Ok(())
});
py_class!(class PySimulation |py| {
data sim: RefCell<::Simulation>;
});
}
// Attempting to build this produces the following error:
/*
error[E0277]: the trait bound `std::rc::Rc<Star>: std::marker::Send` is not satisfied in `Simulation`
--> src/main.rs:29:5
|
29 | / py_class!(class PySimulation |py| {
30 | | data sim: RefCell<::Simulation>;
31 | | });
| |___________^ `std::rc::Rc<Star>` cannot be sent between threads safely
|
= help: within `Simulation`, the trait `std::marker::Send` is not implemented for `std::rc::Rc<Star>`
= note: required because it appears within the type `Simulation`
= note: required because of the requirements on the impl of `std::marker::Send` for `std::cell::RefCell<Simulation>`
= note: required by `cpython::py_class::data_init`
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment