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

Improvements to EitherOrBoth #629

Merged
merged 7 commits into from Jun 14, 2023
Merged

Improvements to EitherOrBoth #629

merged 7 commits into from Jun 14, 2023

Conversation

JoJoJet
Copy link
Contributor

@JoJoJet JoJoJet commented Jul 4, 2022

  • Added more methods for accessing the left and right variants.
  • Added methods for inserting left and right values (using unsafe code).
  • Improved some grammar.

@phimuemue
Copy link
Member

Hi there, thanks for addressing the compilation issues.

Could you rebase the changes so we do not need to review back-and-forth changes?

@JoJoJet
Copy link
Contributor Author

JoJoJet commented Jul 7, 2022

Could you rebase the changes so we do not need to review back-and-forth changes?

Done, I think.

pub fn both(self) -> Option<(A, B)> {
match self {
Both(a, b) => Some((a, b)),
_ => None,
}
}

/// If `Left` or `Both`, return the left value. Otherwise, convert the right value and return it.
pub fn into_left(self) -> A
Copy link

Choose a reason for hiding this comment

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

I prefer left_biased, which implies the fallback behavior on Right. So does into_right -> right_biased.
This *_biased wording is also used by rowan, the parser library of rust-analyzer, in an enum like our EitherOrBoth but allows empty.
https://docs.rs/rowan/0.15.11/rowan/enum.TokenAtOffset.html#method.left_biased

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think that the *_biased naming convention really captures what's going on here. The TokenAtOffset struct does not have to perform type conversions which makes that name work -- however EitherOrBoth does have to perform type conversions, so calling it biased doesn't really make sense to me. The into_* convention communicates that this operation performs a type conversion when necessary. Also, the TokenAtOffset struct seems too different to be comparable to EitherOrBoth. That struct does not have Left or Right variants, just a Single variant that does not specify a side. Plus, as you mentioned, it has an None variant which drastically changes how this operation works.

Copy link

Choose a reason for hiding this comment

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

I don't think that the *_biased naming convention really captures what's going on here.

To my intuition, left_biased implies that "prefer Left, but fallback to return Right if there's no Left". into_left sounds like "we know there's a Left inside, just unwrap it" (like into_inner?).
But you're right, there's a conversion when Right(_) is encountered, and into_left captures this behavior.

Copy link
Member

@jswrenn jswrenn left a comment

Choose a reason for hiding this comment

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

bors r+

@jswrenn jswrenn added this to the next milestone Jun 14, 2023
@bors
Copy link
Contributor

bors bot commented Jun 14, 2023

Build succeeded!

The publicly hosted instance of bors-ng is deprecated and will go away soon.

If you want to self-host your own instance, instructions are here.
For more help, visit the forum.

If you want to switch to GitHub's built-in merge queue, visit their help page.

@bors bors bot merged commit 8bc377e into rust-itertools:master Jun 14, 2023
1 check passed
@jswrenn jswrenn modified the milestones: next, v11.0.0 Nov 13, 2023
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

4 participants