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 {left,right}_or_{right,left} methods to EitherOrBoth #721

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/either_or_both.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,44 @@ impl<T> EitherOrBoth<T, T> {
Both(a, b) => f(a, b),
}
}

/// Returns either left or right, or if both are present returns right
///
/// Complement of [`left_or_right`](EitherOrBoth::left_or_right).
///
/// # Examples
/// ```
/// # use itertools::EitherOrBoth;
/// assert_eq!(EitherOrBoth::Both(3, 7).right_or_left(), 7);
/// assert_eq!(EitherOrBoth::Left(3).right_or_left(), 3);
/// assert_eq!(EitherOrBoth::Right(7).right_or_left(), 7);
/// ```
pub fn right_or_left(self) -> T {
match self {
Left(t) => t,
Right(t) => t,
Both(_, t) => t,
}
}

/// Returns either left or right, or if both are present returns left
///
/// Complement of [`right_or_left`](EitherOrBoth::right_or_left).
///
/// # Examples
/// ```
/// # use itertools::EitherOrBoth;
/// assert_eq!(EitherOrBoth::Both(3, 7).left_or_right(), 3);
/// assert_eq!(EitherOrBoth::Left(3).left_or_right(), 3);
/// assert_eq!(EitherOrBoth::Right(7).left_or_right(), 7);
/// ```
pub fn left_or_right(self) -> T {
match self {
Left(t) => t,
Right(t) => t,
Both(t, _) => t,
}
}
}

impl<A, B> Into<Option<Either<A, B>>> for EitherOrBoth<A, B> {
Expand Down