Reputation: 14604
I've hit the issue a couple of times of trying to pass things into the spawn
function (to create a new thread/task) and have the compiler tell me error: cannot capture variable of type "blah blah", which does not fulfill "Send", in a bounded closure
.
Is there a way to transform a type be able to fulfill "Send" or is that fixed based on some set of rules?
For example, I can easily implement the ToStr
trait by using a directive like this:
#[deriving(ToStr, Rand)]
struct Point {
x: int,
y: int,
}
Can I do something similar for the Send
trait? Or are "kind" Traits treated differently?
Here's a concrete example of this issue - is there some way to overcome it?
fn create_process_options(cmdinfo: &CmdInfo) -> (ProcessOptions, Option<FileDesc>) {
// ... omitted
}
// "po" is of type std::run::ProcessOptions
let (po, filedesc_opt) = create_process_options(&cmdinfo);
spawn(proc() {
let mut ps = Process::new(cmdinfo.program, cmdinfo.args, po).expect("darn");
ps.finish();
});
Compiler error:
error: cannot capture variable of type `std::run::ProcessOptions<>`, which does not fulfill `Send`, in a bounded closure
let mut process = Process::new(cmdinfo.program, cmdinfo.args, po).expect("darn");
^~
note: this closure's environment must satisfy `Send`
let mut process = Process::new(cmdinfo.program, cmdinfo.args, po).expect("darn");
Upvotes: 4
Views: 5384
Reputation: 7236
Send is a rust Kind, the other things you mentioned are Traits. While both can be used to bound generics, they are in fact quite different. You have to opt-in to a Trait, but what Kinds a type has are inferred based on their contents - besides changing the contents you can't change the Kind of a type.
For most Kinds, the rule is "a type X is of Kind Y if all the members of X are of Kind Y."
In this case, since being Send requires that you fulfill 'static
, which means that they don't contain any non-'static
references. Since ProcessOptions
contains a non-static lifetime Option<&'a Path>
, like Chris Morgan detailed in his comment, ProcessOptions
does not fulfill Send.
Upvotes: 8