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

Environment variable iterator overrides #31809

Closed
wants to merge 4 commits into from

Conversation

Nemo157
Copy link
Member

@Nemo157 Nemo157 commented Feb 21, 2016

For case #24214, additional iterators mentioned in #24214 (comment).

Overrides std::env::{ Vars, VarsOs }::{ count, nth, last} to proxy through to their inner iterator (would be nice if there were some iterator wrapper trait that meant these didn't need to be implemented every time an iterator was wrapped...).

Overrides std::sys::unix::os::Env::{ count, nth, last } to proxy through to the inner vec::IntoIter. Should make count faster, and future proofs in case last or nth get optimised implementations in the future.

Overrides std::sys::windows::Env::{ count, nth, last } to not allocate OsStrings when not needed. Done by defining a sub-iterator that returns the raw pointers and lengths, this uses the default implementations as they should be pretty much equivalent to manually implemented versions, then in the outer iterator does the allocation with the result.

I don't have a machine I can actually test the windows changes on, with some hackery (https://gist.github.com/Nemo157/0d1f02b08c132f1a00da) I managed to get the type checker at least running to ensure it should compile. It would be nice if there were some way to test the higher level platform specific code. As far as I can tell testing windows::Env on unix would work fine if the infrastructure was setup for it.

Allows usage of optimized underlying implementations if any OS supports
them.
Will allow optimised usage of `count` now and `nth` & `last` if
`IntoIter` ever gets them in the future.
@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @brson (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

inner: EnvSlices,
}

struct EnvSlices {
Copy link
Member

Choose a reason for hiding this comment

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

To me when I initially read this it wasn't clear why the separation here was needed, could you add a comment explaining why these are separate?

@alexcrichton
Copy link
Member

Could you add some tests as well which exercise these functions?

@Nemo157
Copy link
Member Author

Nemo157 commented Feb 22, 2016

@alexcrichton done.

Again I could only type check the tests as I don't have a windows machine setup for rust dev.

I hope using the type alias for sys::windows::os::Env is ok? I couldn't figure out any other way to make sure the tests had the correct lifetime and use slices between the iterators without either having to add a unused lifetime parameter to the sys::unix::os::Env or change the public API.

@alexcrichton
Copy link
Member

Ah that may be an extra layer or two of indirection which may not be worth it, perhaps just Env could be used which uses the inner copy with 'static? I'd also be fine if EnvSlices just returned &'static [u16] and documenting that it's unsafe and the lifetime needs to be contained elsewhere.

Also, could you move the tests so they cover both the Unix and Windows cases? That should also help exercise the implementation in terms of what's actually being run with env::vars. I'd recommend adding the test to run-pass to ensure it's isolated from everything else going on.

} else {
let status = Command::new(env::current_exe().unwrap())
.arg("empty")
.env_clear()
Copy link
Member

Choose a reason for hiding this comment

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

This may or may not pass on Windows due to #31259

Copy link
Member Author

Choose a reason for hiding this comment

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

Damn, that is... annoying 😦

@alexcrichton
Copy link
Member

Ok, this looks good to me, could you also squash the commits together?

Avoids doing the [u16] -> OsString conversion when we don't care about
using the env vars.
@brson brson assigned alexcrichton and unassigned brson Feb 25, 2016
@alexcrichton
Copy link
Member

@bors: r+ 52d506e

Thanks!

bors added a commit that referenced this pull request Feb 25, 2016
@bors
Copy link
Contributor

bors commented Feb 25, 2016

⌛ Testing commit 52d506e with merge 9e16bea...

@alexcrichton
Copy link
Member

@bors: retry force

@bors
Copy link
Contributor

bors commented Feb 26, 2016

⌛ Testing commit 52d506e with merge ce41c0e...

@bors
Copy link
Contributor

bors commented Feb 26, 2016

💔 Test failed - auto-linux-64-x-android-t

@alexcrichton
Copy link
Member

Closing due to inactivity, but feel free to reopen with tests fixed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants