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

Add exactly_one function #310

Merged
merged 5 commits into from Dec 13, 2018
Merged

Add exactly_one function #310

merged 5 commits into from Dec 13, 2018

Conversation

Xaeroxe
Copy link
Contributor

@Xaeroxe Xaeroxe commented Oct 3, 2018

Adds a small function I found uses for personally. Can't be added to the core library because in the error case it allocates a vec and stores data in it.

@Centril
Copy link
Contributor

Centril commented Nov 25, 2018

Relevant: rust-lang/rust#55355.

@bluss
Copy link
Member

bluss commented Nov 25, 2018

I'd prefer this in Centril's version. And now we know, std doesn't want this method, so we can take it.

@Centril
Copy link
Contributor

Centril commented Nov 25, 2018

(Feel free to copy the contents from my PR if you want)

@Xaeroxe
Copy link
Contributor Author

Xaeroxe commented Nov 25, 2018

@bluss Why do we prefer returning nothing in the case of there being more than 1? Since both versions consume the iterator it seems to me we're just needlessly hiding valuable debugging info.

@Xaeroxe
Copy link
Contributor Author

Xaeroxe commented Nov 25, 2018

If we simply don't like memory allocation for the error vec then I suppose we could make this take &mut instead and change the Err type to instead be Option<(Self::Item, Self::Item)>.

@Centril
Copy link
Contributor

Centril commented Nov 27, 2018

In all the cases where I needed this I didn't want to get the remaining elements so the allocation shouldn't be pushed on folks imo; If you want to give back the iterator you can do: -> Result<Self::Item, (Self::Item, Self)> and then you can use rest in Err(x, rest) to get the remaining bits if you need that.

@bluss
Copy link
Member

bluss commented Nov 27, 2018

We can return an iterator or similar, we already have one for iterator + one item in itertools, PutBack, so that s a nice one to put in the error case

@Xaeroxe
Copy link
Contributor Author

Xaeroxe commented Nov 27, 2018

@bluss I've rewritten this to use an iterator in the error case.

@bluss
Copy link
Member

bluss commented Nov 27, 2018

Oh right, we need to put back two elements. Either use putbackn or a custom iterator in that case, I don't mind the latter

@Centril
Copy link
Contributor

Centril commented Nov 27, 2018

I think this turned out nicely. :)

@Xaeroxe
Copy link
Contributor Author

Xaeroxe commented Nov 27, 2018

@bluss how's this?

@Xaeroxe
Copy link
Contributor Author

Xaeroxe commented Nov 27, 2018

putbackn uses a vec internally which puts us back at the original problem, so I went for a custom iterator.

@Xaeroxe
Copy link
Contributor Author

Xaeroxe commented Nov 29, 2018

@bluss Just pinging you in case this got lost. Based on your github history it seems the past few days have been very busy for you.

@bluss
Copy link
Member

bluss commented Dec 2, 2018

This is good, I'll leave comments inline. I don't always have time. If you look at this repo, one might have to wait months. Pinging is always good. 🙂

src/exactly_one_err.rs Outdated Show resolved Hide resolved
@Xaeroxe
Copy link
Contributor Author

Xaeroxe commented Dec 3, 2018

@bluss done, please review

@Xaeroxe
Copy link
Contributor Author

Xaeroxe commented Dec 12, 2018

@bluss Believe this is ready to merge now.

I: Iterator,
{
/// Creates a new `ExactlyOneErr` iterator.
pub fn new(first_two: (Option<I::Item>, Option<I::Item>), inner: I) -> Self {
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 pub(crate) and not public, but I'll fix it up

@bluss
Copy link
Member

bluss commented Dec 13, 2018

Sorry, we need to add at test for this iterator before it merges. Then I agree it's ready. Can be a simple quickcheck test.

@Xaeroxe
Copy link
Contributor Author

Xaeroxe commented Dec 13, 2018

@bluss is the doc comment test not sufficient?

@bluss
Copy link
Member

bluss commented Dec 13, 2018

@Xaeroxe I realize I had thought about tests before and accepted the doctests, so I'm sorry that I didn't remember that. I shouldn't change my mind.

But in principle, no, features should have tests. Preferably quickcheck tests! They are very good at finding bugs. I feel strongly that testing documentation is often misunderstood — we test documentation to make sure that the examples we give actually work. We should make understandable examples, and they are there for documentation, not testing.

@bluss
Copy link
Member

bluss commented Dec 13, 2018

So since it's annoying to change one's mind, I'll merge since I had already decided some point prior to just let it slide.

@bluss
Copy link
Member

bluss commented Dec 13, 2018

bors r+

Thank you!

bors bot added a commit that referenced this pull request Dec 13, 2018
310: Add exactly_one function r=bluss a=Xaeroxe

Adds a small function I found uses for personally.  Can't be added to the core library because in the error case it allocates a vec and stores data in it.

Co-authored-by: Jacob Kiesel <kieseljake@gmail.com>
@Xaeroxe
Copy link
Contributor Author

Xaeroxe commented Dec 13, 2018

@bluss well I mean you made a really good case and I agree with you, so I'm happy to add the unit tests if you want.

@bors
Copy link
Contributor

bors bot commented Dec 13, 2018

Build succeeded

@bluss
Copy link
Member

bluss commented Dec 13, 2018

@Xaeroxe ok, that can be a new PR if you want to

@bors bors bot merged commit 094b416 into rust-itertools:master Dec 13, 2018
@Xaeroxe Xaeroxe deleted the exactly-one branch December 13, 2018 16:52
@bluss
Copy link
Member

bluss commented Dec 13, 2018

Wheels of bors are already turning

@Xaeroxe
Copy link
Contributor Author

Xaeroxe commented Dec 13, 2018

@bluss I've opened #324 , thanks!

bors bot added a commit that referenced this pull request Dec 13, 2018
324: Add unit tests for exactly_one and move ExactlyOneError::new to pub(crate) r=bluss a=Xaeroxe

Follow up to #310 

Ready for review

Co-authored-by: Jacob Kiesel <kieseljake@gmail.com>
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.

None yet

3 participants