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

std: Improve downstream codegen in Command::env #64186

Merged
merged 1 commit into from
Sep 6, 2019
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
8 changes: 5 additions & 3 deletions src/libstd/sys/cloudabi/shims/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ use crate::io;
use crate::sys::fs::File;
use crate::sys::pipe::AnonPipe;
use crate::sys::{unsupported, Void};
use crate::sys_common::process::{CommandEnv, DefaultEnvKey};
use crate::sys_common::process::CommandEnv;

pub use crate::ffi::OsString as EnvKey;

////////////////////////////////////////////////////////////////////////////////
// Command
////////////////////////////////////////////////////////////////////////////////

pub struct Command {
env: CommandEnv<DefaultEnvKey>,
env: CommandEnv,
}

// passed back to std::process with the pipes connected to the child, if any
Expand All @@ -37,7 +39,7 @@ impl Command {

pub fn arg(&mut self, _arg: &OsStr) {}

pub fn env_mut(&mut self) -> &mut CommandEnv<DefaultEnvKey> {
pub fn env_mut(&mut self) -> &mut CommandEnv {
&mut self.env
}

Expand Down
8 changes: 5 additions & 3 deletions src/libstd/sys/sgx/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ use crate::io;
use crate::sys::fs::File;
use crate::sys::pipe::AnonPipe;
use crate::sys::{unsupported, Void};
use crate::sys_common::process::{CommandEnv, DefaultEnvKey};
use crate::sys_common::process::CommandEnv;

pub use crate::ffi::OsString as EnvKey;

////////////////////////////////////////////////////////////////////////////////
// Command
////////////////////////////////////////////////////////////////////////////////

pub struct Command {
env: CommandEnv<DefaultEnvKey>
env: CommandEnv,
}

// passed back to std::process with the pipes connected to the child, if any
Expand All @@ -38,7 +40,7 @@ impl Command {
pub fn arg(&mut self, _arg: &OsStr) {
}

pub fn env_mut(&mut self) -> &mut CommandEnv<DefaultEnvKey> {
pub fn env_mut(&mut self) -> &mut CommandEnv {
&mut self.env
}

Expand Down
1 change: 1 addition & 0 deletions src/libstd/sys/unix/process/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub use self::process_common::{Command, ExitStatus, ExitCode, Stdio, StdioPipes};
pub use self::process_inner::Process;
pub use crate::ffi::OsString as EnvKey;

mod process_common;
#[cfg(not(target_os = "fuchsia"))]
Expand Down
8 changes: 4 additions & 4 deletions src/libstd/sys/unix/process/process_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::ptr;
use crate::sys::fd::FileDesc;
use crate::sys::fs::{File, OpenOptions};
use crate::sys::pipe::{self, AnonPipe};
use crate::sys_common::process::{CommandEnv, DefaultEnvKey};
use crate::sys_common::process::CommandEnv;
use crate::collections::BTreeMap;

use libc::{c_int, gid_t, uid_t, c_char, EXIT_SUCCESS, EXIT_FAILURE};
Expand Down Expand Up @@ -69,7 +69,7 @@ pub struct Command {
program: CString,
args: Vec<CString>,
argv: Argv,
env: CommandEnv<DefaultEnvKey>,
env: CommandEnv,

cwd: Option<CString>,
uid: Option<uid_t>,
Expand Down Expand Up @@ -201,7 +201,7 @@ impl Command {
self.stderr = Some(stderr);
}

pub fn env_mut(&mut self) -> &mut CommandEnv<DefaultEnvKey> {
pub fn env_mut(&mut self) -> &mut CommandEnv {
&mut self.env
}

Expand Down Expand Up @@ -271,7 +271,7 @@ impl CStringArray {
}
}

fn construct_envp(env: BTreeMap<DefaultEnvKey, OsString>, saw_nul: &mut bool) -> CStringArray {
fn construct_envp(env: BTreeMap<OsString, OsString>, saw_nul: &mut bool) -> CStringArray {
let mut result = CStringArray::with_capacity(env.len());
for (k, v) in env {
let mut k: OsString = k.into();
Expand Down
10 changes: 6 additions & 4 deletions src/libstd/sys/vxworks/process/process_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ use crate::ptr;
use crate::sys::fd::FileDesc;
use crate::sys::fs::{File, OpenOptions};
use crate::sys::pipe::{self, AnonPipe};
use crate::sys_common::process::{CommandEnv, DefaultEnvKey};
use crate::sys_common::process::CommandEnv;
use crate::collections::BTreeMap;

use libc::{c_int, gid_t, uid_t, c_char, EXIT_SUCCESS, EXIT_FAILURE};

pub use crate::ffi::OsString as EnvKey;

////////////////////////////////////////////////////////////////////////////////
// Command
////////////////////////////////////////////////////////////////////////////////
Expand All @@ -37,7 +39,7 @@ pub struct Command {
program: CString,
args: Vec<CString>,
argv: Argv,
env: CommandEnv<DefaultEnvKey>,
env: CommandEnv,

cwd: Option<CString>,
uid: Option<uid_t>,
Expand Down Expand Up @@ -170,7 +172,7 @@ impl Command {
self.stderr = Some(stderr);
}

pub fn env_mut(&mut self) -> &mut CommandEnv<DefaultEnvKey> {
pub fn env_mut(&mut self) -> &mut CommandEnv {
&mut self.env
}

Expand Down Expand Up @@ -240,7 +242,7 @@ impl CStringArray {
}
}

fn construct_envp(env: BTreeMap<DefaultEnvKey, OsString>, saw_nul: &mut bool) -> CStringArray {
fn construct_envp(env: BTreeMap<OsString, OsString>, saw_nul: &mut bool) -> CStringArray {
let mut result = CStringArray::with_capacity(env.len());
for (k, v) in env {
let mut k: OsString = k.into();
Expand Down
8 changes: 5 additions & 3 deletions src/libstd/sys/wasi/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ use crate::io;
use crate::sys::fs::File;
use crate::sys::pipe::AnonPipe;
use crate::sys::{unsupported, Void};
use crate::sys_common::process::{CommandEnv, DefaultEnvKey};
use crate::sys_common::process::CommandEnv;

pub use crate::ffi::OsString as EnvKey;

////////////////////////////////////////////////////////////////////////////////
// Command
////////////////////////////////////////////////////////////////////////////////

pub struct Command {
env: CommandEnv<DefaultEnvKey>
env: CommandEnv
}

// passed back to std::process with the pipes connected to the child, if any
Expand All @@ -38,7 +40,7 @@ impl Command {
pub fn arg(&mut self, _arg: &OsStr) {
}

pub fn env_mut(&mut self) -> &mut CommandEnv<DefaultEnvKey> {
pub fn env_mut(&mut self) -> &mut CommandEnv {
&mut self.env
}

Expand Down
8 changes: 5 additions & 3 deletions src/libstd/sys/wasm/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ use crate::io;
use crate::sys::fs::File;
use crate::sys::pipe::AnonPipe;
use crate::sys::{unsupported, Void};
use crate::sys_common::process::{CommandEnv, DefaultEnvKey};
use crate::sys_common::process::CommandEnv;

pub use crate::ffi::OsString as EnvKey;

////////////////////////////////////////////////////////////////////////////////
// Command
////////////////////////////////////////////////////////////////////////////////

pub struct Command {
env: CommandEnv<DefaultEnvKey>
env: CommandEnv,
}

// passed back to std::process with the pipes connected to the child, if any
Expand All @@ -38,7 +40,7 @@ impl Command {
pub fn arg(&mut self, _arg: &OsStr) {
}

pub fn env_mut(&mut self) -> &mut CommandEnv<DefaultEnvKey> {
pub fn env_mut(&mut self) -> &mut CommandEnv {
&mut self.env
}

Expand Down
24 changes: 11 additions & 13 deletions src/libstd/sys/windows/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::sys::pipe::{self, AnonPipe};
use crate::sys::stdio;
use crate::sys::cvt;
use crate::sys_common::{AsInner, FromInner, IntoInner};
use crate::sys_common::process::{CommandEnv, EnvKey};
use crate::sys_common::process::CommandEnv;
use crate::borrow::Borrow;

use libc::{c_void, EXIT_SUCCESS, EXIT_FAILURE};
Expand All @@ -30,30 +30,28 @@ use libc::{c_void, EXIT_SUCCESS, EXIT_FAILURE};

#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
#[doc(hidden)]
pub struct WindowsEnvKey(OsString);
pub struct EnvKey(OsString);

impl From<OsString> for WindowsEnvKey {
impl From<OsString> for EnvKey {
fn from(k: OsString) -> Self {
let mut buf = k.into_inner().into_inner();
buf.make_ascii_uppercase();
WindowsEnvKey(FromInner::from_inner(FromInner::from_inner(buf)))
EnvKey(FromInner::from_inner(FromInner::from_inner(buf)))
}
}

impl From<WindowsEnvKey> for OsString {
fn from(k: WindowsEnvKey) -> Self { k.0 }
impl From<EnvKey> for OsString {
fn from(k: EnvKey) -> Self { k.0 }
}

impl Borrow<OsStr> for WindowsEnvKey {
impl Borrow<OsStr> for EnvKey {
fn borrow(&self) -> &OsStr { &self.0 }
}

impl AsRef<OsStr> for WindowsEnvKey {
impl AsRef<OsStr> for EnvKey {
fn as_ref(&self) -> &OsStr { &self.0 }
}

impl EnvKey for WindowsEnvKey {}


fn ensure_no_nuls<T: AsRef<OsStr>>(str: T) -> io::Result<T> {
if str.as_ref().encode_wide().any(|b| b == 0) {
Expand All @@ -66,7 +64,7 @@ fn ensure_no_nuls<T: AsRef<OsStr>>(str: T) -> io::Result<T> {
pub struct Command {
program: OsString,
args: Vec<OsString>,
env: CommandEnv<WindowsEnvKey>,
env: CommandEnv,
cwd: Option<OsString>,
flags: u32,
detach: bool, // not currently exposed in std::process
Expand Down Expand Up @@ -110,7 +108,7 @@ impl Command {
pub fn arg(&mut self, arg: &OsStr) {
self.args.push(arg.to_os_string())
}
pub fn env_mut(&mut self) -> &mut CommandEnv<WindowsEnvKey> {
pub fn env_mut(&mut self) -> &mut CommandEnv {
&mut self.env
}
pub fn cwd(&mut self, dir: &OsStr) {
Expand Down Expand Up @@ -498,7 +496,7 @@ fn make_command_line(prog: &OsStr, args: &[OsString]) -> io::Result<Vec<u16>> {
}
}

fn make_envp(maybe_env: Option<BTreeMap<WindowsEnvKey, OsString>>)
fn make_envp(maybe_env: Option<BTreeMap<EnvKey, OsString>>)
-> io::Result<(*mut c_void, Vec<u16>)> {
// On Windows we pass an "environment block" which is not a char**, but
// rather a concatenation of null-terminated k=v\0 sequences, with a final
Expand Down
51 changes: 14 additions & 37 deletions src/libstd/sys_common/process.rs
Original file line number Diff line number Diff line change
@@ -1,47 +1,20 @@
#![allow(dead_code)]
#![unstable(feature = "process_internals", issue = "0")]

use crate::ffi::{OsStr, OsString};
use crate::env;
use crate::collections::BTreeMap;
use crate::borrow::Borrow;

pub trait EnvKey:
From<OsString> + Into<OsString> +
Borrow<OsStr> + Borrow<Self> + AsRef<OsStr> +
Ord + Clone {}

// Implement a case-sensitive environment variable key
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub struct DefaultEnvKey(OsString);

impl From<OsString> for DefaultEnvKey {
fn from(k: OsString) -> Self { DefaultEnvKey(k) }
}

impl From<DefaultEnvKey> for OsString {
fn from(k: DefaultEnvKey) -> Self { k.0 }
}

impl Borrow<OsStr> for DefaultEnvKey {
fn borrow(&self) -> &OsStr { &self.0 }
}

impl AsRef<OsStr> for DefaultEnvKey {
fn as_ref(&self) -> &OsStr { &self.0 }
}

impl EnvKey for DefaultEnvKey {}
use crate::env;
use crate::ffi::{OsStr, OsString};
use crate::sys::process::EnvKey;

// Stores a set of changes to an environment
#[derive(Clone, Debug)]
pub struct CommandEnv<K> {
pub struct CommandEnv {
clear: bool,
saw_path: bool,
vars: BTreeMap<K, Option<OsString>>
vars: BTreeMap<EnvKey, Option<OsString>>
}

impl<K: EnvKey> Default for CommandEnv<K> {
impl Default for CommandEnv {
fn default() -> Self {
CommandEnv {
clear: false,
Expand All @@ -51,10 +24,10 @@ impl<K: EnvKey> Default for CommandEnv<K> {
}
}

impl<K: EnvKey> CommandEnv<K> {
impl CommandEnv {
// Capture the current environment with these changes applied
pub fn capture(&self) -> BTreeMap<K, OsString> {
let mut result = BTreeMap::<K, OsString>::new();
pub fn capture(&self) -> BTreeMap<EnvKey, OsString> {
let mut result = BTreeMap::<EnvKey, OsString>::new();
if !self.clear {
for (k, v) in env::vars_os() {
result.insert(k.into(), v);
Expand Down Expand Up @@ -90,7 +63,7 @@ impl<K: EnvKey> CommandEnv<K> {
!self.clear && self.vars.is_empty()
}

pub fn capture_if_changed(&self) -> Option<BTreeMap<K, OsString>> {
pub fn capture_if_changed(&self) -> Option<BTreeMap<EnvKey, OsString>> {
if self.is_unchanged() {
None
} else {
Expand All @@ -103,6 +76,7 @@ impl<K: EnvKey> CommandEnv<K> {
self.maybe_saw_path(&key);
self.vars.insert(key.to_owned().into(), Some(value.to_owned()));
}

pub fn remove(&mut self, key: &OsStr) {
self.maybe_saw_path(&key);
if self.clear {
Expand All @@ -111,13 +85,16 @@ impl<K: EnvKey> CommandEnv<K> {
self.vars.insert(key.to_owned().into(), None);
}
}

pub fn clear(&mut self) {
self.clear = true;
self.vars.clear();
}

pub fn have_changed_path(&self) -> bool {
self.saw_path || self.clear
}

fn maybe_saw_path(&mut self, key: &OsStr) {
if !self.saw_path && key == "PATH" {
self.saw_path = true;
Expand Down