You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Rust book says that raw pointers "are considered sendable (if their contents is considered sendable)".
However from what I can see, the compiler generates the error the trait core::marker::Send is not implemented for the type when it encounters a raw pointer to a Sendable type.
It could be that I am doing something wrong, it could be that the documentation is unclear or it could be that the error message isn't helpful at indicating whatever the real problem with this code is.
test.rs:15:21: 15:34 error: the trait `core::marker::Send` is not implemented for the type`*const ShouldBeSendable` [E0277]
test.rs:15 let something = Thread::spawn(closure);
^~~~~~~~~~~~~
test.rs:15:21: 15:34 note: `*const ShouldBeSendable` cannot be sent between threads safely
test.rs:15 let something = Thread::spawn(closure);
^~~~~~~~~~~~~
error: aborting due to previous error
@drewcrawford You can use the Unique wrapper to make your raw pointer Sendable. This modified version of your code works:
use std::ptr::Unique;use std::thread::Thread;#[derive(Copy)]structShouldBeSendable{x:i32}unsafeimpl std::marker::SendforShouldBeSendable{}fnmain(){let ptr :*mutShouldBeSendable = &mutShouldBeSendable{x:5};// this is not `Send`let sendablePtr = Unique(ptr);// but this is!let closure = move |:| {// `sendablePtr` con be moved inside this closurelet ptr = sendablePtr.0;// unpack the raw pointerprintln!("{}",unsafe{(*ptr).x })};let something = Thread::scoped(closure).join();}
Activity
huonw commentedon Jan 28, 2015
This changed recently, leaving the docs out of date. Thanks for noticing and filing a bug!
japaric commentedon Jan 28, 2015
@drewcrawford You can use the
Uniquewrapper to make your raw pointerSendable. This modified version of your code works:Remove incorrect statement about raw pointers.
rubenrua commentedon Apr 24, 2018
@japaric example for Rust 1.25 using
NonNull.https://play.rust-lang.org/?gist=1ce2532a0eefc60695663c26faddebe1&version=stable
Merge pull request rust-lang#21709 from Wilfred/race_condition_flyche…