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

DoubleEndedIterator for Args #33312

Merged
merged 1 commit into from
Jul 27, 2016
Merged

DoubleEndedIterator for Args #33312

merged 1 commit into from
Jul 27, 2016

Conversation

Byron
Copy link
Member

@Byron Byron commented May 1, 2016

This PR implements the DoubleEndedIterator trait for the std::env::Args[Os] structure, as well
as the internal implementations.

It is primarily motivated by me, as I happened to implement a simple reversor program many times
now, which so far had to use code like this:

for arg in std::env::args().skip(1).collect::<Vec<_>>().iter().rev() {}

... even though I would have loved to do this instead:

for arg in std::env::args().skip(1).rev() {}

The latter is more natural, and I did not find a reason for not implementing it.
After all, on every system, the number of arguments passed to the program are known
at runtime.

About the implementation

To my mind, it follows KISS, and does not try to be smart at all. Also, there are no unit-tests,
primarily as I did not find any existing tests for the Args struct either.

Critical Comments

The windows implementation is basically a copy-pasted variant of the next() method implementation,
and I could imagine sharing most of the code instead. Actually I would be happy if the reviewer would
ask for it.

@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.

@alexcrichton
Copy link
Member

Looks good to me, thanks for the PR! As you mentioned, can you share code in the Windows implementation to deduplicate, and then can you also add a few tests for this?

@Byron
Copy link
Member Author

Byron commented May 25, 2016

Thanks @alexcrichton for the hints ! Currently I am trying to see how to best run the tests. For now, the fasted run I got was via time make check-stage1-std, which took just 10.5m . I also tried time make check-stage1-std TESTNAME=env::tests, but that one takes even longer.

Do you know how to speed it up testing in my case ?

@Byron
Copy link
Member Author

Byron commented May 25, 2016

Never mind - I ran make check-stage1-std -n to see the commands actually executed, and from there I could cut down the time for each run to about 2 minutes. That's as good as it gets, as the compilation took 1:44 minutes already.

@alexcrichton
Copy link
Member

Another option may be the NO_REBUILD environment variable which should avoid recompiling the whole of libstd and instead go straight to tests. Finally you may wish to pass ./configure --disable-optimize-tests as that helps stdtest compile a little more quickly

}
}

#[stable(feature = "env", since = "1.0.0")]
Copy link
Member

Choose a reason for hiding this comment

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

This should be marked since = "1.11.0", not 1.0.0

@sfackler
Copy link
Member

Can you add on ExactSizeIterator impls while we're at it?

@Byron
Copy link
Member Author

Byron commented May 26, 2016

@sfackler Good idea, ExactSizeIterator shall be done as well !

@Byron
Copy link
Member Author

Byron commented May 26, 2016

@sfackler When running make tidy with the requested changes to since, I get an error saying:

* 421 error codes
* highest error code: E0524
/Users/byron/Documents/dev/rust/rust-lang/rust/src/libstd/env.rs:590: different `since` than before
/Users/byron/Documents/dev/rust/rust-lang/rust/src/libstd/env.rs:609: different `since` than before
thread '<main>' panicked at 'some tidy checks failed', /Users/byron/Documents/dev/rust/rust-lang/rust/src/tools/tidy/src/main.rs:51
note: Run with `RUST_BACKTRACE=1` for a backtrace.
make: *** [tidy] Error 101

I assume that I have to create a new feature and assign it a since 1.11 accordingly. If so, what is the feature name you would like to see here ?
Thank you

@Byron
Copy link
Member Author

Byron commented May 26, 2016

I think this PR is ready for review. Please note that I am not sure my feature-name is desirable.

Also I found ExactSizeIterator to be implemented already.

assert!(args[l-1].contains("stdtest"));
assert_eq!(args[1], "--logfile");
assert!(args[0].ends_with(".log"));
}
Copy link
Member

Choose a reason for hiding this comment

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

Could you add a unit test elsewhere with this test as this'd unfortunately break running the binary manually :(

Copy link
Member

Choose a reason for hiding this comment

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

(e.g., adding a test to src/test/run-pass where the test just executes itself to run the test)

@alexcrichton
Copy link
Member

Ah yeah the test name is fine, we tend to just pick totally arbitrary names anyway :)

@Byron
Copy link
Member Author

Byron commented May 26, 2016

Thanks for the feedback ! The problem I see with the run-pass tests is the inability to pass arguments to the program, making it impossible to be sure the feature is implemented correctly. Do you have an idea how that could be done ?

@alexcrichton
Copy link
Member

Ah yeah the trick I normally take looks like this test where a test re-executes itself to run the test and then asserts a property of the output

@Byron
Copy link
Member Author

Byron commented May 27, 2016

Ok, it seems to be done :) !

A few things I noticed:

  • the run-pass test does not need to specify a feature. Apparently trait implementations are not affected by feature gates ?
  • I am unsure if the feature name env_iterators is descriptive enough, and I'd be happy to hear other suggestions.

@@ -271,6 +271,7 @@
#![feature(vec_push_all)]
#![feature(zero_one)]
#![feature(question_mark)]
#![feature(env_iterators)]
Copy link
Member

Choose a reason for hiding this comment

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

Ah this isn't actually required for stable features, so it's ok to leave this off

@alexcrichton
Copy link
Member

Thanks! Yeah feature annotations aren't required for stable features (which this is), and we generally pick totally arbitrary names for these so env_iterators is fine :)

@Byron
Copy link
Member Author

Byron commented May 28, 2016

Alright, I have removed the feature, which indeed is not required. The test was made a little less-trusty too.
Let's see what travis thinks about this.

@alexcrichton
Copy link
Member

That all looks good to me, thanks! Could you squash down into one commit as well?

@brson brson assigned alexcrichton and unassigned brson May 31, 2016
@Byron
Copy link
Member Author

Byron commented Jun 2, 2016

Alright, it's done :) !

@alexcrichton
Copy link
Member

@bors: r+ 8f4b7ad

@bors
Copy link
Contributor

bors commented Jun 2, 2016

⌛ Testing commit 8f4b7ad with merge 62f0bb1...

@bors
Copy link
Contributor

bors commented Jun 2, 2016

💔 Test failed - auto-win-msvc-64-cargotest

@Byron
Copy link
Member Author

Byron commented Jun 25, 2016

I believe to have fixed the function signature to not cause build failures on windows anymore.

@alexcrichton
Copy link
Member

@bors: r+ 6ad18eb

@bors
Copy link
Contributor

bors commented Jun 27, 2016

💔 Test failed - auto-win-msvc-64-opt-rustbuild

@Byron
Copy link
Member Author

Byron commented Jul 17, 2016

Alright, getting there ! With a little bit of luck, this totally works now.

@Byron
Copy link
Member Author

Byron commented Jul 23, 2016

@alexcrichton Could you have a look again please ? It should go through on windows now. Thank you.

@alexcrichton
Copy link
Member

@bors: r+ e68a134

@bors
Copy link
Contributor

bors commented Jul 25, 2016

⌛ Testing commit e68a134 with merge 57558d1...

@bors
Copy link
Contributor

bors commented Jul 25, 2016

💔 Test failed - auto-win-gnu-32-opt-rustbuild

@Byron
Copy link
Member Author

Byron commented Jul 25, 2016

@alexcrichton The failure seems to be unrelated:

Synchronizing submodule url for 'src/rt/hoedown'
Synchronizing submodule url for 'src/rust-installer'
fatal: Needed a single revision
Unable to find current revision in submodule path 'src/llvm'

Is there anything I can do? Thanks.

@alexcrichton
Copy link
Member

@bors: retry

nah we just have a lot of network failures recently :(

On Sun, Jul 24, 2016 at 11:06 PM, Sebastian Thiel [email protected]
wrote:

@alexcrichton https://github.com/alexcrichton The failure seems to be
unrelated:

Synchronizing submodule url for 'src/rt/hoedown'
Synchronizing submodule url for 'src/rust-installer'
fatal: Needed a single revision
Unable to find current revision in submodule path 'src/llvm'

Is there anything I can do? Thanks.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#33312 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAD95G9fEeiEXXG5W0bynPht8LU_sAicks5qZFJXgaJpZM4IUWU6
.

@bors
Copy link
Contributor

bors commented Jul 25, 2016

⌛ Testing commit e68a134 with merge ed4f588...

bors added a commit that referenced this pull request Jul 25, 2016
…ichton

DoubleEndedIterator for Args

This PR implements the DoubleEndedIterator trait for the `std::env::Args[Os]` structure, as well
as the internal implementations.

It is primarily motivated by me, as I happened to implement a simple `reversor` program many times
now, which so far had to use code like this:

```Rust
for arg in std::env::args().skip(1).collect::<Vec<_>>().iter().rev() {}
```

... even though I would have loved to do this instead:

```Rust
for arg in std::env::args().skip(1).rev() {}
```

The latter is more natural, and I did not find a reason for not implementing it.
After all, on every system, the number of arguments passed to the program are known
at runtime.

To my mind, it follows KISS, and does not try to be smart at all. Also, there are no unit-tests,
primarily as I did not find any existing tests for the `Args` struct either.

The windows implementation is basically a copy-pasted variant of the `next()` method implementation,
and I could imagine sharing most of the code instead. Actually I would be happy if the reviewer would
ask for it.
@bors
Copy link
Contributor

bors commented Jul 25, 2016

💥 Test timed out

@alexcrichton
Copy link
Member

@bors: retry force clean

@alexcrichton
Copy link
Member

@bors: retry

@bors
Copy link
Contributor

bors commented Jul 25, 2016

⌛ Testing commit e68a134 with merge da8959a...

@bors
Copy link
Contributor

bors commented Jul 25, 2016

💔 Test failed - auto-win-gnu-32-opt

@alexcrichton
Copy link
Member

@bors: retry

On Mon, Jul 25, 2016 at 2:41 PM, bors [email protected] wrote:

💔 Test failed - auto-win-gnu-32-opt
https://buildbot.rust-lang.org/builders/auto-win-gnu-32-opt/builds/5066


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#33312 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAD95FgiXAfKTseTbKdlUlp6lRwI1v02ks5qZS10gaJpZM4IUWU6
.

@bors
Copy link
Contributor

bors commented Jul 26, 2016

⌛ Testing commit e68a134 with merge 69e335b...

@bors
Copy link
Contributor

bors commented Jul 26, 2016

💔 Test failed - auto-win-msvc-64-cargotest

The number of arguments given to a process is always known, which
makes implementing DoubleEndedIterator possible.

That way, the Iterator::rev() method becomes usable, among others.

Signed-off-by: Sebastian Thiel <[email protected]>

Tidy for DoubleEndedIterator

I chose to not create a new feature for it, even though
technically, this makes me lie about the original availability
of the implementation.

Verify with @alexchrichton

Setup feature flag for new std::env::Args iterators

Add test for Args reverse iterator

It's somewhat depending on the input of the test program,
but made in such a way that should be somewhat flexible to changes
to the way it is called.

Deduplicate windows ArgsOS code for DEI

DEI = DoubleEndedIterator

Move env::args().rev() test to run-pass

It must be controlling it's arguments for full isolation.

Remove superfluous feature name

Assert all arguments returned by env::args().rev()

Let's be very sure it works as we expect, why take chances.

Fix rval of os_string_from_ptr

A trait cannot be returned, but only the corresponding object.

Deref pointers to actually operate on the argument

Put unsafe to correct location
@Byron
Copy link
Member Author

Byron commented Jul 26, 2016

@alexcrichton I have placed the unsafe scope into the right place, and rebased everything into one commit which now will hopefully go through. My apologies for all the attempts it took to make work this simple patch.

@alexcrichton
Copy link
Member

@Byron no worries! Most failures nowadays are due to our infrastructure anyways :)

@bors: r+ 1aa8dad

@bors
Copy link
Contributor

bors commented Jul 27, 2016

⌛ Testing commit 1aa8dad with merge 422ebd5...

bors added a commit that referenced this pull request Jul 27, 2016
…ichton

DoubleEndedIterator for Args

This PR implements the DoubleEndedIterator trait for the `std::env::Args[Os]` structure, as well
as the internal implementations.

It is primarily motivated by me, as I happened to implement a simple `reversor` program many times
now, which so far had to use code like this:

```Rust
for arg in std::env::args().skip(1).collect::<Vec<_>>().iter().rev() {}
```

... even though I would have loved to do this instead:

```Rust
for arg in std::env::args().skip(1).rev() {}
```

The latter is more natural, and I did not find a reason for not implementing it.
After all, on every system, the number of arguments passed to the program are known
at runtime.

To my mind, it follows KISS, and does not try to be smart at all. Also, there are no unit-tests,
primarily as I did not find any existing tests for the `Args` struct either.

The windows implementation is basically a copy-pasted variant of the `next()` method implementation,
and I could imagine sharing most of the code instead. Actually I would be happy if the reviewer would
ask for it.
@bors bors merged commit 1aa8dad into rust-lang:master Jul 27, 2016
@apasel422 apasel422 added the relnotes Marks issues that should be documented in the release notes of the next release. label Jul 27, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
relnotes Marks issues that should be documented in the release notes of the next release.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants