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

Amend RFC 517: Add material on std::env #578

Merged
merged 1 commit into from
Jan 30, 2015

Conversation

aturon
Copy link
Member

@aturon aturon commented Jan 13, 2015

The IO reform RFC is being split into several semi-independent pieces, posted as PRs like this one.

This RFC amendment adds material for std::env.

Rendered

**Environment variables**:

* `vars` (renamed from `env`): yields a vector of `(OsStrBuf, OsStrBuf)` pairs.
* `var` (renamed from `getenv`): take a value bounded by `IntoOsStrBuf`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IntoOsStrBuf or IntoOsStr?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that these APIs will need to go into an OsStrBuf regardless as they need to be passed to a system API which means they have to be nul-terminated (and a slice won't have the terminator).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These actually all ended up being AsStrBuf anyway, so I think the RFC just needs to be updated.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would make more sense for these APIs to return a Result<String, EnvError> where EnvError is:

enum EnvError {
  Missing,
  NotUnicode(OsString)
}

That way you can quickly and ergonomically get at a String, which is usually what you want, but still get at the OsString if you really need it (usually to present a good error message).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, to be more precise, I think it makes sense to have two variants of these APIs, one of which provides an Option<OsString> and one of which provides a Result.

I would propose var for the version that returns a Result<String, E> and raw_var for the one that returns an Option<OsString>

@arthurprs
Copy link

Please let's get the names consistent using cpu_count instead of num_cpus.

Ya, there's a lot of discussion in the previous PR about thread/physical-core count and here.
I believe that in the software world what matters (at least in 95%+ of the cases) is the thread count and thus that's what should be in the stdlib. This is also consistent with other programming languages behaviors (GO, C++, etc..). Details like socket count, physical core counts, etc.. should be elsewhere.

@alexcrichton
Copy link
Member

I've got a working implementation of std::env currently through cargo here: https://github.com/alexcrichton/io2/blob/master/src/env.rs

There are still a few lingering questions on this RFC, but I'd like to propose marking those APIs as #[unstable] so we can start moving to OsString instead of String being returned from some of the less controversial APIs to get some traction using it. Specifically, I believe the remaining will all still be #[unstable] pending a future RFC to stabilize them:

  • num_cpus - we could rename to cpu_count perhaps right now, but there are also reasons to avoid cpu_count. All in all the naming and semantics here haven't been totally pinned down, so we should leave ourselves to tweaking this in the future.
  • {get,set}_exit_status - this may not be the precise interface we'd like to stabilize one. We may want to allow fn main() -> i32 for example, in which case we may not need these at all. For now they're going to remain #[unstable] as they may go away in the future pending other design changes.
  • page_size - better safe than sorry! (this also isn't used too too often)

If I missed something though please let me know!

@brson
Copy link
Contributor

brson commented Jan 30, 2015

join_paths and split_paths seem out of place in a module that seems to be about modifying the global environment.

current_exe is a sort of eccentric function - it exists to support rustc, and I'm not sure that this functionality can be supported universally.

@pcwalton
Copy link
Contributor

@brson: current_exe is used by my e10s patch in Servo. That said, it doesn't have to live in libstd of course.

@aturon
Copy link
Member Author

aturon commented Jan 30, 2015

re: current_exe, note that it yield an Option.

@aturon
Copy link
Member Author

aturon commented Jan 30, 2015

An update on this RFC:

Use of Cow

After discussion above and on IRC, it seems that everyone is basically happy to use Cow in principle for things like arg, but there are some concerns about the implementation and the possible interaction with mutation by non-Rust code. Based on IRC discussion, all parties are OK to go with OsString for now, and to move to Cow if a good enough implementation is available before stabilization.

Variants for returning `String

The idea that @wycats mentioned about String variants came out of an IRC discussion, and I think it's quite nice.

However, it focuses just on var, and the story is somewhat less clear for args and vars, because they return multiple strings (each of which might or might not be unicode). In addition, changing var to be the String variant and having a separate raw_var breaks the symmetry with set_var, which wants an OsString.

I suggest that instead we keep var as proposed, but add a var_string variant with the API @wycats proposed.

@alexcrichton
Copy link
Member

There seems to be broad enough support for this RFC that I am going to merge it. There are still some components which are going to be left #[unstable] for a future RFC as the semantics are hammered out and we're still going to allow for future minor updates which may tweak semantics slightly.

Thanks again for all the feedback everyone, it has all been quite helpful!

@alexcrichton alexcrichton merged commit da470e7 into rust-lang:master Jan 30, 2015
alexcrichton added a commit to alexcrichton/rust that referenced this pull request Feb 1, 2015
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]: rust-lang/rfcs#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]
@Centril Centril added the A-env Environment variable related proposals & ideas label Nov 23, 2018
djrenren pushed a commit to djrenren/libtest that referenced this pull request Jan 22, 2019
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]: rust-lang/rfcs#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]
gnzlbg pushed a commit to rust-lang/libtest that referenced this pull request Mar 2, 2019
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]: rust-lang/rfcs#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]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-env Environment variable related proposals & ideas
Projects
None yet
Development

Successfully merging this pull request may close these issues.