aboutsummaryrefslogtreecommitdiff
path: root/ctr-std/src/sys_common/process.rs
diff options
context:
space:
mode:
authorFenrir <[email protected]>2018-01-21 14:06:28 -0700
committerFenrirWolf <[email protected]>2018-01-21 19:16:33 -0700
commit23be3f4885688e5e0011005e2295c75168854c0a (patch)
treedd0850f9c73c489e114a761d5c0757f3dbec3a65 /ctr-std/src/sys_common/process.rs
parentUpdate CI for Rust nightly-2017-12-01 + other fixes (diff)
downloadctru-rs-23be3f4885688e5e0011005e2295c75168854c0a.tar.xz
ctru-rs-23be3f4885688e5e0011005e2295c75168854c0a.zip
Recreate ctr-std from latest nightly
Diffstat (limited to 'ctr-std/src/sys_common/process.rs')
-rw-r--r--ctr-std/src/sys_common/process.rs124
1 files changed, 124 insertions, 0 deletions
diff --git a/ctr-std/src/sys_common/process.rs b/ctr-std/src/sys_common/process.rs
new file mode 100644
index 0000000..fd1a5fd
--- /dev/null
+++ b/ctr-std/src/sys_common/process.rs
@@ -0,0 +1,124 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#![allow(dead_code)]
+#![unstable(feature = "process_internals", issue = "0")]
+
+use ffi::{OsStr, OsString};
+use env;
+use collections::BTreeMap;
+use alloc::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 {}
+
+// Stores a set of changes to an environment
+#[derive(Clone, Debug)]
+pub struct CommandEnv<K> {
+ clear: bool,
+ vars: BTreeMap<K, Option<OsString>>
+}
+
+impl<K: EnvKey> Default for CommandEnv<K> {
+ fn default() -> Self {
+ CommandEnv {
+ clear: false,
+ vars: Default::default()
+ }
+ }
+}
+
+impl<K: EnvKey> CommandEnv<K> {
+ // Capture the current environment with these changes applied
+ pub fn capture(&self) -> BTreeMap<K, OsString> {
+ let mut result = BTreeMap::<K, OsString>::new();
+ if !self.clear {
+ for (k, v) in env::vars_os() {
+ result.insert(k.into(), v);
+ }
+ }
+ for (k, maybe_v) in &self.vars {
+ if let &Some(ref v) = maybe_v {
+ result.insert(k.clone(), v.clone());
+ } else {
+ result.remove(k);
+ }
+ }
+ result
+ }
+
+ // Apply these changes directly to the current environment
+ pub fn apply(&self) {
+ if self.clear {
+ for (k, _) in env::vars_os() {
+ env::remove_var(k);
+ }
+ }
+ for (key, maybe_val) in self.vars.iter() {
+ if let &Some(ref val) = maybe_val {
+ env::set_var(key, val);
+ } else {
+ env::remove_var(key);
+ }
+ }
+ }
+
+ pub fn is_unchanged(&self) -> bool {
+ !self.clear && self.vars.is_empty()
+ }
+
+ pub fn capture_if_changed(&self) -> Option<BTreeMap<K, OsString>> {
+ if self.is_unchanged() {
+ None
+ } else {
+ Some(self.capture())
+ }
+ }
+
+ // The following functions build up changes
+ pub fn set(&mut self, key: &OsStr, value: &OsStr) {
+ self.vars.insert(key.to_owned().into(), Some(value.to_owned()));
+ }
+ pub fn remove(&mut self, key: &OsStr) {
+ if self.clear {
+ self.vars.remove(key);
+ } else {
+ self.vars.insert(key.to_owned().into(), None);
+ }
+ }
+ pub fn clear(&mut self) {
+ self.clear = true;
+ self.vars.clear();
+ }
+}