Skip to content

Commit ad70efd

Browse files
bors[bot]abrassel
andauthored
Merge #538
538: Added the `or_default` method to `EitherOrBoth` r=phimuemue a=abrassel This commit is motivated by the context explained in issue #537 Allows for doing things like padding out an iterator with a default value while the other one is consumed. Co-authored-by: Alex Brassel <[email protected]>
2 parents 1255025 + 1ee058b commit ad70efd

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/either_or_both.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,21 @@ impl<A, B> EitherOrBoth<A, B> {
163163
Right(b) | Both(_, b) => f(b),
164164
}
165165
}
166+
167+
/// Returns a tuple consisting of the `l` and `r` in `Both(l, r)`, if present.
168+
/// Otherwise, returns the wrapped value for the present element, and the [`default`](Default::default)
169+
/// for the other.
170+
pub fn or_default(self) -> (A, B)
171+
where
172+
A: Default,
173+
B: Default,
174+
{
175+
match self {
176+
EitherOrBoth::Left(l) => (l, B::default()),
177+
EitherOrBoth::Right(r) => (A::default(), r),
178+
EitherOrBoth::Both(l, r) => (l, r),
179+
}
180+
}
166181
}
167182

168183
impl<T> EitherOrBoth<T, T> {

0 commit comments

Comments
 (0)