about summary refs log tree commit diff
path: root/src/librustc_driver
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2015-01-27 12:20:58 -0800
committerAlex Crichton <alex@alexcrichton.com>2015-02-01 11:08:15 -0800
commit70ed3a48dfa301c5bb56de3e0a7c25214539b83c (patch)
treec05dbc24078626ff2a2aa6de769343756528df30 /src/librustc_driver
parentf1398d2736f132dd4af828b3d9134691f924b7e1 (diff)
downloadrust-70ed3a48dfa301c5bb56de3e0a7c25214539b83c.tar.gz
rust-70ed3a48dfa301c5bb56de3e0a7c25214539b83c.zip
std: Add a new `env` module
This is an implementation of [RFC 578][rfc] which adds a new `std::env` module
to replace most of the functionality in the current `std::os` module. More
details can be found in the RFC itself, but as a summary the following methods
have all been deprecated:

[rfc]: https://github.com/rust-lang/rfcs/pull/578

* `os::args_as_bytes`   => `env::args`
* `os::args`            => `env::args`
* `os::consts`          => `env::consts`
* `os::dll_filename`    => no replacement, use `env::consts` directly
* `os::page_size`       => `env::page_size`
* `os::make_absolute`   => use `env::current_dir` + `join` instead
* `os::getcwd`          => `env::current_dir`
* `os::change_dir`      => `env::set_current_dir`
* `os::homedir`         => `env::home_dir`
* `os::tmpdir`          => `env::temp_dir`
* `os::join_paths`      => `env::join_paths`
* `os::split_paths`     => `env::split_paths`
* `os::self_exe_name`   => `env::current_exe`
* `os::self_exe_path`   => use `env::current_exe` + `pop`
* `os::set_exit_status` => `env::set_exit_status`
* `os::get_exit_status` => `env::get_exit_status`
* `os::env`             => `env::vars`
* `os::env_as_bytes`    => `env::vars`
* `os::getenv`          => `env::var` or `env::var_string`
* `os::getenv_as_bytes` => `env::var`
* `os::setenv`          => `env::set_var`
* `os::unsetenv`        => `env::remove_var`

Many function signatures have also been tweaked for various purposes, but the
main changes were:

* `Vec`-returning APIs now all return iterators instead
* All APIs are now centered around `OsString` instead of `Vec<u8>` or `String`.
  There is currently on convenience API, `env::var_string`, which can be used to
  get the value of an environment variable as a unicode `String`.

All old APIs are `#[deprecated]` in-place and will remain for some time to allow
for migrations. The semantics of the APIs have been tweaked slightly with regard
to dealing with invalid unicode (panic instead of replacement).

The new `std::env` module is all contained within the `env` feature, so crates
must add the following to access the new APIs:

    #![feature(env)]

[breaking-change]
Diffstat (limited to 'src/librustc_driver')
-rw-r--r--src/librustc_driver/driver.rs23
-rw-r--r--src/librustc_driver/lib.rs13
2 files changed, 19 insertions, 17 deletions
diff --git a/src/librustc_driver/driver.rs b/src/librustc_driver/driver.rs
index 9e00844b7ee..4aa4193d511 100644
--- a/src/librustc_driver/driver.rs
+++ b/src/librustc_driver/driver.rs
@@ -30,9 +30,10 @@ use rustc_privacy;
 
 use serialize::json;
 
-use std::old_io;
+use std::env;
+use std::ffi::OsString;
 use std::old_io::fs;
-use std::os;
+use std::old_io;
 use syntax::ast;
 use syntax::ast_map;
 use syntax::attr;
@@ -460,12 +461,12 @@ pub fn phase_2_configure_and_expand(sess: &Session,
             // dependent dlls. Note that this uses cfg!(windows) as opposed to
             // targ_cfg because syntax extensions are always loaded for the host
             // compiler, not for the target.
-            let mut _old_path = String::new();
+            let mut _old_path = OsString::from_str("");
             if cfg!(windows) {
-                _old_path = os::getenv("PATH").unwrap_or(_old_path);
+                _old_path = env::var("PATH").unwrap_or(_old_path);
                 let mut new_path = sess.host_filesearch(PathKind::All).get_dylib_search_paths();
-                new_path.extend(os::split_paths(&_old_path[]).into_iter());
-                os::setenv("PATH", os::join_paths(&new_path[]).unwrap());
+                new_path.extend(env::split_paths(&_old_path));
+                env::set_var("PATH", &env::join_paths(new_path.iter()).unwrap());
             }
             let cfg = syntax::ext::expand::ExpansionConfig {
                 crate_name: crate_name.to_string(),
@@ -478,7 +479,7 @@ pub fn phase_2_configure_and_expand(sess: &Session,
                                               syntax_exts,
                                               krate);
             if cfg!(windows) {
-                os::setenv("PATH", _old_path);
+                env::set_var("PATH", &_old_path);
             }
             ret
         }
@@ -734,10 +735,10 @@ pub fn phase_5_run_llvm_passes(sess: &Session,
 pub fn phase_6_link_output(sess: &Session,
                            trans: &trans::CrateTranslation,
                            outputs: &OutputFilenames) {
-    let old_path = os::getenv("PATH").unwrap_or_else(||String::new());
+    let old_path = env::var("PATH").unwrap_or(OsString::from_str(""));
     let mut new_path = sess.host_filesearch(PathKind::All).get_tools_search_paths();
-    new_path.extend(os::split_paths(&old_path[]).into_iter());
-    os::setenv("PATH", os::join_paths(&new_path[]).unwrap());
+    new_path.extend(env::split_paths(&old_path));
+    env::set_var("PATH", &env::join_paths(new_path.iter()).unwrap());
 
     time(sess.time_passes(), "linking", (), |_|
          link::link_binary(sess,
@@ -745,7 +746,7 @@ pub fn phase_6_link_output(sess: &Session,
                            outputs,
                            &trans.link.crate_name[]));
 
-    os::setenv("PATH", old_path);
+    env::set_var("PATH", &old_path);
 }
 
 fn escape_dep_filename(filename: &str) -> String {
diff --git a/src/librustc_driver/lib.rs b/src/librustc_driver/lib.rs
index 2eada1ff174..6230f0b14a2 100644
--- a/src/librustc_driver/lib.rs
+++ b/src/librustc_driver/lib.rs
@@ -26,6 +26,7 @@
 #![feature(box_syntax)]
 #![feature(collections)]
 #![feature(core)]
+#![feature(env)]
 #![feature(int_uint)]
 #![feature(io)]
 #![feature(libc)]
@@ -74,7 +75,7 @@ use rustc::util::common::time;
 use std::cmp::Ordering::Equal;
 use std::old_io;
 use std::iter::repeat;
-use std::os;
+use std::env;
 use std::sync::mpsc::channel;
 use std::thread;
 
@@ -252,7 +253,7 @@ pub fn get_unstable_features_setting() -> UnstableFeatures {
     // subverting the unstable features lints
     let bootstrap_secret_key = option_env!("CFG_BOOTSTRAP_KEY");
     // The matching key to the above, only known by the build system
-    let bootstrap_provided_key = os::getenv("RUSTC_BOOTSTRAP_KEY");
+    let bootstrap_provided_key = env::var_string("RUSTC_BOOTSTRAP_KEY").ok();
     match (disable_unstable_features, bootstrap_secret_key, bootstrap_provided_key) {
         (_, Some(ref s), Some(ref p)) if s == p => UnstableFeatures::Cheat,
         (true, _, _) => UnstableFeatures::Disallow,
@@ -618,7 +619,7 @@ pub fn monitor<F:FnOnce()+Send>(f: F) {
 
     // FIXME: Hacks on hacks. If the env is trying to override the stack size
     // then *don't* set it explicitly.
-    if os::getenv("RUST_MIN_STACK").is_none() {
+    if env::var("RUST_MIN_STACK").is_none() {
         cfg = cfg.stack_size(STACK_SIZE);
     }
 
@@ -682,8 +683,8 @@ pub fn diagnostics_registry() -> diagnostics::registry::Registry {
 }
 
 pub fn main() {
-    let args = std::os::args();
-    let result = run(args);
-    std::os::set_exit_status(result);
+    let args = env::args().map(|s| s.into_string().unwrap());
+    let result = run(args.collect());
+    std::env::set_exit_status(result as i32);
 }