14669f6dcSopenharmony_ci//! The enum [`Either`] with variants `Left` and `Right` is a general purpose 24669f6dcSopenharmony_ci//! sum type with two cases. 34669f6dcSopenharmony_ci//! 44669f6dcSopenharmony_ci//! [`Either`]: enum.Either.html 54669f6dcSopenharmony_ci//! 64669f6dcSopenharmony_ci//! **Crate features:** 74669f6dcSopenharmony_ci//! 84669f6dcSopenharmony_ci//! * `"use_std"` 94669f6dcSopenharmony_ci//! Enabled by default. Disable to make the library `#![no_std]`. 104669f6dcSopenharmony_ci//! 114669f6dcSopenharmony_ci//! * `"serde"` 124669f6dcSopenharmony_ci//! Disabled by default. Enable to `#[derive(Serialize, Deserialize)]` for `Either` 134669f6dcSopenharmony_ci//! 144669f6dcSopenharmony_ci 154669f6dcSopenharmony_ci#![doc(html_root_url = "https://docs.rs/either/1/")] 164669f6dcSopenharmony_ci#![no_std] 174669f6dcSopenharmony_ci 184669f6dcSopenharmony_ci#[cfg(any(test, feature = "use_std"))] 194669f6dcSopenharmony_ciextern crate std; 204669f6dcSopenharmony_ci 214669f6dcSopenharmony_ci#[cfg(feature = "serde")] 224669f6dcSopenharmony_cipub mod serde_untagged; 234669f6dcSopenharmony_ci 244669f6dcSopenharmony_ci#[cfg(feature = "serde")] 254669f6dcSopenharmony_cipub mod serde_untagged_optional; 264669f6dcSopenharmony_ci 274669f6dcSopenharmony_ciuse core::convert::{AsMut, AsRef}; 284669f6dcSopenharmony_ciuse core::fmt; 294669f6dcSopenharmony_ciuse core::future::Future; 304669f6dcSopenharmony_ciuse core::iter; 314669f6dcSopenharmony_ciuse core::ops::Deref; 324669f6dcSopenharmony_ciuse core::ops::DerefMut; 334669f6dcSopenharmony_ciuse core::pin::Pin; 344669f6dcSopenharmony_ci 354669f6dcSopenharmony_ci#[cfg(any(test, feature = "use_std"))] 364669f6dcSopenharmony_ciuse std::error::Error; 374669f6dcSopenharmony_ci#[cfg(any(test, feature = "use_std"))] 384669f6dcSopenharmony_ciuse std::io::{self, BufRead, Read, Seek, SeekFrom, Write}; 394669f6dcSopenharmony_ci 404669f6dcSopenharmony_cipub use crate::Either::{Left, Right}; 414669f6dcSopenharmony_ci 424669f6dcSopenharmony_ci/// The enum `Either` with variants `Left` and `Right` is a general purpose 434669f6dcSopenharmony_ci/// sum type with two cases. 444669f6dcSopenharmony_ci/// 454669f6dcSopenharmony_ci/// The `Either` type is symmetric and treats its variants the same way, without 464669f6dcSopenharmony_ci/// preference. 474669f6dcSopenharmony_ci/// (For representing success or error, use the regular `Result` enum instead.) 484669f6dcSopenharmony_ci#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] 494669f6dcSopenharmony_ci#[derive(Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] 504669f6dcSopenharmony_cipub enum Either<L, R> { 514669f6dcSopenharmony_ci /// A value of type `L`. 524669f6dcSopenharmony_ci Left(L), 534669f6dcSopenharmony_ci /// A value of type `R`. 544669f6dcSopenharmony_ci Right(R), 554669f6dcSopenharmony_ci} 564669f6dcSopenharmony_ci 574669f6dcSopenharmony_ci/// Evaluate the provided expression for both [`Either::Left`] and [`Either::Right`]. 584669f6dcSopenharmony_ci/// 594669f6dcSopenharmony_ci/// This macro is useful in cases where both sides of [`Either`] can be interacted with 604669f6dcSopenharmony_ci/// in the same way even though the don't share the same type. 614669f6dcSopenharmony_ci/// 624669f6dcSopenharmony_ci/// Syntax: `either::for_both!(` *expression* `,` *pattern* `=>` *expression* `)` 634669f6dcSopenharmony_ci/// 644669f6dcSopenharmony_ci/// # Example 654669f6dcSopenharmony_ci/// 664669f6dcSopenharmony_ci/// ``` 674669f6dcSopenharmony_ci/// use either::Either; 684669f6dcSopenharmony_ci/// 694669f6dcSopenharmony_ci/// fn length(owned_or_borrowed: Either<String, &'static str>) -> usize { 704669f6dcSopenharmony_ci/// either::for_both!(owned_or_borrowed, s => s.len()) 714669f6dcSopenharmony_ci/// } 724669f6dcSopenharmony_ci/// 734669f6dcSopenharmony_ci/// fn main() { 744669f6dcSopenharmony_ci/// let borrowed = Either::Right("Hello world!"); 754669f6dcSopenharmony_ci/// let owned = Either::Left("Hello world!".to_owned()); 764669f6dcSopenharmony_ci/// 774669f6dcSopenharmony_ci/// assert_eq!(length(borrowed), 12); 784669f6dcSopenharmony_ci/// assert_eq!(length(owned), 12); 794669f6dcSopenharmony_ci/// } 804669f6dcSopenharmony_ci/// ``` 814669f6dcSopenharmony_ci#[macro_export] 824669f6dcSopenharmony_cimacro_rules! for_both { 834669f6dcSopenharmony_ci ($value:expr, $pattern:pat => $result:expr) => { 844669f6dcSopenharmony_ci match $value { 854669f6dcSopenharmony_ci $crate::Either::Left($pattern) => $result, 864669f6dcSopenharmony_ci $crate::Either::Right($pattern) => $result, 874669f6dcSopenharmony_ci } 884669f6dcSopenharmony_ci }; 894669f6dcSopenharmony_ci} 904669f6dcSopenharmony_ci 914669f6dcSopenharmony_ci/// Macro for unwrapping the left side of an `Either`, which fails early 924669f6dcSopenharmony_ci/// with the opposite side. Can only be used in functions that return 934669f6dcSopenharmony_ci/// `Either` because of the early return of `Right` that it provides. 944669f6dcSopenharmony_ci/// 954669f6dcSopenharmony_ci/// See also `try_right!` for its dual, which applies the same just to the 964669f6dcSopenharmony_ci/// right side. 974669f6dcSopenharmony_ci/// 984669f6dcSopenharmony_ci/// # Example 994669f6dcSopenharmony_ci/// 1004669f6dcSopenharmony_ci/// ``` 1014669f6dcSopenharmony_ci/// use either::{Either, Left, Right}; 1024669f6dcSopenharmony_ci/// 1034669f6dcSopenharmony_ci/// fn twice(wrapper: Either<u32, &str>) -> Either<u32, &str> { 1044669f6dcSopenharmony_ci/// let value = either::try_left!(wrapper); 1054669f6dcSopenharmony_ci/// Left(value * 2) 1064669f6dcSopenharmony_ci/// } 1074669f6dcSopenharmony_ci/// 1084669f6dcSopenharmony_ci/// fn main() { 1094669f6dcSopenharmony_ci/// assert_eq!(twice(Left(2)), Left(4)); 1104669f6dcSopenharmony_ci/// assert_eq!(twice(Right("ups")), Right("ups")); 1114669f6dcSopenharmony_ci/// } 1124669f6dcSopenharmony_ci/// ``` 1134669f6dcSopenharmony_ci#[macro_export] 1144669f6dcSopenharmony_cimacro_rules! try_left { 1154669f6dcSopenharmony_ci ($expr:expr) => { 1164669f6dcSopenharmony_ci match $expr { 1174669f6dcSopenharmony_ci $crate::Left(val) => val, 1184669f6dcSopenharmony_ci $crate::Right(err) => return $crate::Right(::core::convert::From::from(err)), 1194669f6dcSopenharmony_ci } 1204669f6dcSopenharmony_ci }; 1214669f6dcSopenharmony_ci} 1224669f6dcSopenharmony_ci 1234669f6dcSopenharmony_ci/// Dual to `try_left!`, see its documentation for more information. 1244669f6dcSopenharmony_ci#[macro_export] 1254669f6dcSopenharmony_cimacro_rules! try_right { 1264669f6dcSopenharmony_ci ($expr:expr) => { 1274669f6dcSopenharmony_ci match $expr { 1284669f6dcSopenharmony_ci $crate::Left(err) => return $crate::Left(::core::convert::From::from(err)), 1294669f6dcSopenharmony_ci $crate::Right(val) => val, 1304669f6dcSopenharmony_ci } 1314669f6dcSopenharmony_ci }; 1324669f6dcSopenharmony_ci} 1334669f6dcSopenharmony_ci 1344669f6dcSopenharmony_ciimpl<L: Clone, R: Clone> Clone for Either<L, R> { 1354669f6dcSopenharmony_ci fn clone(&self) -> Self { 1364669f6dcSopenharmony_ci match self { 1374669f6dcSopenharmony_ci Left(inner) => Left(inner.clone()), 1384669f6dcSopenharmony_ci Right(inner) => Right(inner.clone()), 1394669f6dcSopenharmony_ci } 1404669f6dcSopenharmony_ci } 1414669f6dcSopenharmony_ci 1424669f6dcSopenharmony_ci fn clone_from(&mut self, source: &Self) { 1434669f6dcSopenharmony_ci match (self, source) { 1444669f6dcSopenharmony_ci (Left(dest), Left(source)) => dest.clone_from(source), 1454669f6dcSopenharmony_ci (Right(dest), Right(source)) => dest.clone_from(source), 1464669f6dcSopenharmony_ci (dest, source) => *dest = source.clone(), 1474669f6dcSopenharmony_ci } 1484669f6dcSopenharmony_ci } 1494669f6dcSopenharmony_ci} 1504669f6dcSopenharmony_ci 1514669f6dcSopenharmony_ciimpl<L, R> Either<L, R> { 1524669f6dcSopenharmony_ci /// Return true if the value is the `Left` variant. 1534669f6dcSopenharmony_ci /// 1544669f6dcSopenharmony_ci /// ``` 1554669f6dcSopenharmony_ci /// use either::*; 1564669f6dcSopenharmony_ci /// 1574669f6dcSopenharmony_ci /// let values = [Left(1), Right("the right value")]; 1584669f6dcSopenharmony_ci /// assert_eq!(values[0].is_left(), true); 1594669f6dcSopenharmony_ci /// assert_eq!(values[1].is_left(), false); 1604669f6dcSopenharmony_ci /// ``` 1614669f6dcSopenharmony_ci pub fn is_left(&self) -> bool { 1624669f6dcSopenharmony_ci match *self { 1634669f6dcSopenharmony_ci Left(_) => true, 1644669f6dcSopenharmony_ci Right(_) => false, 1654669f6dcSopenharmony_ci } 1664669f6dcSopenharmony_ci } 1674669f6dcSopenharmony_ci 1684669f6dcSopenharmony_ci /// Return true if the value is the `Right` variant. 1694669f6dcSopenharmony_ci /// 1704669f6dcSopenharmony_ci /// ``` 1714669f6dcSopenharmony_ci /// use either::*; 1724669f6dcSopenharmony_ci /// 1734669f6dcSopenharmony_ci /// let values = [Left(1), Right("the right value")]; 1744669f6dcSopenharmony_ci /// assert_eq!(values[0].is_right(), false); 1754669f6dcSopenharmony_ci /// assert_eq!(values[1].is_right(), true); 1764669f6dcSopenharmony_ci /// ``` 1774669f6dcSopenharmony_ci pub fn is_right(&self) -> bool { 1784669f6dcSopenharmony_ci !self.is_left() 1794669f6dcSopenharmony_ci } 1804669f6dcSopenharmony_ci 1814669f6dcSopenharmony_ci /// Convert the left side of `Either<L, R>` to an `Option<L>`. 1824669f6dcSopenharmony_ci /// 1834669f6dcSopenharmony_ci /// ``` 1844669f6dcSopenharmony_ci /// use either::*; 1854669f6dcSopenharmony_ci /// 1864669f6dcSopenharmony_ci /// let left: Either<_, ()> = Left("some value"); 1874669f6dcSopenharmony_ci /// assert_eq!(left.left(), Some("some value")); 1884669f6dcSopenharmony_ci /// 1894669f6dcSopenharmony_ci /// let right: Either<(), _> = Right(321); 1904669f6dcSopenharmony_ci /// assert_eq!(right.left(), None); 1914669f6dcSopenharmony_ci /// ``` 1924669f6dcSopenharmony_ci pub fn left(self) -> Option<L> { 1934669f6dcSopenharmony_ci match self { 1944669f6dcSopenharmony_ci Left(l) => Some(l), 1954669f6dcSopenharmony_ci Right(_) => None, 1964669f6dcSopenharmony_ci } 1974669f6dcSopenharmony_ci } 1984669f6dcSopenharmony_ci 1994669f6dcSopenharmony_ci /// Convert the right side of `Either<L, R>` to an `Option<R>`. 2004669f6dcSopenharmony_ci /// 2014669f6dcSopenharmony_ci /// ``` 2024669f6dcSopenharmony_ci /// use either::*; 2034669f6dcSopenharmony_ci /// 2044669f6dcSopenharmony_ci /// let left: Either<_, ()> = Left("some value"); 2054669f6dcSopenharmony_ci /// assert_eq!(left.right(), None); 2064669f6dcSopenharmony_ci /// 2074669f6dcSopenharmony_ci /// let right: Either<(), _> = Right(321); 2084669f6dcSopenharmony_ci /// assert_eq!(right.right(), Some(321)); 2094669f6dcSopenharmony_ci /// ``` 2104669f6dcSopenharmony_ci pub fn right(self) -> Option<R> { 2114669f6dcSopenharmony_ci match self { 2124669f6dcSopenharmony_ci Left(_) => None, 2134669f6dcSopenharmony_ci Right(r) => Some(r), 2144669f6dcSopenharmony_ci } 2154669f6dcSopenharmony_ci } 2164669f6dcSopenharmony_ci 2174669f6dcSopenharmony_ci /// Convert `&Either<L, R>` to `Either<&L, &R>`. 2184669f6dcSopenharmony_ci /// 2194669f6dcSopenharmony_ci /// ``` 2204669f6dcSopenharmony_ci /// use either::*; 2214669f6dcSopenharmony_ci /// 2224669f6dcSopenharmony_ci /// let left: Either<_, ()> = Left("some value"); 2234669f6dcSopenharmony_ci /// assert_eq!(left.as_ref(), Left(&"some value")); 2244669f6dcSopenharmony_ci /// 2254669f6dcSopenharmony_ci /// let right: Either<(), _> = Right("some value"); 2264669f6dcSopenharmony_ci /// assert_eq!(right.as_ref(), Right(&"some value")); 2274669f6dcSopenharmony_ci /// ``` 2284669f6dcSopenharmony_ci pub fn as_ref(&self) -> Either<&L, &R> { 2294669f6dcSopenharmony_ci match *self { 2304669f6dcSopenharmony_ci Left(ref inner) => Left(inner), 2314669f6dcSopenharmony_ci Right(ref inner) => Right(inner), 2324669f6dcSopenharmony_ci } 2334669f6dcSopenharmony_ci } 2344669f6dcSopenharmony_ci 2354669f6dcSopenharmony_ci /// Convert `&mut Either<L, R>` to `Either<&mut L, &mut R>`. 2364669f6dcSopenharmony_ci /// 2374669f6dcSopenharmony_ci /// ``` 2384669f6dcSopenharmony_ci /// use either::*; 2394669f6dcSopenharmony_ci /// 2404669f6dcSopenharmony_ci /// fn mutate_left(value: &mut Either<u32, u32>) { 2414669f6dcSopenharmony_ci /// if let Some(l) = value.as_mut().left() { 2424669f6dcSopenharmony_ci /// *l = 999; 2434669f6dcSopenharmony_ci /// } 2444669f6dcSopenharmony_ci /// } 2454669f6dcSopenharmony_ci /// 2464669f6dcSopenharmony_ci /// let mut left = Left(123); 2474669f6dcSopenharmony_ci /// let mut right = Right(123); 2484669f6dcSopenharmony_ci /// mutate_left(&mut left); 2494669f6dcSopenharmony_ci /// mutate_left(&mut right); 2504669f6dcSopenharmony_ci /// assert_eq!(left, Left(999)); 2514669f6dcSopenharmony_ci /// assert_eq!(right, Right(123)); 2524669f6dcSopenharmony_ci /// ``` 2534669f6dcSopenharmony_ci pub fn as_mut(&mut self) -> Either<&mut L, &mut R> { 2544669f6dcSopenharmony_ci match *self { 2554669f6dcSopenharmony_ci Left(ref mut inner) => Left(inner), 2564669f6dcSopenharmony_ci Right(ref mut inner) => Right(inner), 2574669f6dcSopenharmony_ci } 2584669f6dcSopenharmony_ci } 2594669f6dcSopenharmony_ci 2604669f6dcSopenharmony_ci /// Convert `Pin<&Either<L, R>>` to `Either<Pin<&L>, Pin<&R>>`, 2614669f6dcSopenharmony_ci /// pinned projections of the inner variants. 2624669f6dcSopenharmony_ci pub fn as_pin_ref(self: Pin<&Self>) -> Either<Pin<&L>, Pin<&R>> { 2634669f6dcSopenharmony_ci // SAFETY: We can use `new_unchecked` because the `inner` parts are 2644669f6dcSopenharmony_ci // guaranteed to be pinned, as they come from `self` which is pinned. 2654669f6dcSopenharmony_ci unsafe { 2664669f6dcSopenharmony_ci match *Pin::get_ref(self) { 2674669f6dcSopenharmony_ci Left(ref inner) => Left(Pin::new_unchecked(inner)), 2684669f6dcSopenharmony_ci Right(ref inner) => Right(Pin::new_unchecked(inner)), 2694669f6dcSopenharmony_ci } 2704669f6dcSopenharmony_ci } 2714669f6dcSopenharmony_ci } 2724669f6dcSopenharmony_ci 2734669f6dcSopenharmony_ci /// Convert `Pin<&mut Either<L, R>>` to `Either<Pin<&mut L>, Pin<&mut R>>`, 2744669f6dcSopenharmony_ci /// pinned projections of the inner variants. 2754669f6dcSopenharmony_ci pub fn as_pin_mut(self: Pin<&mut Self>) -> Either<Pin<&mut L>, Pin<&mut R>> { 2764669f6dcSopenharmony_ci // SAFETY: `get_unchecked_mut` is fine because we don't move anything. 2774669f6dcSopenharmony_ci // We can use `new_unchecked` because the `inner` parts are guaranteed 2784669f6dcSopenharmony_ci // to be pinned, as they come from `self` which is pinned, and we never 2794669f6dcSopenharmony_ci // offer an unpinned `&mut L` or `&mut R` through `Pin<&mut Self>`. We 2804669f6dcSopenharmony_ci // also don't have an implementation of `Drop`, nor manual `Unpin`. 2814669f6dcSopenharmony_ci unsafe { 2824669f6dcSopenharmony_ci match *Pin::get_unchecked_mut(self) { 2834669f6dcSopenharmony_ci Left(ref mut inner) => Left(Pin::new_unchecked(inner)), 2844669f6dcSopenharmony_ci Right(ref mut inner) => Right(Pin::new_unchecked(inner)), 2854669f6dcSopenharmony_ci } 2864669f6dcSopenharmony_ci } 2874669f6dcSopenharmony_ci } 2884669f6dcSopenharmony_ci 2894669f6dcSopenharmony_ci /// Convert `Either<L, R>` to `Either<R, L>`. 2904669f6dcSopenharmony_ci /// 2914669f6dcSopenharmony_ci /// ``` 2924669f6dcSopenharmony_ci /// use either::*; 2934669f6dcSopenharmony_ci /// 2944669f6dcSopenharmony_ci /// let left: Either<_, ()> = Left(123); 2954669f6dcSopenharmony_ci /// assert_eq!(left.flip(), Right(123)); 2964669f6dcSopenharmony_ci /// 2974669f6dcSopenharmony_ci /// let right: Either<(), _> = Right("some value"); 2984669f6dcSopenharmony_ci /// assert_eq!(right.flip(), Left("some value")); 2994669f6dcSopenharmony_ci /// ``` 3004669f6dcSopenharmony_ci pub fn flip(self) -> Either<R, L> { 3014669f6dcSopenharmony_ci match self { 3024669f6dcSopenharmony_ci Left(l) => Right(l), 3034669f6dcSopenharmony_ci Right(r) => Left(r), 3044669f6dcSopenharmony_ci } 3054669f6dcSopenharmony_ci } 3064669f6dcSopenharmony_ci 3074669f6dcSopenharmony_ci /// Apply the function `f` on the value in the `Left` variant if it is present rewrapping the 3084669f6dcSopenharmony_ci /// result in `Left`. 3094669f6dcSopenharmony_ci /// 3104669f6dcSopenharmony_ci /// ``` 3114669f6dcSopenharmony_ci /// use either::*; 3124669f6dcSopenharmony_ci /// 3134669f6dcSopenharmony_ci /// let left: Either<_, u32> = Left(123); 3144669f6dcSopenharmony_ci /// assert_eq!(left.map_left(|x| x * 2), Left(246)); 3154669f6dcSopenharmony_ci /// 3164669f6dcSopenharmony_ci /// let right: Either<u32, _> = Right(123); 3174669f6dcSopenharmony_ci /// assert_eq!(right.map_left(|x| x * 2), Right(123)); 3184669f6dcSopenharmony_ci /// ``` 3194669f6dcSopenharmony_ci pub fn map_left<F, M>(self, f: F) -> Either<M, R> 3204669f6dcSopenharmony_ci where 3214669f6dcSopenharmony_ci F: FnOnce(L) -> M, 3224669f6dcSopenharmony_ci { 3234669f6dcSopenharmony_ci match self { 3244669f6dcSopenharmony_ci Left(l) => Left(f(l)), 3254669f6dcSopenharmony_ci Right(r) => Right(r), 3264669f6dcSopenharmony_ci } 3274669f6dcSopenharmony_ci } 3284669f6dcSopenharmony_ci 3294669f6dcSopenharmony_ci /// Apply the function `f` on the value in the `Right` variant if it is present rewrapping the 3304669f6dcSopenharmony_ci /// result in `Right`. 3314669f6dcSopenharmony_ci /// 3324669f6dcSopenharmony_ci /// ``` 3334669f6dcSopenharmony_ci /// use either::*; 3344669f6dcSopenharmony_ci /// 3354669f6dcSopenharmony_ci /// let left: Either<_, u32> = Left(123); 3364669f6dcSopenharmony_ci /// assert_eq!(left.map_right(|x| x * 2), Left(123)); 3374669f6dcSopenharmony_ci /// 3384669f6dcSopenharmony_ci /// let right: Either<u32, _> = Right(123); 3394669f6dcSopenharmony_ci /// assert_eq!(right.map_right(|x| x * 2), Right(246)); 3404669f6dcSopenharmony_ci /// ``` 3414669f6dcSopenharmony_ci pub fn map_right<F, S>(self, f: F) -> Either<L, S> 3424669f6dcSopenharmony_ci where 3434669f6dcSopenharmony_ci F: FnOnce(R) -> S, 3444669f6dcSopenharmony_ci { 3454669f6dcSopenharmony_ci match self { 3464669f6dcSopenharmony_ci Left(l) => Left(l), 3474669f6dcSopenharmony_ci Right(r) => Right(f(r)), 3484669f6dcSopenharmony_ci } 3494669f6dcSopenharmony_ci } 3504669f6dcSopenharmony_ci 3514669f6dcSopenharmony_ci /// Apply one of two functions depending on contents, unifying their result. If the value is 3524669f6dcSopenharmony_ci /// `Left(L)` then the first function `f` is applied; if it is `Right(R)` then the second 3534669f6dcSopenharmony_ci /// function `g` is applied. 3544669f6dcSopenharmony_ci /// 3554669f6dcSopenharmony_ci /// ``` 3564669f6dcSopenharmony_ci /// use either::*; 3574669f6dcSopenharmony_ci /// 3584669f6dcSopenharmony_ci /// fn square(n: u32) -> i32 { (n * n) as i32 } 3594669f6dcSopenharmony_ci /// fn negate(n: i32) -> i32 { -n } 3604669f6dcSopenharmony_ci /// 3614669f6dcSopenharmony_ci /// let left: Either<u32, i32> = Left(4); 3624669f6dcSopenharmony_ci /// assert_eq!(left.either(square, negate), 16); 3634669f6dcSopenharmony_ci /// 3644669f6dcSopenharmony_ci /// let right: Either<u32, i32> = Right(-4); 3654669f6dcSopenharmony_ci /// assert_eq!(right.either(square, negate), 4); 3664669f6dcSopenharmony_ci /// ``` 3674669f6dcSopenharmony_ci pub fn either<F, G, T>(self, f: F, g: G) -> T 3684669f6dcSopenharmony_ci where 3694669f6dcSopenharmony_ci F: FnOnce(L) -> T, 3704669f6dcSopenharmony_ci G: FnOnce(R) -> T, 3714669f6dcSopenharmony_ci { 3724669f6dcSopenharmony_ci match self { 3734669f6dcSopenharmony_ci Left(l) => f(l), 3744669f6dcSopenharmony_ci Right(r) => g(r), 3754669f6dcSopenharmony_ci } 3764669f6dcSopenharmony_ci } 3774669f6dcSopenharmony_ci 3784669f6dcSopenharmony_ci /// Like `either`, but provide some context to whichever of the 3794669f6dcSopenharmony_ci /// functions ends up being called. 3804669f6dcSopenharmony_ci /// 3814669f6dcSopenharmony_ci /// ``` 3824669f6dcSopenharmony_ci /// // In this example, the context is a mutable reference 3834669f6dcSopenharmony_ci /// use either::*; 3844669f6dcSopenharmony_ci /// 3854669f6dcSopenharmony_ci /// let mut result = Vec::new(); 3864669f6dcSopenharmony_ci /// 3874669f6dcSopenharmony_ci /// let values = vec![Left(2), Right(2.7)]; 3884669f6dcSopenharmony_ci /// 3894669f6dcSopenharmony_ci /// for value in values { 3904669f6dcSopenharmony_ci /// value.either_with(&mut result, 3914669f6dcSopenharmony_ci /// |ctx, integer| ctx.push(integer), 3924669f6dcSopenharmony_ci /// |ctx, real| ctx.push(f64::round(real) as i32)); 3934669f6dcSopenharmony_ci /// } 3944669f6dcSopenharmony_ci /// 3954669f6dcSopenharmony_ci /// assert_eq!(result, vec![2, 3]); 3964669f6dcSopenharmony_ci /// ``` 3974669f6dcSopenharmony_ci pub fn either_with<Ctx, F, G, T>(self, ctx: Ctx, f: F, g: G) -> T 3984669f6dcSopenharmony_ci where 3994669f6dcSopenharmony_ci F: FnOnce(Ctx, L) -> T, 4004669f6dcSopenharmony_ci G: FnOnce(Ctx, R) -> T, 4014669f6dcSopenharmony_ci { 4024669f6dcSopenharmony_ci match self { 4034669f6dcSopenharmony_ci Left(l) => f(ctx, l), 4044669f6dcSopenharmony_ci Right(r) => g(ctx, r), 4054669f6dcSopenharmony_ci } 4064669f6dcSopenharmony_ci } 4074669f6dcSopenharmony_ci 4084669f6dcSopenharmony_ci /// Apply the function `f` on the value in the `Left` variant if it is present. 4094669f6dcSopenharmony_ci /// 4104669f6dcSopenharmony_ci /// ``` 4114669f6dcSopenharmony_ci /// use either::*; 4124669f6dcSopenharmony_ci /// 4134669f6dcSopenharmony_ci /// let left: Either<_, u32> = Left(123); 4144669f6dcSopenharmony_ci /// assert_eq!(left.left_and_then::<_,()>(|x| Right(x * 2)), Right(246)); 4154669f6dcSopenharmony_ci /// 4164669f6dcSopenharmony_ci /// let right: Either<u32, _> = Right(123); 4174669f6dcSopenharmony_ci /// assert_eq!(right.left_and_then(|x| Right::<(), _>(x * 2)), Right(123)); 4184669f6dcSopenharmony_ci /// ``` 4194669f6dcSopenharmony_ci pub fn left_and_then<F, S>(self, f: F) -> Either<S, R> 4204669f6dcSopenharmony_ci where 4214669f6dcSopenharmony_ci F: FnOnce(L) -> Either<S, R>, 4224669f6dcSopenharmony_ci { 4234669f6dcSopenharmony_ci match self { 4244669f6dcSopenharmony_ci Left(l) => f(l), 4254669f6dcSopenharmony_ci Right(r) => Right(r), 4264669f6dcSopenharmony_ci } 4274669f6dcSopenharmony_ci } 4284669f6dcSopenharmony_ci 4294669f6dcSopenharmony_ci /// Apply the function `f` on the value in the `Right` variant if it is present. 4304669f6dcSopenharmony_ci /// 4314669f6dcSopenharmony_ci /// ``` 4324669f6dcSopenharmony_ci /// use either::*; 4334669f6dcSopenharmony_ci /// 4344669f6dcSopenharmony_ci /// let left: Either<_, u32> = Left(123); 4354669f6dcSopenharmony_ci /// assert_eq!(left.right_and_then(|x| Right(x * 2)), Left(123)); 4364669f6dcSopenharmony_ci /// 4374669f6dcSopenharmony_ci /// let right: Either<u32, _> = Right(123); 4384669f6dcSopenharmony_ci /// assert_eq!(right.right_and_then(|x| Right(x * 2)), Right(246)); 4394669f6dcSopenharmony_ci /// ``` 4404669f6dcSopenharmony_ci pub fn right_and_then<F, S>(self, f: F) -> Either<L, S> 4414669f6dcSopenharmony_ci where 4424669f6dcSopenharmony_ci F: FnOnce(R) -> Either<L, S>, 4434669f6dcSopenharmony_ci { 4444669f6dcSopenharmony_ci match self { 4454669f6dcSopenharmony_ci Left(l) => Left(l), 4464669f6dcSopenharmony_ci Right(r) => f(r), 4474669f6dcSopenharmony_ci } 4484669f6dcSopenharmony_ci } 4494669f6dcSopenharmony_ci 4504669f6dcSopenharmony_ci /// Convert the inner value to an iterator. 4514669f6dcSopenharmony_ci /// 4524669f6dcSopenharmony_ci /// ``` 4534669f6dcSopenharmony_ci /// use either::*; 4544669f6dcSopenharmony_ci /// 4554669f6dcSopenharmony_ci /// let left: Either<_, Vec<u32>> = Left(vec![1, 2, 3, 4, 5]); 4564669f6dcSopenharmony_ci /// let mut right: Either<Vec<u32>, _> = Right(vec![]); 4574669f6dcSopenharmony_ci /// right.extend(left.into_iter()); 4584669f6dcSopenharmony_ci /// assert_eq!(right, Right(vec![1, 2, 3, 4, 5])); 4594669f6dcSopenharmony_ci /// ``` 4604669f6dcSopenharmony_ci #[allow(clippy::should_implement_trait)] 4614669f6dcSopenharmony_ci pub fn into_iter(self) -> Either<L::IntoIter, R::IntoIter> 4624669f6dcSopenharmony_ci where 4634669f6dcSopenharmony_ci L: IntoIterator, 4644669f6dcSopenharmony_ci R: IntoIterator<Item = L::Item>, 4654669f6dcSopenharmony_ci { 4664669f6dcSopenharmony_ci match self { 4674669f6dcSopenharmony_ci Left(l) => Left(l.into_iter()), 4684669f6dcSopenharmony_ci Right(r) => Right(r.into_iter()), 4694669f6dcSopenharmony_ci } 4704669f6dcSopenharmony_ci } 4714669f6dcSopenharmony_ci 4724669f6dcSopenharmony_ci /// Return left value or given value 4734669f6dcSopenharmony_ci /// 4744669f6dcSopenharmony_ci /// Arguments passed to `left_or` are eagerly evaluated; if you are passing 4754669f6dcSopenharmony_ci /// the result of a function call, it is recommended to use [`left_or_else`], 4764669f6dcSopenharmony_ci /// which is lazily evaluated. 4774669f6dcSopenharmony_ci /// 4784669f6dcSopenharmony_ci /// [`left_or_else`]: #method.left_or_else 4794669f6dcSopenharmony_ci /// 4804669f6dcSopenharmony_ci /// # Examples 4814669f6dcSopenharmony_ci /// 4824669f6dcSopenharmony_ci /// ``` 4834669f6dcSopenharmony_ci /// # use either::*; 4844669f6dcSopenharmony_ci /// let left: Either<&str, &str> = Left("left"); 4854669f6dcSopenharmony_ci /// assert_eq!(left.left_or("foo"), "left"); 4864669f6dcSopenharmony_ci /// 4874669f6dcSopenharmony_ci /// let right: Either<&str, &str> = Right("right"); 4884669f6dcSopenharmony_ci /// assert_eq!(right.left_or("left"), "left"); 4894669f6dcSopenharmony_ci /// ``` 4904669f6dcSopenharmony_ci pub fn left_or(self, other: L) -> L { 4914669f6dcSopenharmony_ci match self { 4924669f6dcSopenharmony_ci Either::Left(l) => l, 4934669f6dcSopenharmony_ci Either::Right(_) => other, 4944669f6dcSopenharmony_ci } 4954669f6dcSopenharmony_ci } 4964669f6dcSopenharmony_ci 4974669f6dcSopenharmony_ci /// Return left or a default 4984669f6dcSopenharmony_ci /// 4994669f6dcSopenharmony_ci /// # Examples 5004669f6dcSopenharmony_ci /// 5014669f6dcSopenharmony_ci /// ``` 5024669f6dcSopenharmony_ci /// # use either::*; 5034669f6dcSopenharmony_ci /// let left: Either<String, u32> = Left("left".to_string()); 5044669f6dcSopenharmony_ci /// assert_eq!(left.left_or_default(), "left"); 5054669f6dcSopenharmony_ci /// 5064669f6dcSopenharmony_ci /// let right: Either<String, u32> = Right(42); 5074669f6dcSopenharmony_ci /// assert_eq!(right.left_or_default(), String::default()); 5084669f6dcSopenharmony_ci /// ``` 5094669f6dcSopenharmony_ci pub fn left_or_default(self) -> L 5104669f6dcSopenharmony_ci where 5114669f6dcSopenharmony_ci L: Default, 5124669f6dcSopenharmony_ci { 5134669f6dcSopenharmony_ci match self { 5144669f6dcSopenharmony_ci Either::Left(l) => l, 5154669f6dcSopenharmony_ci Either::Right(_) => L::default(), 5164669f6dcSopenharmony_ci } 5174669f6dcSopenharmony_ci } 5184669f6dcSopenharmony_ci 5194669f6dcSopenharmony_ci /// Returns left value or computes it from a closure 5204669f6dcSopenharmony_ci /// 5214669f6dcSopenharmony_ci /// # Examples 5224669f6dcSopenharmony_ci /// 5234669f6dcSopenharmony_ci /// ``` 5244669f6dcSopenharmony_ci /// # use either::*; 5254669f6dcSopenharmony_ci /// let left: Either<String, u32> = Left("3".to_string()); 5264669f6dcSopenharmony_ci /// assert_eq!(left.left_or_else(|_| unreachable!()), "3"); 5274669f6dcSopenharmony_ci /// 5284669f6dcSopenharmony_ci /// let right: Either<String, u32> = Right(3); 5294669f6dcSopenharmony_ci /// assert_eq!(right.left_or_else(|x| x.to_string()), "3"); 5304669f6dcSopenharmony_ci /// ``` 5314669f6dcSopenharmony_ci pub fn left_or_else<F>(self, f: F) -> L 5324669f6dcSopenharmony_ci where 5334669f6dcSopenharmony_ci F: FnOnce(R) -> L, 5344669f6dcSopenharmony_ci { 5354669f6dcSopenharmony_ci match self { 5364669f6dcSopenharmony_ci Either::Left(l) => l, 5374669f6dcSopenharmony_ci Either::Right(r) => f(r), 5384669f6dcSopenharmony_ci } 5394669f6dcSopenharmony_ci } 5404669f6dcSopenharmony_ci 5414669f6dcSopenharmony_ci /// Return right value or given value 5424669f6dcSopenharmony_ci /// 5434669f6dcSopenharmony_ci /// Arguments passed to `right_or` are eagerly evaluated; if you are passing 5444669f6dcSopenharmony_ci /// the result of a function call, it is recommended to use [`right_or_else`], 5454669f6dcSopenharmony_ci /// which is lazily evaluated. 5464669f6dcSopenharmony_ci /// 5474669f6dcSopenharmony_ci /// [`right_or_else`]: #method.right_or_else 5484669f6dcSopenharmony_ci /// 5494669f6dcSopenharmony_ci /// # Examples 5504669f6dcSopenharmony_ci /// 5514669f6dcSopenharmony_ci /// ``` 5524669f6dcSopenharmony_ci /// # use either::*; 5534669f6dcSopenharmony_ci /// let right: Either<&str, &str> = Right("right"); 5544669f6dcSopenharmony_ci /// assert_eq!(right.right_or("foo"), "right"); 5554669f6dcSopenharmony_ci /// 5564669f6dcSopenharmony_ci /// let left: Either<&str, &str> = Left("left"); 5574669f6dcSopenharmony_ci /// assert_eq!(left.right_or("right"), "right"); 5584669f6dcSopenharmony_ci /// ``` 5594669f6dcSopenharmony_ci pub fn right_or(self, other: R) -> R { 5604669f6dcSopenharmony_ci match self { 5614669f6dcSopenharmony_ci Either::Left(_) => other, 5624669f6dcSopenharmony_ci Either::Right(r) => r, 5634669f6dcSopenharmony_ci } 5644669f6dcSopenharmony_ci } 5654669f6dcSopenharmony_ci 5664669f6dcSopenharmony_ci /// Return right or a default 5674669f6dcSopenharmony_ci /// 5684669f6dcSopenharmony_ci /// # Examples 5694669f6dcSopenharmony_ci /// 5704669f6dcSopenharmony_ci /// ``` 5714669f6dcSopenharmony_ci /// # use either::*; 5724669f6dcSopenharmony_ci /// let left: Either<String, u32> = Left("left".to_string()); 5734669f6dcSopenharmony_ci /// assert_eq!(left.right_or_default(), u32::default()); 5744669f6dcSopenharmony_ci /// 5754669f6dcSopenharmony_ci /// let right: Either<String, u32> = Right(42); 5764669f6dcSopenharmony_ci /// assert_eq!(right.right_or_default(), 42); 5774669f6dcSopenharmony_ci /// ``` 5784669f6dcSopenharmony_ci pub fn right_or_default(self) -> R 5794669f6dcSopenharmony_ci where 5804669f6dcSopenharmony_ci R: Default, 5814669f6dcSopenharmony_ci { 5824669f6dcSopenharmony_ci match self { 5834669f6dcSopenharmony_ci Either::Left(_) => R::default(), 5844669f6dcSopenharmony_ci Either::Right(r) => r, 5854669f6dcSopenharmony_ci } 5864669f6dcSopenharmony_ci } 5874669f6dcSopenharmony_ci 5884669f6dcSopenharmony_ci /// Returns right value or computes it from a closure 5894669f6dcSopenharmony_ci /// 5904669f6dcSopenharmony_ci /// # Examples 5914669f6dcSopenharmony_ci /// 5924669f6dcSopenharmony_ci /// ``` 5934669f6dcSopenharmony_ci /// # use either::*; 5944669f6dcSopenharmony_ci /// let left: Either<String, u32> = Left("3".to_string()); 5954669f6dcSopenharmony_ci /// assert_eq!(left.right_or_else(|x| x.parse().unwrap()), 3); 5964669f6dcSopenharmony_ci /// 5974669f6dcSopenharmony_ci /// let right: Either<String, u32> = Right(3); 5984669f6dcSopenharmony_ci /// assert_eq!(right.right_or_else(|_| unreachable!()), 3); 5994669f6dcSopenharmony_ci /// ``` 6004669f6dcSopenharmony_ci pub fn right_or_else<F>(self, f: F) -> R 6014669f6dcSopenharmony_ci where 6024669f6dcSopenharmony_ci F: FnOnce(L) -> R, 6034669f6dcSopenharmony_ci { 6044669f6dcSopenharmony_ci match self { 6054669f6dcSopenharmony_ci Either::Left(l) => f(l), 6064669f6dcSopenharmony_ci Either::Right(r) => r, 6074669f6dcSopenharmony_ci } 6084669f6dcSopenharmony_ci } 6094669f6dcSopenharmony_ci 6104669f6dcSopenharmony_ci /// Returns the left value 6114669f6dcSopenharmony_ci /// 6124669f6dcSopenharmony_ci /// # Examples 6134669f6dcSopenharmony_ci /// 6144669f6dcSopenharmony_ci /// ``` 6154669f6dcSopenharmony_ci /// # use either::*; 6164669f6dcSopenharmony_ci /// let left: Either<_, ()> = Left(3); 6174669f6dcSopenharmony_ci /// assert_eq!(left.unwrap_left(), 3); 6184669f6dcSopenharmony_ci /// ``` 6194669f6dcSopenharmony_ci /// 6204669f6dcSopenharmony_ci /// # Panics 6214669f6dcSopenharmony_ci /// 6224669f6dcSopenharmony_ci /// When `Either` is a `Right` value 6234669f6dcSopenharmony_ci /// 6244669f6dcSopenharmony_ci /// ```should_panic 6254669f6dcSopenharmony_ci /// # use either::*; 6264669f6dcSopenharmony_ci /// let right: Either<(), _> = Right(3); 6274669f6dcSopenharmony_ci /// right.unwrap_left(); 6284669f6dcSopenharmony_ci /// ``` 6294669f6dcSopenharmony_ci pub fn unwrap_left(self) -> L 6304669f6dcSopenharmony_ci where 6314669f6dcSopenharmony_ci R: core::fmt::Debug, 6324669f6dcSopenharmony_ci { 6334669f6dcSopenharmony_ci match self { 6344669f6dcSopenharmony_ci Either::Left(l) => l, 6354669f6dcSopenharmony_ci Either::Right(r) => { 6364669f6dcSopenharmony_ci panic!("called `Either::unwrap_left()` on a `Right` value: {:?}", r) 6374669f6dcSopenharmony_ci } 6384669f6dcSopenharmony_ci } 6394669f6dcSopenharmony_ci } 6404669f6dcSopenharmony_ci 6414669f6dcSopenharmony_ci /// Returns the right value 6424669f6dcSopenharmony_ci /// 6434669f6dcSopenharmony_ci /// # Examples 6444669f6dcSopenharmony_ci /// 6454669f6dcSopenharmony_ci /// ``` 6464669f6dcSopenharmony_ci /// # use either::*; 6474669f6dcSopenharmony_ci /// let right: Either<(), _> = Right(3); 6484669f6dcSopenharmony_ci /// assert_eq!(right.unwrap_right(), 3); 6494669f6dcSopenharmony_ci /// ``` 6504669f6dcSopenharmony_ci /// 6514669f6dcSopenharmony_ci /// # Panics 6524669f6dcSopenharmony_ci /// 6534669f6dcSopenharmony_ci /// When `Either` is a `Left` value 6544669f6dcSopenharmony_ci /// 6554669f6dcSopenharmony_ci /// ```should_panic 6564669f6dcSopenharmony_ci /// # use either::*; 6574669f6dcSopenharmony_ci /// let left: Either<_, ()> = Left(3); 6584669f6dcSopenharmony_ci /// left.unwrap_right(); 6594669f6dcSopenharmony_ci /// ``` 6604669f6dcSopenharmony_ci pub fn unwrap_right(self) -> R 6614669f6dcSopenharmony_ci where 6624669f6dcSopenharmony_ci L: core::fmt::Debug, 6634669f6dcSopenharmony_ci { 6644669f6dcSopenharmony_ci match self { 6654669f6dcSopenharmony_ci Either::Right(r) => r, 6664669f6dcSopenharmony_ci Either::Left(l) => panic!("called `Either::unwrap_right()` on a `Left` value: {:?}", l), 6674669f6dcSopenharmony_ci } 6684669f6dcSopenharmony_ci } 6694669f6dcSopenharmony_ci 6704669f6dcSopenharmony_ci /// Returns the left value 6714669f6dcSopenharmony_ci /// 6724669f6dcSopenharmony_ci /// # Examples 6734669f6dcSopenharmony_ci /// 6744669f6dcSopenharmony_ci /// ``` 6754669f6dcSopenharmony_ci /// # use either::*; 6764669f6dcSopenharmony_ci /// let left: Either<_, ()> = Left(3); 6774669f6dcSopenharmony_ci /// assert_eq!(left.expect_left("value was Right"), 3); 6784669f6dcSopenharmony_ci /// ``` 6794669f6dcSopenharmony_ci /// 6804669f6dcSopenharmony_ci /// # Panics 6814669f6dcSopenharmony_ci /// 6824669f6dcSopenharmony_ci /// When `Either` is a `Right` value 6834669f6dcSopenharmony_ci /// 6844669f6dcSopenharmony_ci /// ```should_panic 6854669f6dcSopenharmony_ci /// # use either::*; 6864669f6dcSopenharmony_ci /// let right: Either<(), _> = Right(3); 6874669f6dcSopenharmony_ci /// right.expect_left("value was Right"); 6884669f6dcSopenharmony_ci /// ``` 6894669f6dcSopenharmony_ci pub fn expect_left(self, msg: &str) -> L 6904669f6dcSopenharmony_ci where 6914669f6dcSopenharmony_ci R: core::fmt::Debug, 6924669f6dcSopenharmony_ci { 6934669f6dcSopenharmony_ci match self { 6944669f6dcSopenharmony_ci Either::Left(l) => l, 6954669f6dcSopenharmony_ci Either::Right(r) => panic!("{}: {:?}", msg, r), 6964669f6dcSopenharmony_ci } 6974669f6dcSopenharmony_ci } 6984669f6dcSopenharmony_ci 6994669f6dcSopenharmony_ci /// Returns the right value 7004669f6dcSopenharmony_ci /// 7014669f6dcSopenharmony_ci /// # Examples 7024669f6dcSopenharmony_ci /// 7034669f6dcSopenharmony_ci /// ``` 7044669f6dcSopenharmony_ci /// # use either::*; 7054669f6dcSopenharmony_ci /// let right: Either<(), _> = Right(3); 7064669f6dcSopenharmony_ci /// assert_eq!(right.expect_right("value was Left"), 3); 7074669f6dcSopenharmony_ci /// ``` 7084669f6dcSopenharmony_ci /// 7094669f6dcSopenharmony_ci /// # Panics 7104669f6dcSopenharmony_ci /// 7114669f6dcSopenharmony_ci /// When `Either` is a `Left` value 7124669f6dcSopenharmony_ci /// 7134669f6dcSopenharmony_ci /// ```should_panic 7144669f6dcSopenharmony_ci /// # use either::*; 7154669f6dcSopenharmony_ci /// let left: Either<_, ()> = Left(3); 7164669f6dcSopenharmony_ci /// left.expect_right("value was Right"); 7174669f6dcSopenharmony_ci /// ``` 7184669f6dcSopenharmony_ci pub fn expect_right(self, msg: &str) -> R 7194669f6dcSopenharmony_ci where 7204669f6dcSopenharmony_ci L: core::fmt::Debug, 7214669f6dcSopenharmony_ci { 7224669f6dcSopenharmony_ci match self { 7234669f6dcSopenharmony_ci Either::Right(r) => r, 7244669f6dcSopenharmony_ci Either::Left(l) => panic!("{}: {:?}", msg, l), 7254669f6dcSopenharmony_ci } 7264669f6dcSopenharmony_ci } 7274669f6dcSopenharmony_ci 7284669f6dcSopenharmony_ci /// Convert the contained value into `T` 7294669f6dcSopenharmony_ci /// 7304669f6dcSopenharmony_ci /// # Examples 7314669f6dcSopenharmony_ci /// 7324669f6dcSopenharmony_ci /// ``` 7334669f6dcSopenharmony_ci /// # use either::*; 7344669f6dcSopenharmony_ci /// // Both u16 and u32 can be converted to u64. 7354669f6dcSopenharmony_ci /// let left: Either<u16, u32> = Left(3u16); 7364669f6dcSopenharmony_ci /// assert_eq!(left.either_into::<u64>(), 3u64); 7374669f6dcSopenharmony_ci /// let right: Either<u16, u32> = Right(7u32); 7384669f6dcSopenharmony_ci /// assert_eq!(right.either_into::<u64>(), 7u64); 7394669f6dcSopenharmony_ci /// ``` 7404669f6dcSopenharmony_ci pub fn either_into<T>(self) -> T 7414669f6dcSopenharmony_ci where 7424669f6dcSopenharmony_ci L: Into<T>, 7434669f6dcSopenharmony_ci R: Into<T>, 7444669f6dcSopenharmony_ci { 7454669f6dcSopenharmony_ci match self { 7464669f6dcSopenharmony_ci Either::Left(l) => l.into(), 7474669f6dcSopenharmony_ci Either::Right(r) => r.into(), 7484669f6dcSopenharmony_ci } 7494669f6dcSopenharmony_ci } 7504669f6dcSopenharmony_ci} 7514669f6dcSopenharmony_ci 7524669f6dcSopenharmony_ciimpl<L, R> Either<Option<L>, Option<R>> { 7534669f6dcSopenharmony_ci /// Factors out `None` from an `Either` of [`Option`]. 7544669f6dcSopenharmony_ci /// 7554669f6dcSopenharmony_ci /// ``` 7564669f6dcSopenharmony_ci /// use either::*; 7574669f6dcSopenharmony_ci /// let left: Either<_, Option<String>> = Left(Some(vec![0])); 7584669f6dcSopenharmony_ci /// assert_eq!(left.factor_none(), Some(Left(vec![0]))); 7594669f6dcSopenharmony_ci /// 7604669f6dcSopenharmony_ci /// let right: Either<Option<Vec<u8>>, _> = Right(Some(String::new())); 7614669f6dcSopenharmony_ci /// assert_eq!(right.factor_none(), Some(Right(String::new()))); 7624669f6dcSopenharmony_ci /// ``` 7634669f6dcSopenharmony_ci // TODO(MSRV): doc(alias) was stabilized in Rust 1.48 7644669f6dcSopenharmony_ci // #[doc(alias = "transpose")] 7654669f6dcSopenharmony_ci pub fn factor_none(self) -> Option<Either<L, R>> { 7664669f6dcSopenharmony_ci match self { 7674669f6dcSopenharmony_ci Left(l) => l.map(Either::Left), 7684669f6dcSopenharmony_ci Right(r) => r.map(Either::Right), 7694669f6dcSopenharmony_ci } 7704669f6dcSopenharmony_ci } 7714669f6dcSopenharmony_ci} 7724669f6dcSopenharmony_ci 7734669f6dcSopenharmony_ciimpl<L, R, E> Either<Result<L, E>, Result<R, E>> { 7744669f6dcSopenharmony_ci /// Factors out a homogenous type from an `Either` of [`Result`]. 7754669f6dcSopenharmony_ci /// 7764669f6dcSopenharmony_ci /// Here, the homogeneous type is the `Err` type of the [`Result`]. 7774669f6dcSopenharmony_ci /// 7784669f6dcSopenharmony_ci /// ``` 7794669f6dcSopenharmony_ci /// use either::*; 7804669f6dcSopenharmony_ci /// let left: Either<_, Result<String, u32>> = Left(Ok(vec![0])); 7814669f6dcSopenharmony_ci /// assert_eq!(left.factor_err(), Ok(Left(vec![0]))); 7824669f6dcSopenharmony_ci /// 7834669f6dcSopenharmony_ci /// let right: Either<Result<Vec<u8>, u32>, _> = Right(Ok(String::new())); 7844669f6dcSopenharmony_ci /// assert_eq!(right.factor_err(), Ok(Right(String::new()))); 7854669f6dcSopenharmony_ci /// ``` 7864669f6dcSopenharmony_ci // TODO(MSRV): doc(alias) was stabilized in Rust 1.48 7874669f6dcSopenharmony_ci // #[doc(alias = "transpose")] 7884669f6dcSopenharmony_ci pub fn factor_err(self) -> Result<Either<L, R>, E> { 7894669f6dcSopenharmony_ci match self { 7904669f6dcSopenharmony_ci Left(l) => l.map(Either::Left), 7914669f6dcSopenharmony_ci Right(r) => r.map(Either::Right), 7924669f6dcSopenharmony_ci } 7934669f6dcSopenharmony_ci } 7944669f6dcSopenharmony_ci} 7954669f6dcSopenharmony_ci 7964669f6dcSopenharmony_ciimpl<T, L, R> Either<Result<T, L>, Result<T, R>> { 7974669f6dcSopenharmony_ci /// Factors out a homogenous type from an `Either` of [`Result`]. 7984669f6dcSopenharmony_ci /// 7994669f6dcSopenharmony_ci /// Here, the homogeneous type is the `Ok` type of the [`Result`]. 8004669f6dcSopenharmony_ci /// 8014669f6dcSopenharmony_ci /// ``` 8024669f6dcSopenharmony_ci /// use either::*; 8034669f6dcSopenharmony_ci /// let left: Either<_, Result<u32, String>> = Left(Err(vec![0])); 8044669f6dcSopenharmony_ci /// assert_eq!(left.factor_ok(), Err(Left(vec![0]))); 8054669f6dcSopenharmony_ci /// 8064669f6dcSopenharmony_ci /// let right: Either<Result<u32, Vec<u8>>, _> = Right(Err(String::new())); 8074669f6dcSopenharmony_ci /// assert_eq!(right.factor_ok(), Err(Right(String::new()))); 8084669f6dcSopenharmony_ci /// ``` 8094669f6dcSopenharmony_ci // TODO(MSRV): doc(alias) was stabilized in Rust 1.48 8104669f6dcSopenharmony_ci // #[doc(alias = "transpose")] 8114669f6dcSopenharmony_ci pub fn factor_ok(self) -> Result<T, Either<L, R>> { 8124669f6dcSopenharmony_ci match self { 8134669f6dcSopenharmony_ci Left(l) => l.map_err(Either::Left), 8144669f6dcSopenharmony_ci Right(r) => r.map_err(Either::Right), 8154669f6dcSopenharmony_ci } 8164669f6dcSopenharmony_ci } 8174669f6dcSopenharmony_ci} 8184669f6dcSopenharmony_ci 8194669f6dcSopenharmony_ciimpl<T, L, R> Either<(T, L), (T, R)> { 8204669f6dcSopenharmony_ci /// Factor out a homogeneous type from an either of pairs. 8214669f6dcSopenharmony_ci /// 8224669f6dcSopenharmony_ci /// Here, the homogeneous type is the first element of the pairs. 8234669f6dcSopenharmony_ci /// 8244669f6dcSopenharmony_ci /// ``` 8254669f6dcSopenharmony_ci /// use either::*; 8264669f6dcSopenharmony_ci /// let left: Either<_, (u32, String)> = Left((123, vec![0])); 8274669f6dcSopenharmony_ci /// assert_eq!(left.factor_first().0, 123); 8284669f6dcSopenharmony_ci /// 8294669f6dcSopenharmony_ci /// let right: Either<(u32, Vec<u8>), _> = Right((123, String::new())); 8304669f6dcSopenharmony_ci /// assert_eq!(right.factor_first().0, 123); 8314669f6dcSopenharmony_ci /// ``` 8324669f6dcSopenharmony_ci pub fn factor_first(self) -> (T, Either<L, R>) { 8334669f6dcSopenharmony_ci match self { 8344669f6dcSopenharmony_ci Left((t, l)) => (t, Left(l)), 8354669f6dcSopenharmony_ci Right((t, r)) => (t, Right(r)), 8364669f6dcSopenharmony_ci } 8374669f6dcSopenharmony_ci } 8384669f6dcSopenharmony_ci} 8394669f6dcSopenharmony_ci 8404669f6dcSopenharmony_ciimpl<T, L, R> Either<(L, T), (R, T)> { 8414669f6dcSopenharmony_ci /// Factor out a homogeneous type from an either of pairs. 8424669f6dcSopenharmony_ci /// 8434669f6dcSopenharmony_ci /// Here, the homogeneous type is the second element of the pairs. 8444669f6dcSopenharmony_ci /// 8454669f6dcSopenharmony_ci /// ``` 8464669f6dcSopenharmony_ci /// use either::*; 8474669f6dcSopenharmony_ci /// let left: Either<_, (String, u32)> = Left((vec![0], 123)); 8484669f6dcSopenharmony_ci /// assert_eq!(left.factor_second().1, 123); 8494669f6dcSopenharmony_ci /// 8504669f6dcSopenharmony_ci /// let right: Either<(Vec<u8>, u32), _> = Right((String::new(), 123)); 8514669f6dcSopenharmony_ci /// assert_eq!(right.factor_second().1, 123); 8524669f6dcSopenharmony_ci /// ``` 8534669f6dcSopenharmony_ci pub fn factor_second(self) -> (Either<L, R>, T) { 8544669f6dcSopenharmony_ci match self { 8554669f6dcSopenharmony_ci Left((l, t)) => (Left(l), t), 8564669f6dcSopenharmony_ci Right((r, t)) => (Right(r), t), 8574669f6dcSopenharmony_ci } 8584669f6dcSopenharmony_ci } 8594669f6dcSopenharmony_ci} 8604669f6dcSopenharmony_ci 8614669f6dcSopenharmony_ciimpl<T> Either<T, T> { 8624669f6dcSopenharmony_ci /// Extract the value of an either over two equivalent types. 8634669f6dcSopenharmony_ci /// 8644669f6dcSopenharmony_ci /// ``` 8654669f6dcSopenharmony_ci /// use either::*; 8664669f6dcSopenharmony_ci /// 8674669f6dcSopenharmony_ci /// let left: Either<_, u32> = Left(123); 8684669f6dcSopenharmony_ci /// assert_eq!(left.into_inner(), 123); 8694669f6dcSopenharmony_ci /// 8704669f6dcSopenharmony_ci /// let right: Either<u32, _> = Right(123); 8714669f6dcSopenharmony_ci /// assert_eq!(right.into_inner(), 123); 8724669f6dcSopenharmony_ci /// ``` 8734669f6dcSopenharmony_ci pub fn into_inner(self) -> T { 8744669f6dcSopenharmony_ci for_both!(self, inner => inner) 8754669f6dcSopenharmony_ci } 8764669f6dcSopenharmony_ci 8774669f6dcSopenharmony_ci /// Map `f` over the contained value and return the result in the 8784669f6dcSopenharmony_ci /// corresponding variant. 8794669f6dcSopenharmony_ci /// 8804669f6dcSopenharmony_ci /// ``` 8814669f6dcSopenharmony_ci /// use either::*; 8824669f6dcSopenharmony_ci /// 8834669f6dcSopenharmony_ci /// let value: Either<_, i32> = Right(42); 8844669f6dcSopenharmony_ci /// 8854669f6dcSopenharmony_ci /// let other = value.map(|x| x * 2); 8864669f6dcSopenharmony_ci /// assert_eq!(other, Right(84)); 8874669f6dcSopenharmony_ci /// ``` 8884669f6dcSopenharmony_ci pub fn map<F, M>(self, f: F) -> Either<M, M> 8894669f6dcSopenharmony_ci where 8904669f6dcSopenharmony_ci F: FnOnce(T) -> M, 8914669f6dcSopenharmony_ci { 8924669f6dcSopenharmony_ci match self { 8934669f6dcSopenharmony_ci Left(l) => Left(f(l)), 8944669f6dcSopenharmony_ci Right(r) => Right(f(r)), 8954669f6dcSopenharmony_ci } 8964669f6dcSopenharmony_ci } 8974669f6dcSopenharmony_ci} 8984669f6dcSopenharmony_ci 8994669f6dcSopenharmony_ci/// Convert from `Result` to `Either` with `Ok => Right` and `Err => Left`. 9004669f6dcSopenharmony_ciimpl<L, R> From<Result<R, L>> for Either<L, R> { 9014669f6dcSopenharmony_ci fn from(r: Result<R, L>) -> Self { 9024669f6dcSopenharmony_ci match r { 9034669f6dcSopenharmony_ci Err(e) => Left(e), 9044669f6dcSopenharmony_ci Ok(o) => Right(o), 9054669f6dcSopenharmony_ci } 9064669f6dcSopenharmony_ci } 9074669f6dcSopenharmony_ci} 9084669f6dcSopenharmony_ci 9094669f6dcSopenharmony_ci/// Convert from `Either` to `Result` with `Right => Ok` and `Left => Err`. 9104669f6dcSopenharmony_ci#[allow(clippy::from_over_into)] // From requires RFC 2451, Rust 1.41 9114669f6dcSopenharmony_ciimpl<L, R> Into<Result<R, L>> for Either<L, R> { 9124669f6dcSopenharmony_ci fn into(self) -> Result<R, L> { 9134669f6dcSopenharmony_ci match self { 9144669f6dcSopenharmony_ci Left(l) => Err(l), 9154669f6dcSopenharmony_ci Right(r) => Ok(r), 9164669f6dcSopenharmony_ci } 9174669f6dcSopenharmony_ci } 9184669f6dcSopenharmony_ci} 9194669f6dcSopenharmony_ci 9204669f6dcSopenharmony_ciimpl<L, R, A> Extend<A> for Either<L, R> 9214669f6dcSopenharmony_ciwhere 9224669f6dcSopenharmony_ci L: Extend<A>, 9234669f6dcSopenharmony_ci R: Extend<A>, 9244669f6dcSopenharmony_ci{ 9254669f6dcSopenharmony_ci fn extend<T>(&mut self, iter: T) 9264669f6dcSopenharmony_ci where 9274669f6dcSopenharmony_ci T: IntoIterator<Item = A>, 9284669f6dcSopenharmony_ci { 9294669f6dcSopenharmony_ci for_both!(*self, ref mut inner => inner.extend(iter)) 9304669f6dcSopenharmony_ci } 9314669f6dcSopenharmony_ci} 9324669f6dcSopenharmony_ci 9334669f6dcSopenharmony_ci/// `Either<L, R>` is an iterator if both `L` and `R` are iterators. 9344669f6dcSopenharmony_ciimpl<L, R> Iterator for Either<L, R> 9354669f6dcSopenharmony_ciwhere 9364669f6dcSopenharmony_ci L: Iterator, 9374669f6dcSopenharmony_ci R: Iterator<Item = L::Item>, 9384669f6dcSopenharmony_ci{ 9394669f6dcSopenharmony_ci type Item = L::Item; 9404669f6dcSopenharmony_ci 9414669f6dcSopenharmony_ci fn next(&mut self) -> Option<Self::Item> { 9424669f6dcSopenharmony_ci for_both!(*self, ref mut inner => inner.next()) 9434669f6dcSopenharmony_ci } 9444669f6dcSopenharmony_ci 9454669f6dcSopenharmony_ci fn size_hint(&self) -> (usize, Option<usize>) { 9464669f6dcSopenharmony_ci for_both!(*self, ref inner => inner.size_hint()) 9474669f6dcSopenharmony_ci } 9484669f6dcSopenharmony_ci 9494669f6dcSopenharmony_ci fn fold<Acc, G>(self, init: Acc, f: G) -> Acc 9504669f6dcSopenharmony_ci where 9514669f6dcSopenharmony_ci G: FnMut(Acc, Self::Item) -> Acc, 9524669f6dcSopenharmony_ci { 9534669f6dcSopenharmony_ci for_both!(self, inner => inner.fold(init, f)) 9544669f6dcSopenharmony_ci } 9554669f6dcSopenharmony_ci 9564669f6dcSopenharmony_ci fn for_each<F>(self, f: F) 9574669f6dcSopenharmony_ci where 9584669f6dcSopenharmony_ci F: FnMut(Self::Item), 9594669f6dcSopenharmony_ci { 9604669f6dcSopenharmony_ci for_both!(self, inner => inner.for_each(f)) 9614669f6dcSopenharmony_ci } 9624669f6dcSopenharmony_ci 9634669f6dcSopenharmony_ci fn count(self) -> usize { 9644669f6dcSopenharmony_ci for_both!(self, inner => inner.count()) 9654669f6dcSopenharmony_ci } 9664669f6dcSopenharmony_ci 9674669f6dcSopenharmony_ci fn last(self) -> Option<Self::Item> { 9684669f6dcSopenharmony_ci for_both!(self, inner => inner.last()) 9694669f6dcSopenharmony_ci } 9704669f6dcSopenharmony_ci 9714669f6dcSopenharmony_ci fn nth(&mut self, n: usize) -> Option<Self::Item> { 9724669f6dcSopenharmony_ci for_both!(*self, ref mut inner => inner.nth(n)) 9734669f6dcSopenharmony_ci } 9744669f6dcSopenharmony_ci 9754669f6dcSopenharmony_ci fn collect<B>(self) -> B 9764669f6dcSopenharmony_ci where 9774669f6dcSopenharmony_ci B: iter::FromIterator<Self::Item>, 9784669f6dcSopenharmony_ci { 9794669f6dcSopenharmony_ci for_both!(self, inner => inner.collect()) 9804669f6dcSopenharmony_ci } 9814669f6dcSopenharmony_ci 9824669f6dcSopenharmony_ci fn partition<B, F>(self, f: F) -> (B, B) 9834669f6dcSopenharmony_ci where 9844669f6dcSopenharmony_ci B: Default + Extend<Self::Item>, 9854669f6dcSopenharmony_ci F: FnMut(&Self::Item) -> bool, 9864669f6dcSopenharmony_ci { 9874669f6dcSopenharmony_ci for_both!(self, inner => inner.partition(f)) 9884669f6dcSopenharmony_ci } 9894669f6dcSopenharmony_ci 9904669f6dcSopenharmony_ci fn all<F>(&mut self, f: F) -> bool 9914669f6dcSopenharmony_ci where 9924669f6dcSopenharmony_ci F: FnMut(Self::Item) -> bool, 9934669f6dcSopenharmony_ci { 9944669f6dcSopenharmony_ci for_both!(*self, ref mut inner => inner.all(f)) 9954669f6dcSopenharmony_ci } 9964669f6dcSopenharmony_ci 9974669f6dcSopenharmony_ci fn any<F>(&mut self, f: F) -> bool 9984669f6dcSopenharmony_ci where 9994669f6dcSopenharmony_ci F: FnMut(Self::Item) -> bool, 10004669f6dcSopenharmony_ci { 10014669f6dcSopenharmony_ci for_both!(*self, ref mut inner => inner.any(f)) 10024669f6dcSopenharmony_ci } 10034669f6dcSopenharmony_ci 10044669f6dcSopenharmony_ci fn find<P>(&mut self, predicate: P) -> Option<Self::Item> 10054669f6dcSopenharmony_ci where 10064669f6dcSopenharmony_ci P: FnMut(&Self::Item) -> bool, 10074669f6dcSopenharmony_ci { 10084669f6dcSopenharmony_ci for_both!(*self, ref mut inner => inner.find(predicate)) 10094669f6dcSopenharmony_ci } 10104669f6dcSopenharmony_ci 10114669f6dcSopenharmony_ci fn find_map<B, F>(&mut self, f: F) -> Option<B> 10124669f6dcSopenharmony_ci where 10134669f6dcSopenharmony_ci F: FnMut(Self::Item) -> Option<B>, 10144669f6dcSopenharmony_ci { 10154669f6dcSopenharmony_ci for_both!(*self, ref mut inner => inner.find_map(f)) 10164669f6dcSopenharmony_ci } 10174669f6dcSopenharmony_ci 10184669f6dcSopenharmony_ci fn position<P>(&mut self, predicate: P) -> Option<usize> 10194669f6dcSopenharmony_ci where 10204669f6dcSopenharmony_ci P: FnMut(Self::Item) -> bool, 10214669f6dcSopenharmony_ci { 10224669f6dcSopenharmony_ci for_both!(*self, ref mut inner => inner.position(predicate)) 10234669f6dcSopenharmony_ci } 10244669f6dcSopenharmony_ci} 10254669f6dcSopenharmony_ci 10264669f6dcSopenharmony_ciimpl<L, R> DoubleEndedIterator for Either<L, R> 10274669f6dcSopenharmony_ciwhere 10284669f6dcSopenharmony_ci L: DoubleEndedIterator, 10294669f6dcSopenharmony_ci R: DoubleEndedIterator<Item = L::Item>, 10304669f6dcSopenharmony_ci{ 10314669f6dcSopenharmony_ci fn next_back(&mut self) -> Option<Self::Item> { 10324669f6dcSopenharmony_ci for_both!(*self, ref mut inner => inner.next_back()) 10334669f6dcSopenharmony_ci } 10344669f6dcSopenharmony_ci 10354669f6dcSopenharmony_ci // TODO(MSRV): This was stabilized in Rust 1.37 10364669f6dcSopenharmony_ci // fn nth_back(&mut self, n: usize) -> Option<Self::Item> { 10374669f6dcSopenharmony_ci // for_both!(*self, ref mut inner => inner.nth_back(n)) 10384669f6dcSopenharmony_ci // } 10394669f6dcSopenharmony_ci 10404669f6dcSopenharmony_ci fn rfold<Acc, G>(self, init: Acc, f: G) -> Acc 10414669f6dcSopenharmony_ci where 10424669f6dcSopenharmony_ci G: FnMut(Acc, Self::Item) -> Acc, 10434669f6dcSopenharmony_ci { 10444669f6dcSopenharmony_ci for_both!(self, inner => inner.rfold(init, f)) 10454669f6dcSopenharmony_ci } 10464669f6dcSopenharmony_ci 10474669f6dcSopenharmony_ci fn rfind<P>(&mut self, predicate: P) -> Option<Self::Item> 10484669f6dcSopenharmony_ci where 10494669f6dcSopenharmony_ci P: FnMut(&Self::Item) -> bool, 10504669f6dcSopenharmony_ci { 10514669f6dcSopenharmony_ci for_both!(*self, ref mut inner => inner.rfind(predicate)) 10524669f6dcSopenharmony_ci } 10534669f6dcSopenharmony_ci} 10544669f6dcSopenharmony_ci 10554669f6dcSopenharmony_ciimpl<L, R> ExactSizeIterator for Either<L, R> 10564669f6dcSopenharmony_ciwhere 10574669f6dcSopenharmony_ci L: ExactSizeIterator, 10584669f6dcSopenharmony_ci R: ExactSizeIterator<Item = L::Item>, 10594669f6dcSopenharmony_ci{ 10604669f6dcSopenharmony_ci fn len(&self) -> usize { 10614669f6dcSopenharmony_ci for_both!(*self, ref inner => inner.len()) 10624669f6dcSopenharmony_ci } 10634669f6dcSopenharmony_ci} 10644669f6dcSopenharmony_ci 10654669f6dcSopenharmony_ciimpl<L, R> iter::FusedIterator for Either<L, R> 10664669f6dcSopenharmony_ciwhere 10674669f6dcSopenharmony_ci L: iter::FusedIterator, 10684669f6dcSopenharmony_ci R: iter::FusedIterator<Item = L::Item>, 10694669f6dcSopenharmony_ci{ 10704669f6dcSopenharmony_ci} 10714669f6dcSopenharmony_ci 10724669f6dcSopenharmony_ci/// `Either<L, R>` is a future if both `L` and `R` are futures. 10734669f6dcSopenharmony_ciimpl<L, R> Future for Either<L, R> 10744669f6dcSopenharmony_ciwhere 10754669f6dcSopenharmony_ci L: Future, 10764669f6dcSopenharmony_ci R: Future<Output = L::Output>, 10774669f6dcSopenharmony_ci{ 10784669f6dcSopenharmony_ci type Output = L::Output; 10794669f6dcSopenharmony_ci 10804669f6dcSopenharmony_ci fn poll( 10814669f6dcSopenharmony_ci self: Pin<&mut Self>, 10824669f6dcSopenharmony_ci cx: &mut core::task::Context<'_>, 10834669f6dcSopenharmony_ci ) -> core::task::Poll<Self::Output> { 10844669f6dcSopenharmony_ci for_both!(self.as_pin_mut(), inner => inner.poll(cx)) 10854669f6dcSopenharmony_ci } 10864669f6dcSopenharmony_ci} 10874669f6dcSopenharmony_ci 10884669f6dcSopenharmony_ci#[cfg(any(test, feature = "use_std"))] 10894669f6dcSopenharmony_ci/// `Either<L, R>` implements `Read` if both `L` and `R` do. 10904669f6dcSopenharmony_ci/// 10914669f6dcSopenharmony_ci/// Requires crate feature `"use_std"` 10924669f6dcSopenharmony_ciimpl<L, R> Read for Either<L, R> 10934669f6dcSopenharmony_ciwhere 10944669f6dcSopenharmony_ci L: Read, 10954669f6dcSopenharmony_ci R: Read, 10964669f6dcSopenharmony_ci{ 10974669f6dcSopenharmony_ci fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { 10984669f6dcSopenharmony_ci for_both!(*self, ref mut inner => inner.read(buf)) 10994669f6dcSopenharmony_ci } 11004669f6dcSopenharmony_ci 11014669f6dcSopenharmony_ci fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { 11024669f6dcSopenharmony_ci for_both!(*self, ref mut inner => inner.read_exact(buf)) 11034669f6dcSopenharmony_ci } 11044669f6dcSopenharmony_ci 11054669f6dcSopenharmony_ci fn read_to_end(&mut self, buf: &mut std::vec::Vec<u8>) -> io::Result<usize> { 11064669f6dcSopenharmony_ci for_both!(*self, ref mut inner => inner.read_to_end(buf)) 11074669f6dcSopenharmony_ci } 11084669f6dcSopenharmony_ci 11094669f6dcSopenharmony_ci fn read_to_string(&mut self, buf: &mut std::string::String) -> io::Result<usize> { 11104669f6dcSopenharmony_ci for_both!(*self, ref mut inner => inner.read_to_string(buf)) 11114669f6dcSopenharmony_ci } 11124669f6dcSopenharmony_ci} 11134669f6dcSopenharmony_ci 11144669f6dcSopenharmony_ci#[cfg(any(test, feature = "use_std"))] 11154669f6dcSopenharmony_ci/// `Either<L, R>` implements `Seek` if both `L` and `R` do. 11164669f6dcSopenharmony_ci/// 11174669f6dcSopenharmony_ci/// Requires crate feature `"use_std"` 11184669f6dcSopenharmony_ciimpl<L, R> Seek for Either<L, R> 11194669f6dcSopenharmony_ciwhere 11204669f6dcSopenharmony_ci L: Seek, 11214669f6dcSopenharmony_ci R: Seek, 11224669f6dcSopenharmony_ci{ 11234669f6dcSopenharmony_ci fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> { 11244669f6dcSopenharmony_ci for_both!(*self, ref mut inner => inner.seek(pos)) 11254669f6dcSopenharmony_ci } 11264669f6dcSopenharmony_ci} 11274669f6dcSopenharmony_ci 11284669f6dcSopenharmony_ci#[cfg(any(test, feature = "use_std"))] 11294669f6dcSopenharmony_ci/// Requires crate feature `"use_std"` 11304669f6dcSopenharmony_ciimpl<L, R> BufRead for Either<L, R> 11314669f6dcSopenharmony_ciwhere 11324669f6dcSopenharmony_ci L: BufRead, 11334669f6dcSopenharmony_ci R: BufRead, 11344669f6dcSopenharmony_ci{ 11354669f6dcSopenharmony_ci fn fill_buf(&mut self) -> io::Result<&[u8]> { 11364669f6dcSopenharmony_ci for_both!(*self, ref mut inner => inner.fill_buf()) 11374669f6dcSopenharmony_ci } 11384669f6dcSopenharmony_ci 11394669f6dcSopenharmony_ci fn consume(&mut self, amt: usize) { 11404669f6dcSopenharmony_ci for_both!(*self, ref mut inner => inner.consume(amt)) 11414669f6dcSopenharmony_ci } 11424669f6dcSopenharmony_ci 11434669f6dcSopenharmony_ci fn read_until(&mut self, byte: u8, buf: &mut std::vec::Vec<u8>) -> io::Result<usize> { 11444669f6dcSopenharmony_ci for_both!(*self, ref mut inner => inner.read_until(byte, buf)) 11454669f6dcSopenharmony_ci } 11464669f6dcSopenharmony_ci 11474669f6dcSopenharmony_ci fn read_line(&mut self, buf: &mut std::string::String) -> io::Result<usize> { 11484669f6dcSopenharmony_ci for_both!(*self, ref mut inner => inner.read_line(buf)) 11494669f6dcSopenharmony_ci } 11504669f6dcSopenharmony_ci} 11514669f6dcSopenharmony_ci 11524669f6dcSopenharmony_ci#[cfg(any(test, feature = "use_std"))] 11534669f6dcSopenharmony_ci/// `Either<L, R>` implements `Write` if both `L` and `R` do. 11544669f6dcSopenharmony_ci/// 11554669f6dcSopenharmony_ci/// Requires crate feature `"use_std"` 11564669f6dcSopenharmony_ciimpl<L, R> Write for Either<L, R> 11574669f6dcSopenharmony_ciwhere 11584669f6dcSopenharmony_ci L: Write, 11594669f6dcSopenharmony_ci R: Write, 11604669f6dcSopenharmony_ci{ 11614669f6dcSopenharmony_ci fn write(&mut self, buf: &[u8]) -> io::Result<usize> { 11624669f6dcSopenharmony_ci for_both!(*self, ref mut inner => inner.write(buf)) 11634669f6dcSopenharmony_ci } 11644669f6dcSopenharmony_ci 11654669f6dcSopenharmony_ci fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { 11664669f6dcSopenharmony_ci for_both!(*self, ref mut inner => inner.write_all(buf)) 11674669f6dcSopenharmony_ci } 11684669f6dcSopenharmony_ci 11694669f6dcSopenharmony_ci fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> { 11704669f6dcSopenharmony_ci for_both!(*self, ref mut inner => inner.write_fmt(fmt)) 11714669f6dcSopenharmony_ci } 11724669f6dcSopenharmony_ci 11734669f6dcSopenharmony_ci fn flush(&mut self) -> io::Result<()> { 11744669f6dcSopenharmony_ci for_both!(*self, ref mut inner => inner.flush()) 11754669f6dcSopenharmony_ci } 11764669f6dcSopenharmony_ci} 11774669f6dcSopenharmony_ci 11784669f6dcSopenharmony_ciimpl<L, R, Target> AsRef<Target> for Either<L, R> 11794669f6dcSopenharmony_ciwhere 11804669f6dcSopenharmony_ci L: AsRef<Target>, 11814669f6dcSopenharmony_ci R: AsRef<Target>, 11824669f6dcSopenharmony_ci{ 11834669f6dcSopenharmony_ci fn as_ref(&self) -> &Target { 11844669f6dcSopenharmony_ci for_both!(*self, ref inner => inner.as_ref()) 11854669f6dcSopenharmony_ci } 11864669f6dcSopenharmony_ci} 11874669f6dcSopenharmony_ci 11884669f6dcSopenharmony_cimacro_rules! impl_specific_ref_and_mut { 11894669f6dcSopenharmony_ci ($t:ty, $($attr:meta),* ) => { 11904669f6dcSopenharmony_ci $(#[$attr])* 11914669f6dcSopenharmony_ci impl<L, R> AsRef<$t> for Either<L, R> 11924669f6dcSopenharmony_ci where L: AsRef<$t>, R: AsRef<$t> 11934669f6dcSopenharmony_ci { 11944669f6dcSopenharmony_ci fn as_ref(&self) -> &$t { 11954669f6dcSopenharmony_ci for_both!(*self, ref inner => inner.as_ref()) 11964669f6dcSopenharmony_ci } 11974669f6dcSopenharmony_ci } 11984669f6dcSopenharmony_ci 11994669f6dcSopenharmony_ci $(#[$attr])* 12004669f6dcSopenharmony_ci impl<L, R> AsMut<$t> for Either<L, R> 12014669f6dcSopenharmony_ci where L: AsMut<$t>, R: AsMut<$t> 12024669f6dcSopenharmony_ci { 12034669f6dcSopenharmony_ci fn as_mut(&mut self) -> &mut $t { 12044669f6dcSopenharmony_ci for_both!(*self, ref mut inner => inner.as_mut()) 12054669f6dcSopenharmony_ci } 12064669f6dcSopenharmony_ci } 12074669f6dcSopenharmony_ci }; 12084669f6dcSopenharmony_ci} 12094669f6dcSopenharmony_ci 12104669f6dcSopenharmony_ciimpl_specific_ref_and_mut!(str,); 12114669f6dcSopenharmony_ciimpl_specific_ref_and_mut!( 12124669f6dcSopenharmony_ci ::std::path::Path, 12134669f6dcSopenharmony_ci cfg(feature = "use_std"), 12144669f6dcSopenharmony_ci doc = "Requires crate feature `use_std`." 12154669f6dcSopenharmony_ci); 12164669f6dcSopenharmony_ciimpl_specific_ref_and_mut!( 12174669f6dcSopenharmony_ci ::std::ffi::OsStr, 12184669f6dcSopenharmony_ci cfg(feature = "use_std"), 12194669f6dcSopenharmony_ci doc = "Requires crate feature `use_std`." 12204669f6dcSopenharmony_ci); 12214669f6dcSopenharmony_ciimpl_specific_ref_and_mut!( 12224669f6dcSopenharmony_ci ::std::ffi::CStr, 12234669f6dcSopenharmony_ci cfg(feature = "use_std"), 12244669f6dcSopenharmony_ci doc = "Requires crate feature `use_std`." 12254669f6dcSopenharmony_ci); 12264669f6dcSopenharmony_ci 12274669f6dcSopenharmony_ciimpl<L, R, Target> AsRef<[Target]> for Either<L, R> 12284669f6dcSopenharmony_ciwhere 12294669f6dcSopenharmony_ci L: AsRef<[Target]>, 12304669f6dcSopenharmony_ci R: AsRef<[Target]>, 12314669f6dcSopenharmony_ci{ 12324669f6dcSopenharmony_ci fn as_ref(&self) -> &[Target] { 12334669f6dcSopenharmony_ci for_both!(*self, ref inner => inner.as_ref()) 12344669f6dcSopenharmony_ci } 12354669f6dcSopenharmony_ci} 12364669f6dcSopenharmony_ci 12374669f6dcSopenharmony_ciimpl<L, R, Target> AsMut<Target> for Either<L, R> 12384669f6dcSopenharmony_ciwhere 12394669f6dcSopenharmony_ci L: AsMut<Target>, 12404669f6dcSopenharmony_ci R: AsMut<Target>, 12414669f6dcSopenharmony_ci{ 12424669f6dcSopenharmony_ci fn as_mut(&mut self) -> &mut Target { 12434669f6dcSopenharmony_ci for_both!(*self, ref mut inner => inner.as_mut()) 12444669f6dcSopenharmony_ci } 12454669f6dcSopenharmony_ci} 12464669f6dcSopenharmony_ci 12474669f6dcSopenharmony_ciimpl<L, R, Target> AsMut<[Target]> for Either<L, R> 12484669f6dcSopenharmony_ciwhere 12494669f6dcSopenharmony_ci L: AsMut<[Target]>, 12504669f6dcSopenharmony_ci R: AsMut<[Target]>, 12514669f6dcSopenharmony_ci{ 12524669f6dcSopenharmony_ci fn as_mut(&mut self) -> &mut [Target] { 12534669f6dcSopenharmony_ci for_both!(*self, ref mut inner => inner.as_mut()) 12544669f6dcSopenharmony_ci } 12554669f6dcSopenharmony_ci} 12564669f6dcSopenharmony_ci 12574669f6dcSopenharmony_ciimpl<L, R> Deref for Either<L, R> 12584669f6dcSopenharmony_ciwhere 12594669f6dcSopenharmony_ci L: Deref, 12604669f6dcSopenharmony_ci R: Deref<Target = L::Target>, 12614669f6dcSopenharmony_ci{ 12624669f6dcSopenharmony_ci type Target = L::Target; 12634669f6dcSopenharmony_ci 12644669f6dcSopenharmony_ci fn deref(&self) -> &Self::Target { 12654669f6dcSopenharmony_ci for_both!(*self, ref inner => &**inner) 12664669f6dcSopenharmony_ci } 12674669f6dcSopenharmony_ci} 12684669f6dcSopenharmony_ci 12694669f6dcSopenharmony_ciimpl<L, R> DerefMut for Either<L, R> 12704669f6dcSopenharmony_ciwhere 12714669f6dcSopenharmony_ci L: DerefMut, 12724669f6dcSopenharmony_ci R: DerefMut<Target = L::Target>, 12734669f6dcSopenharmony_ci{ 12744669f6dcSopenharmony_ci fn deref_mut(&mut self) -> &mut Self::Target { 12754669f6dcSopenharmony_ci for_both!(*self, ref mut inner => &mut *inner) 12764669f6dcSopenharmony_ci } 12774669f6dcSopenharmony_ci} 12784669f6dcSopenharmony_ci 12794669f6dcSopenharmony_ci#[cfg(any(test, feature = "use_std"))] 12804669f6dcSopenharmony_ci/// `Either` implements `Error` if *both* `L` and `R` implement it. 12814669f6dcSopenharmony_ciimpl<L, R> Error for Either<L, R> 12824669f6dcSopenharmony_ciwhere 12834669f6dcSopenharmony_ci L: Error, 12844669f6dcSopenharmony_ci R: Error, 12854669f6dcSopenharmony_ci{ 12864669f6dcSopenharmony_ci fn source(&self) -> Option<&(dyn Error + 'static)> { 12874669f6dcSopenharmony_ci for_both!(*self, ref inner => inner.source()) 12884669f6dcSopenharmony_ci } 12894669f6dcSopenharmony_ci 12904669f6dcSopenharmony_ci #[allow(deprecated)] 12914669f6dcSopenharmony_ci fn description(&self) -> &str { 12924669f6dcSopenharmony_ci for_both!(*self, ref inner => inner.description()) 12934669f6dcSopenharmony_ci } 12944669f6dcSopenharmony_ci 12954669f6dcSopenharmony_ci #[allow(deprecated)] 12964669f6dcSopenharmony_ci fn cause(&self) -> Option<&dyn Error> { 12974669f6dcSopenharmony_ci for_both!(*self, ref inner => inner.cause()) 12984669f6dcSopenharmony_ci } 12994669f6dcSopenharmony_ci} 13004669f6dcSopenharmony_ci 13014669f6dcSopenharmony_ciimpl<L, R> fmt::Display for Either<L, R> 13024669f6dcSopenharmony_ciwhere 13034669f6dcSopenharmony_ci L: fmt::Display, 13044669f6dcSopenharmony_ci R: fmt::Display, 13054669f6dcSopenharmony_ci{ 13064669f6dcSopenharmony_ci fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 13074669f6dcSopenharmony_ci for_both!(*self, ref inner => inner.fmt(f)) 13084669f6dcSopenharmony_ci } 13094669f6dcSopenharmony_ci} 13104669f6dcSopenharmony_ci 13114669f6dcSopenharmony_ci#[test] 13124669f6dcSopenharmony_cifn basic() { 13134669f6dcSopenharmony_ci let mut e = Left(2); 13144669f6dcSopenharmony_ci let r = Right(2); 13154669f6dcSopenharmony_ci assert_eq!(e, Left(2)); 13164669f6dcSopenharmony_ci e = r; 13174669f6dcSopenharmony_ci assert_eq!(e, Right(2)); 13184669f6dcSopenharmony_ci assert_eq!(e.left(), None); 13194669f6dcSopenharmony_ci assert_eq!(e.right(), Some(2)); 13204669f6dcSopenharmony_ci assert_eq!(e.as_ref().right(), Some(&2)); 13214669f6dcSopenharmony_ci assert_eq!(e.as_mut().right(), Some(&mut 2)); 13224669f6dcSopenharmony_ci} 13234669f6dcSopenharmony_ci 13244669f6dcSopenharmony_ci#[test] 13254669f6dcSopenharmony_cifn macros() { 13264669f6dcSopenharmony_ci use std::string::String; 13274669f6dcSopenharmony_ci 13284669f6dcSopenharmony_ci fn a() -> Either<u32, u32> { 13294669f6dcSopenharmony_ci let x: u32 = try_left!(Right(1337u32)); 13304669f6dcSopenharmony_ci Left(x * 2) 13314669f6dcSopenharmony_ci } 13324669f6dcSopenharmony_ci assert_eq!(a(), Right(1337)); 13334669f6dcSopenharmony_ci 13344669f6dcSopenharmony_ci fn b() -> Either<String, &'static str> { 13354669f6dcSopenharmony_ci Right(try_right!(Left("foo bar"))) 13364669f6dcSopenharmony_ci } 13374669f6dcSopenharmony_ci assert_eq!(b(), Left(String::from("foo bar"))); 13384669f6dcSopenharmony_ci} 13394669f6dcSopenharmony_ci 13404669f6dcSopenharmony_ci#[test] 13414669f6dcSopenharmony_cifn deref() { 13424669f6dcSopenharmony_ci use std::string::String; 13434669f6dcSopenharmony_ci 13444669f6dcSopenharmony_ci fn is_str(_: &str) {} 13454669f6dcSopenharmony_ci let value: Either<String, &str> = Left(String::from("test")); 13464669f6dcSopenharmony_ci is_str(&*value); 13474669f6dcSopenharmony_ci} 13484669f6dcSopenharmony_ci 13494669f6dcSopenharmony_ci#[test] 13504669f6dcSopenharmony_cifn iter() { 13514669f6dcSopenharmony_ci let x = 3; 13524669f6dcSopenharmony_ci let mut iter = match x { 13534669f6dcSopenharmony_ci 3 => Left(0..10), 13544669f6dcSopenharmony_ci _ => Right(17..), 13554669f6dcSopenharmony_ci }; 13564669f6dcSopenharmony_ci 13574669f6dcSopenharmony_ci assert_eq!(iter.next(), Some(0)); 13584669f6dcSopenharmony_ci assert_eq!(iter.count(), 9); 13594669f6dcSopenharmony_ci} 13604669f6dcSopenharmony_ci 13614669f6dcSopenharmony_ci#[test] 13624669f6dcSopenharmony_cifn seek() { 13634669f6dcSopenharmony_ci use std::io; 13644669f6dcSopenharmony_ci 13654669f6dcSopenharmony_ci let use_empty = false; 13664669f6dcSopenharmony_ci let mut mockdata = [0x00; 256]; 13674669f6dcSopenharmony_ci for i in 0..256 { 13684669f6dcSopenharmony_ci mockdata[i] = i as u8; 13694669f6dcSopenharmony_ci } 13704669f6dcSopenharmony_ci 13714669f6dcSopenharmony_ci let mut reader = if use_empty { 13724669f6dcSopenharmony_ci // Empty didn't impl Seek until Rust 1.51 13734669f6dcSopenharmony_ci Left(io::Cursor::new([])) 13744669f6dcSopenharmony_ci } else { 13754669f6dcSopenharmony_ci Right(io::Cursor::new(&mockdata[..])) 13764669f6dcSopenharmony_ci }; 13774669f6dcSopenharmony_ci 13784669f6dcSopenharmony_ci let mut buf = [0u8; 16]; 13794669f6dcSopenharmony_ci assert_eq!(reader.read(&mut buf).unwrap(), buf.len()); 13804669f6dcSopenharmony_ci assert_eq!(buf, mockdata[..buf.len()]); 13814669f6dcSopenharmony_ci 13824669f6dcSopenharmony_ci // the first read should advance the cursor and return the next 16 bytes thus the `ne` 13834669f6dcSopenharmony_ci assert_eq!(reader.read(&mut buf).unwrap(), buf.len()); 13844669f6dcSopenharmony_ci assert_ne!(buf, mockdata[..buf.len()]); 13854669f6dcSopenharmony_ci 13864669f6dcSopenharmony_ci // if the seek operation fails it should read 16..31 instead of 0..15 13874669f6dcSopenharmony_ci reader.seek(io::SeekFrom::Start(0)).unwrap(); 13884669f6dcSopenharmony_ci assert_eq!(reader.read(&mut buf).unwrap(), buf.len()); 13894669f6dcSopenharmony_ci assert_eq!(buf, mockdata[..buf.len()]); 13904669f6dcSopenharmony_ci} 13914669f6dcSopenharmony_ci 13924669f6dcSopenharmony_ci#[test] 13934669f6dcSopenharmony_cifn read_write() { 13944669f6dcSopenharmony_ci use std::io; 13954669f6dcSopenharmony_ci 13964669f6dcSopenharmony_ci let use_stdio = false; 13974669f6dcSopenharmony_ci let mockdata = [0xff; 256]; 13984669f6dcSopenharmony_ci 13994669f6dcSopenharmony_ci let mut reader = if use_stdio { 14004669f6dcSopenharmony_ci Left(io::stdin()) 14014669f6dcSopenharmony_ci } else { 14024669f6dcSopenharmony_ci Right(&mockdata[..]) 14034669f6dcSopenharmony_ci }; 14044669f6dcSopenharmony_ci 14054669f6dcSopenharmony_ci let mut buf = [0u8; 16]; 14064669f6dcSopenharmony_ci assert_eq!(reader.read(&mut buf).unwrap(), buf.len()); 14074669f6dcSopenharmony_ci assert_eq!(&buf, &mockdata[..buf.len()]); 14084669f6dcSopenharmony_ci 14094669f6dcSopenharmony_ci let mut mockbuf = [0u8; 256]; 14104669f6dcSopenharmony_ci let mut writer = if use_stdio { 14114669f6dcSopenharmony_ci Left(io::stdout()) 14124669f6dcSopenharmony_ci } else { 14134669f6dcSopenharmony_ci Right(&mut mockbuf[..]) 14144669f6dcSopenharmony_ci }; 14154669f6dcSopenharmony_ci 14164669f6dcSopenharmony_ci let buf = [1u8; 16]; 14174669f6dcSopenharmony_ci assert_eq!(writer.write(&buf).unwrap(), buf.len()); 14184669f6dcSopenharmony_ci} 14194669f6dcSopenharmony_ci 14204669f6dcSopenharmony_ci#[test] 14214669f6dcSopenharmony_ci#[allow(deprecated)] 14224669f6dcSopenharmony_cifn error() { 14234669f6dcSopenharmony_ci let invalid_utf8 = b"\xff"; 14244669f6dcSopenharmony_ci let res = if let Err(error) = ::std::str::from_utf8(invalid_utf8) { 14254669f6dcSopenharmony_ci Err(Left(error)) 14264669f6dcSopenharmony_ci } else if let Err(error) = "x".parse::<i32>() { 14274669f6dcSopenharmony_ci Err(Right(error)) 14284669f6dcSopenharmony_ci } else { 14294669f6dcSopenharmony_ci Ok(()) 14304669f6dcSopenharmony_ci }; 14314669f6dcSopenharmony_ci assert!(res.is_err()); 14324669f6dcSopenharmony_ci res.unwrap_err().description(); // make sure this can be called 14334669f6dcSopenharmony_ci} 14344669f6dcSopenharmony_ci 14354669f6dcSopenharmony_ci/// A helper macro to check if AsRef and AsMut are implemented for a given type. 14364669f6dcSopenharmony_cimacro_rules! check_t { 14374669f6dcSopenharmony_ci ($t:ty) => {{ 14384669f6dcSopenharmony_ci fn check_ref<T: AsRef<$t>>() {} 14394669f6dcSopenharmony_ci fn propagate_ref<T1: AsRef<$t>, T2: AsRef<$t>>() { 14404669f6dcSopenharmony_ci check_ref::<Either<T1, T2>>() 14414669f6dcSopenharmony_ci } 14424669f6dcSopenharmony_ci fn check_mut<T: AsMut<$t>>() {} 14434669f6dcSopenharmony_ci fn propagate_mut<T1: AsMut<$t>, T2: AsMut<$t>>() { 14444669f6dcSopenharmony_ci check_mut::<Either<T1, T2>>() 14454669f6dcSopenharmony_ci } 14464669f6dcSopenharmony_ci }}; 14474669f6dcSopenharmony_ci} 14484669f6dcSopenharmony_ci 14494669f6dcSopenharmony_ci// This "unused" method is here to ensure that compilation doesn't fail on given types. 14504669f6dcSopenharmony_cifn _unsized_ref_propagation() { 14514669f6dcSopenharmony_ci check_t!(str); 14524669f6dcSopenharmony_ci 14534669f6dcSopenharmony_ci fn check_array_ref<T: AsRef<[Item]>, Item>() {} 14544669f6dcSopenharmony_ci fn check_array_mut<T: AsMut<[Item]>, Item>() {} 14554669f6dcSopenharmony_ci 14564669f6dcSopenharmony_ci fn propagate_array_ref<T1: AsRef<[Item]>, T2: AsRef<[Item]>, Item>() { 14574669f6dcSopenharmony_ci check_array_ref::<Either<T1, T2>, _>() 14584669f6dcSopenharmony_ci } 14594669f6dcSopenharmony_ci 14604669f6dcSopenharmony_ci fn propagate_array_mut<T1: AsMut<[Item]>, T2: AsMut<[Item]>, Item>() { 14614669f6dcSopenharmony_ci check_array_mut::<Either<T1, T2>, _>() 14624669f6dcSopenharmony_ci } 14634669f6dcSopenharmony_ci} 14644669f6dcSopenharmony_ci 14654669f6dcSopenharmony_ci// This "unused" method is here to ensure that compilation doesn't fail on given types. 14664669f6dcSopenharmony_ci#[cfg(feature = "use_std")] 14674669f6dcSopenharmony_cifn _unsized_std_propagation() { 14684669f6dcSopenharmony_ci check_t!(::std::path::Path); 14694669f6dcSopenharmony_ci check_t!(::std::ffi::OsStr); 14704669f6dcSopenharmony_ci check_t!(::std::ffi::CStr); 14714669f6dcSopenharmony_ci} 1472