Add into_common

This commit is contained in:
TonalidadeHidrica
2021-11-26 00:33:56 +09:00
parent 044cd3511e
commit 7e4d4ebe4d
+22
View File
@@ -654,6 +654,28 @@ impl<L, R> Either<L, R> {
Either::Left(l) => panic!("{}: {:?}", msg, l),
}
}
/// Converts to a common type
///
/// # Examples
///
/// ```
/// # use either::*;
/// // Both u16 and u32 can be converted to u64.
/// let left: Either<u16, u32> = Left(3u16);
/// assert_eq!(left.into_common::<u64>(), 3u64);
/// let right: Either<u16, u32> = Right(7u32);
/// assert_eq!(right.into_common::<u64>(), 7u64);
/// ```
pub fn into_common<T>(self) -> T
where
T: From<L> + From<R>,
{
match self {
Either::Left(l) => l.into(),
Either::Right(r) => r.into(),
}
}
}
impl<T, L, R> Either<(T, L), (T, R)> {