Skip to content

Commit ad1a7f0

Browse files
committed
Added the or_default method to EitherOrBoth See #537 for context.
1 parent 1255025 commit ad1a7f0

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/either_or_both.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,16 @@ impl<A, B> Into<Option<Either<A, B>>> for EitherOrBoth<A, B> {
188188
}
189189
}
190190
}
191+
192+
impl<A: Default, B: Default> EitherOrBoth<A,B> {
193+
/// Returns a tuple consisting of the `l` and `r` in `Both(l, r)`, if present.
194+
/// Otherwise, returns the wrapped value for the present element, and the [`default`](Default::default)
195+
/// for the other.
196+
pub fn or_default(self) -> (A, B) {
197+
match self {
198+
EitherOrBoth::Left(l) => (l, B::default()),
199+
EitherOrBoth::Right(r) => (A::default(), r),
200+
EitherOrBoth::Both(l, r) => (l, r),
201+
}
202+
}
203+
}

0 commit comments

Comments
 (0)