mirror of
https://github.com/openharmony/third_party_rust_either.git
synced 2026-07-19 23:23:31 -04:00
Add into_common
This commit is contained in:
+22
@@ -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)> {
|
||||
|
||||
Reference in New Issue
Block a user