I was designing a type that can be sent between threads or not, depending if it's an Owned type or not.
This code:
use std::cell::RefCell;
use std::rc::Rc;
pub enum M<'a, T> {
Owned(),
Ref(Rc<RefCell<&'a mut [T]>>)
}
fn main() {
let m: M<'static, u8> = M::Owned();
std::thread::spawn(||{
let m = m;
});
}
gives
error[E0277]: `Rc<RefCell<&mut [_]>>` cannot be sent between threads safely
--> src/main.rs:11:5
|
11 | std::thread::spawn(||{
| _____^^^^^^^^^^^^^^^^^^_-
| | |
| | `Rc<RefCell<&mut [_]>>` cannot be sent between threads safely
12 | | let m = m;
13 | | });
| |_____- within this `[closure@src/main.rs:11:24: 13:6]`
I understand that this is not Send because RefCell is !Send. I think this is the only thing preventing M from being Send. Is there a RefCell alternative that is Send?