Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement Send for process::Command on unix #47760

Merged
merged 3 commits into from
Jan 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/libstd/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1843,4 +1843,10 @@ mod tests {
}
assert!(events > 0);
}

#[test]
fn test_command_implements_send() {
fn take_send_type<T: Send>(_: T) {}
take_send_type(Command::new(""))
}
}
16 changes: 11 additions & 5 deletions src/libstd/sys/unix/process/process_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub struct Command {
// other keys.
program: CString,
args: Vec<CString>,
argv: Vec<*const c_char>,
argv: Argv,
env: CommandEnv<DefaultEnvKey>,

cwd: Option<CString>,
Expand All @@ -58,6 +58,12 @@ pub struct Command {
stderr: Option<Stdio>,
}

// Create a new type for argv, so that we can make it `Send`
struct Argv(Vec<*const c_char>);

// It is safe to make Argv Send, because it contains pointers to memory owned by `Command.args`
unsafe impl Send for Argv {}

// passed back to std::process with the pipes connected to the child, if any
// were requested
pub struct StdioPipes {
Expand Down Expand Up @@ -92,7 +98,7 @@ impl Command {
let mut saw_nul = false;
let program = os2c(program, &mut saw_nul);
Command {
argv: vec![program.as_ptr(), ptr::null()],
argv: Argv(vec![program.as_ptr(), ptr::null()]),
program,
args: Vec::new(),
env: Default::default(),
Expand All @@ -111,8 +117,8 @@ impl Command {
// Overwrite the trailing NULL pointer in `argv` and then add a new null
// pointer.
let arg = os2c(arg, &mut self.saw_nul);
self.argv[self.args.len() + 1] = arg.as_ptr();
self.argv.push(ptr::null());
self.argv.0[self.args.len() + 1] = arg.as_ptr();
self.argv.0.push(ptr::null());

// Also make sure we keep track of the owned value to schedule a
// destructor for this memory.
Expand All @@ -133,7 +139,7 @@ impl Command {
self.saw_nul
}
pub fn get_argv(&self) -> &Vec<*const c_char> {
&self.argv
&self.argv.0
}

#[allow(dead_code)]
Expand Down