EitherOrBoth: Add or and or_else methods to simplify getting default values #593
+49
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Introducing
or
andor_else
methods toEitherOrBoth
This feature reduces the amount of code required to get specific default values when using EitherOrBoth.
An example of the current way of using
zip_longest
with custom default values:An example with the proposed
or
method:I have also included the
or_else
method which does the same but with closures.Contribute questions
There are no tests for the other
EitherOrBoth
methods, so I was unsure how to place them. However, I have added DocTest's to both methods. These examples allows for easier to understand documentation, and also tests the validity of the methods.The EitherOrBoth struct does not exist in rust std library.
Concerns
The naming is slightly inconsistent when compared to rust's
std::Option
, considering the naming from there the added methods should be namedunwrap_or
andunwrap_or_else
.However, this would then become inconsistent with the existing method
EitherOrBoth::or_default
. Which is why I went with the chosen names.I can change the method names if needed, but then we should consider changing
or_default
too.P.S.
The
CHANGELOG.md
file has the textAdd EitherOrBoth::or_default (#583)
. This number is wrong, it should be #538.