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]
This commit is contained in:
@@ -21,7 +21,7 @@ use ext::base;
|
||||
use ext::build::AstBuilder;
|
||||
use parse::token;
|
||||
|
||||
use std::os;
|
||||
use std::env;
|
||||
|
||||
pub fn expand_option_env<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
|
||||
-> Box<base::MacResult+'cx> {
|
||||
@@ -30,8 +30,8 @@ pub fn expand_option_env<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenT
|
||||
Some(v) => v
|
||||
};
|
||||
|
||||
let e = match os::getenv(&var[]) {
|
||||
None => {
|
||||
let e = match env::var_string(&var[]) {
|
||||
Err(..) => {
|
||||
cx.expr_path(cx.path_all(sp,
|
||||
true,
|
||||
vec!(cx.ident_of("std"),
|
||||
@@ -48,7 +48,7 @@ pub fn expand_option_env<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenT
|
||||
ast::MutImmutable)),
|
||||
Vec::new()))
|
||||
}
|
||||
Some(s) => {
|
||||
Ok(s) => {
|
||||
cx.expr_call_global(sp,
|
||||
vec!(cx.ident_of("std"),
|
||||
cx.ident_of("option"),
|
||||
@@ -101,12 +101,12 @@ pub fn expand_env<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
|
||||
}
|
||||
}
|
||||
|
||||
let e = match os::getenv(var.get()) {
|
||||
None => {
|
||||
let e = match env::var_string(var.get()) {
|
||||
Err(..) => {
|
||||
cx.span_err(sp, msg.get());
|
||||
cx.expr_usize(sp, 0)
|
||||
}
|
||||
Some(s) => cx.expr_str(sp, token::intern_and_get_ident(&s[]))
|
||||
Ok(s) => cx.expr_str(sp, token::intern_and_get_ident(&s[]))
|
||||
};
|
||||
MacExpr::new(e)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user