//! The enum [`Either`] with variants `Left` and `Right` is a general purpose //! sum type with two cases. //! //! [`Either`]: enum.Either.html //! //! **Crate features:** //! //! * `"use_std"` //! Enabled by default. Disable to make the library `#![no_std]`. //! //! * `"serde"` //! Disabled by default. Enable to `#[derive(Serialize, Deserialize)]` for `Either` //!
use core::convert::{AsMut, AsRef}; use core::fmt; use core::future::Future; use core::iter; use core::ops::Deref; use core::ops::DerefMut; use core::pin::Pin;
#[cfg(any(test, feature = "use_std"))] use std::error::Error; #[cfg(any(test, feature = "use_std"))] use std::io::{self, BufRead, Read, Seek, SeekFrom, Write};
pubusecrate::Either::{Left, Right};
/// The enum `Either` with variants `Left` and `Right` is a general purpose /// sum type with two cases. /// /// The `Either` type is symmetric and treats its variants the same way, without /// preference. /// (For representing success or error, use the regular `Result` enum instead.) #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] pubenum Either<L, R> { /// A value of type `L`.
Left(L), /// A value of type `R`.
Right(R),
}
/// Evaluate the provided expression for both [`Either::Left`] and [`Either::Right`]. /// /// This macro is useful in cases where both sides of [`Either`] can be interacted with /// in the same way even though the don't share the same type. /// /// Syntax: `either::for_both!(` *expression* `,` *pattern* `=>` *expression* `)` /// /// # Example /// /// ``` /// use either::Either; /// /// fn length(owned_or_borrowed: Either<String, &'static str>) -> usize { /// either::for_both!(owned_or_borrowed, s => s.len()) /// } /// /// fn main() { /// let borrowed = Either::Right("Hello world!"); /// let owned = Either::Left("Hello world!".to_owned()); /// /// assert_eq!(length(borrowed), 12); /// assert_eq!(length(owned), 12); /// } /// ``` #[macro_export]
macro_rules! for_both {
($value:expr, $pattern:pat => $result:expr) => { match $value {
$crate::Either::Left($pattern) => $result,
$crate::Either::Right($pattern) => $result,
}
};
}
/// Macro for unwrapping the left side of an `Either`, which fails early /// with the opposite side. Can only be used in functions that return /// `Either` because of the early return of `Right` that it provides. /// /// See also `try_right!` for its dual, which applies the same just to the /// right side. /// /// # Example /// /// ``` /// use either::{Either, Left, Right}; /// /// fn twice(wrapper: Either<u32, &str>) -> Either<u32, &str> { /// let value = either::try_left!(wrapper); /// Left(value * 2) /// } /// /// fn main() { /// assert_eq!(twice(Left(2)), Left(4)); /// assert_eq!(twice(Right("ups")), Right("ups")); /// } /// ``` #[macro_export]
macro_rules! try_left {
($expr:expr) => { match $expr {
$crate::Left(val) => val,
$crate::Right(err) => return $crate::Right(::core::convert::From::from(err)),
}
};
}
/// Dual to `try_left!`, see its documentation for more information. #[macro_export]
macro_rules! try_right {
($expr:expr) => { match $expr {
$crate::Left(err) => return $crate::Left(::core::convert::From::from(err)),
$crate::Right(val) => val,
}
};
}
impl<L, R> Either<L, R> { /// Return true if the value is the `Left` variant. /// /// ``` /// use either::*; /// /// let values = [Left(1), Right("the right value")]; /// assert_eq!(values[0].is_left(), true); /// assert_eq!(values[1].is_left(), false); /// ``` pubfn is_left(&self) -> bool { match *self {
Left(_) => true,
Right(_) => false,
}
}
/// Return true if the value is the `Right` variant. /// /// ``` /// use either::*; /// /// let values = [Left(1), Right("the right value")]; /// assert_eq!(values[0].is_right(), false); /// assert_eq!(values[1].is_right(), true); /// ``` pubfn is_right(&self) -> bool {
!self.is_left()
}
/// Convert the left side of `Either<L, R>` to an `Option<L>`. /// /// ``` /// use either::*; /// /// let left: Either<_, ()> = Left("some value"); /// assert_eq!(left.left(), Some("some value")); /// /// let right: Either<(), _> = Right(321); /// assert_eq!(right.left(), None); /// ``` pubfn left(self) -> Option<L> { matchself {
Left(l) => Some(l),
Right(_) => None,
}
}
/// Convert the right side of `Either<L, R>` to an `Option<R>`. /// /// ``` /// use either::*; /// /// let left: Either<_, ()> = Left("some value"); /// assert_eq!(left.right(), None); /// /// let right: Either<(), _> = Right(321); /// assert_eq!(right.right(), Some(321)); /// ``` pubfn right(self) -> Option<R> { matchself {
Left(_) => None,
Right(r) => Some(r),
}
}
/// Convert `&mut Either<L, R>` to `Either<&mut L, &mut R>`. /// /// ``` /// use either::*; /// /// fn mutate_left(value: &mut Either<u32, u32>) { /// if let Some(l) = value.as_mut().left() { /// *l = 999; /// } /// } /// /// let mut left = Left(123); /// let mut right = Right(123); /// mutate_left(&mut left); /// mutate_left(&mut right); /// assert_eq!(left, Left(999)); /// assert_eq!(right, Right(123)); /// ``` pubfn as_mut(&mutself) -> Either<&mut L, &an style='color:red'>mut R> { match *self {
Left(refmut inner) => Left(inner),
Right(refmut inner) => Right(inner),
}
}
/// Convert `Pin<&Either<L, R>>` to `Either<Pin<&L>, Pin<&R>>`, /// pinned projections of the inner variants. pubfn as_pin_ref(self: Pin<&Self>) -> Either<Pin<&L>, Pin<&R>> { // SAFETY: We can use `new_unchecked` because the `inner` parts are // guaranteed to be pinned, as they come from `self` which is pinned. unsafe { match *Pin::get_ref(self) {
Left(ref inner) => Left(Pin::new_unchecked(inner)),
Right(ref inner) => Right(Pin::new_unchecked(inner)),
}
}
}
/// Convert `Pin<&mut Either<L, R>>` to `Either<Pin<&mut L>, Pin<&mut R>>`, /// pinned projections of the inner variants. pubfn as_pin_mut(self: Pin<&mutSelf>) -> Either<Pin<&mut L>, Pin<&mut R>> { // SAFETY: `get_unchecked_mut` is fine because we don't move anything. // We can use `new_unchecked` because the `inner` parts are guaranteed // to be pinned, as they come from `self` which is pinned, and we never // offer an unpinned `&mut L` or `&mut R` through `Pin<&mut Self>`. We // also don't have an implementation of `Drop`, nor manual `Unpin`. unsafe { match *Pin::get_unchecked_mut(self) {
Left(refmut inner) => Left(Pin::new_unchecked(inner)),
Right(refmut inner) => Right(Pin::new_unchecked(inner)),
}
}
}
/// Apply the function `f` on the value in the `Left` variant if it is present rewrapping the /// result in `Left`. /// /// ``` /// use either::*; /// /// let left: Either<_, u32> = Left(123); /// assert_eq!(left.map_left(|x| x * 2), Left(246)); /// /// let right: Either<u32, _> = Right(123); /// assert_eq!(right.map_left(|x| x * 2), Right(123)); /// ``` pubfn map_left<F, M>(self, f: F) -> Either<M, R> where
F: FnOnce(L) -> M,
{ matchself {
Left(l) => Left(f(l)),
Right(r) => Right(r),
}
}
/// Apply the function `f` on the value in the `Right` variant if it is present rewrapping the /// result in `Right`. /// /// ``` /// use either::*; /// /// let left: Either<_, u32> = Left(123); /// assert_eq!(left.map_right(|x| x * 2), Left(123)); /// /// let right: Either<u32, _> = Right(123); /// assert_eq!(right.map_right(|x| x * 2), Right(246)); /// ``` pubfn map_right<F, S>(self, f: F) -> Either<L, S> where
F: FnOnce(R) -> S,
{ matchself {
Left(l) => Left(l),
Right(r) => Right(f(r)),
}
}
/// Apply one of two functions depending on contents, unifying their result. If the value is /// `Left(L)` then the first function `f` is applied; if it is `Right(R)` then the second /// function `g` is applied. /// /// ``` /// use either::*; /// /// fn square(n: u32) -> i32 { (n * n) as i32 } /// fn negate(n: i32) -> i32 { -n } /// /// let left: Either<u32, i32> = Left(4); /// assert_eq!(left.either(square, negate), 16); /// /// let right: Either<u32, i32> = Right(-4); /// assert_eq!(right.either(square, negate), 4); /// ``` pubfn either<F, G, T>(self, f: F, g: G) -> T where
F: FnOnce(L) -> T,
G: FnOnce(R) -> T,
{ matchself {
Left(l) => f(l),
Right(r) => g(r),
}
}
/// Like `either`, but provide some context to whichever of the /// functions ends up being called. /// /// ``` /// // In this example, the context is a mutable reference /// use either::*; /// /// let mut result = Vec::new(); /// /// let values = vec![Left(2), Right(2.7)]; /// /// for value in values { /// value.either_with(&mut result, /// |ctx, integer| ctx.push(integer), /// |ctx, real| ctx.push(f64::round(real) as i32)); /// } /// /// assert_eq!(result, vec![2, 3]); /// ``` pubfn either_with<Ctx, F, G, T>(self, ctx: Ctx, f: F, g: G) -> T where
F: FnOnce(Ctx, L) -> T,
G: FnOnce(Ctx, R) -> T,
{ matchself {
Left(l) => f(ctx, l),
Right(r) => g(ctx, r),
}
}
/// Apply the function `f` on the value in the `Left` variant if it is present. /// /// ``` /// use either::*; /// /// let left: Either<_, u32> = Left(123); /// assert_eq!(left.left_and_then::<_,()>(|x| Right(x * 2)), Right(246)); /// /// let right: Either<u32, _> = Right(123); /// assert_eq!(right.left_and_then(|x| Right::<(), _>(x * 2)), Right(123)); /// ``` pubfn left_and_then<F, S>(self, f: F) -> Either<S, R> where
F: FnOnce(L) -> Either<S, R>,
{ matchself {
Left(l) => f(l),
Right(r) => Right(r),
}
}
/// Apply the function `f` on the value in the `Right` variant if it is present. /// /// ``` /// use either::*; /// /// let left: Either<_, u32> = Left(123); /// assert_eq!(left.right_and_then(|x| Right(x * 2)), Left(123)); /// /// let right: Either<u32, _> = Right(123); /// assert_eq!(right.right_and_then(|x| Right(x * 2)), Right(246)); /// ``` pubfn right_and_then<F, S>(self, f: F) -> Either<L, S> where
F: FnOnce(R) -> Either<L, S>,
{ matchself {
Left(l) => Left(l),
Right(r) => f(r),
}
}
/// Convert the inner value to an iterator. /// /// ``` /// use either::*; /// /// let left: Either<_, Vec<u32>> = Left(vec![1, 2, 3, 4, 5]); /// let mut right: Either<Vec<u32>, _> = Right(vec![]); /// right.extend(left.into_iter()); /// assert_eq!(right, Right(vec![1, 2, 3, 4, 5])); /// ``` #[allow(clippy::should_implement_trait)] pubfn into_iter(self) -> Either<L::IntoIter, R::IntoIter> where
L: IntoIterator,
R: IntoIterator<Item = L::Item>,
{ matchself {
Left(l) => Left(l.into_iter()),
Right(r) => Right(r.into_iter()),
}
}
/// Return left value or given value /// /// Arguments passed to `left_or` are eagerly evaluated; if you are passing /// the result of a function call, it is recommended to use [`left_or_else`], /// which is lazily evaluated. /// /// [`left_or_else`]: #method.left_or_else /// /// # Examples /// /// ``` /// # use either::*; /// let left: Either<&str, &str> = Left("left"); /// assert_eq!(left.left_or("foo"), "left"); /// /// let right: Either<&str, &str> = Right("right"); /// assert_eq!(right.left_or("left"), "left"); /// ``` pubfn left_or(self, other: L) -> L { matchself {
Either::Left(l) => l,
Either::Right(_) => other,
}
}
/// Return left or a default /// /// # Examples /// /// ``` /// # use either::*; /// let left: Either<String, u32> = Left("left".to_string()); /// assert_eq!(left.left_or_default(), "left"); /// /// let right: Either<String, u32> = Right(42); /// assert_eq!(right.left_or_default(), String::default()); /// ``` pubfn left_or_default(self) -> L where
L: Default,
{ matchself {
Either::Left(l) => l,
Either::Right(_) => L::default(),
}
}
/// Returns left value or computes it from a closure /// /// # Examples /// /// ``` /// # use either::*; /// let left: Either<String, u32> = Left("3".to_string()); /// assert_eq!(left.left_or_else(|_| unreachable!()), "3"); /// /// let right: Either<String, u32> = Right(3); /// assert_eq!(right.left_or_else(|x| x.to_string()), "3"); /// ``` pubfn left_or_else<F>(self, f: F) -> L where
F: FnOnce(R) -> L,
{ matchself {
Either::Left(l) => l,
Either::Right(r) => f(r),
}
}
/// Return right value or given value /// /// Arguments passed to `right_or` are eagerly evaluated; if you are passing /// the result of a function call, it is recommended to use [`right_or_else`], /// which is lazily evaluated. /// /// [`right_or_else`]: #method.right_or_else /// /// # Examples /// /// ``` /// # use either::*; /// let right: Either<&str, &str> = Right("right"); /// assert_eq!(right.right_or("foo"), "right"); /// /// let left: Either<&str, &str> = Left("left"); /// assert_eq!(left.right_or("right"), "right"); /// ``` pubfn right_or(self, other: R) -> R { matchself {
Either::Left(_) => other,
Either::Right(r) => r,
}
}
/// Return right or a default /// /// # Examples /// /// ``` /// # use either::*; /// let left: Either<String, u32> = Left("left".to_string()); /// assert_eq!(left.right_or_default(), u32::default()); /// /// let right: Either<String, u32> = Right(42); /// assert_eq!(right.right_or_default(), 42); /// ``` pubfn right_or_default(self) -> R where
R: Default,
{ matchself {
Either::Left(_) => R::default(),
Either::Right(r) => r,
}
}
/// Returns right value or computes it from a closure /// /// # Examples /// /// ``` /// # use either::*; /// let left: Either<String, u32> = Left("3".to_string()); /// assert_eq!(left.right_or_else(|x| x.parse().unwrap()), 3); /// /// let right: Either<String, u32> = Right(3); /// assert_eq!(right.right_or_else(|_| unreachable!()), 3); /// ``` pubfn right_or_else<F>(self, f: F) -> R where
F: FnOnce(L) -> R,
{ matchself {
Either::Left(l) => f(l),
Either::Right(r) => r,
}
}
/// Returns the left value /// /// # Examples /// /// ``` /// # use either::*; /// let left: Either<_, ()> = Left(3); /// assert_eq!(left.unwrap_left(), 3); /// ``` /// /// # Panics /// /// When `Either` is a `Right` value /// /// ```should_panic /// # use either::*; /// let right: Either<(), _> = Right(3); /// right.unwrap_left(); /// ``` pubfn unwrap_left(self) -> L where
R: core::fmt::Debug,
{ matchself {
Either::Left(l) => l,
Either::Right(r) => {
panic!("called `Either::unwrap_left()` on a `Right` value: {:?}", r)
}
}
}
/// Returns the right value /// /// # Examples /// /// ``` /// # use either::*; /// let right: Either<(), _> = Right(3); /// assert_eq!(right.unwrap_right(), 3); /// ``` /// /// # Panics /// /// When `Either` is a `Left` value /// /// ```should_panic /// # use either::*; /// let left: Either<_, ()> = Left(3); /// left.unwrap_right(); /// ``` pubfn unwrap_right(self) -> R where
L: core::fmt::Debug,
{ matchself {
Either::Right(r) => r,
Either::Left(l) => panic!("called `Either::unwrap_right()` on a `Left` value: {:?}", l),
}
}
/// Returns the left value /// /// # Examples /// /// ``` /// # use either::*; /// let left: Either<_, ()> = Left(3); /// assert_eq!(left.expect_left("value was Right"), 3); /// ``` /// /// # Panics /// /// When `Either` is a `Right` value /// /// ```should_panic /// # use either::*; /// let right: Either<(), _> = Right(3); /// right.expect_left("value was Right"); /// ``` pubfn expect_left(self, msg: &str) -> L where
R: core::fmt::Debug,
{ matchself {
Either::Left(l) => l,
Either::Right(r) => panic!("{}: {:?}", msg, r),
}
}
/// Returns the right value /// /// # Examples /// /// ``` /// # use either::*; /// let right: Either<(), _> = Right(3); /// assert_eq!(right.expect_right("value was Left"), 3); /// ``` /// /// # Panics /// /// When `Either` is a `Left` value /// /// ```should_panic /// # use either::*; /// let left: Either<_, ()> = Left(3); /// left.expect_right("value was Right"); /// ``` pubfn expect_right(self, msg: &str) -> R where
L: core::fmt::Debug,
{ matchself {
Either::Right(r) => r,
Either::Left(l) => panic!("{}: {:?}", msg, l),
}
}
/// Convert the contained value into `T` /// /// # Examples /// /// ``` /// # use either::*; /// // Both u16 and u32 can be converted to u64. /// let left: Either<u16, u32> = Left(3u16); /// assert_eq!(left.either_into::<u64>(), 3u64); /// let right: Either<u16, u32> = Right(7u32); /// assert_eq!(right.either_into::<u64>(), 7u64); /// ``` pubfn either_into<T>(self) -> T where
L: Into<T>,
R: Into<T>,
{ matchself {
Either::Left(l) => l.into(),
Either::Right(r) => r.into(),
}
}
}
impl<L, R> Either<Option<L>, Option<R>> { /// Factors out `None` from an `Either` of [`Option`]. /// /// ``` /// use either::*; /// let left: Either<_, Option<String>> = Left(Some(vec![0])); /// assert_eq!(left.factor_none(), Some(Left(vec![0]))); /// /// let right: Either<Option<Vec<u8>>, _> = Right(Some(String::new())); /// assert_eq!(right.factor_none(), Some(Right(String::new()))); /// ``` // TODO(MSRV): doc(alias) was stabilized in Rust 1.48 // #[doc(alias = "transpose")] pubfn factor_none(self) -> Option<Either<L, R>> { matchself {
Left(l) => l.map(Either::Left),
Right(r) => r.map(Either::Right),
}
}
}
impl<L, R, E> Either<Result<L, E>, Result<R, E>> { /// Factors out a homogenous type from an `Either` of [`Result`]. /// /// Here, the homogeneous type is the `Err` type of the [`Result`]. /// /// ``` /// use either::*; /// let left: Either<_, Result<String, u32>> = Left(Ok(vec![0])); /// assert_eq!(left.factor_err(), Ok(Left(vec![0]))); /// /// let right: Either<Result<Vec<u8>, u32>, _> = Right(Ok(String::new())); /// assert_eq!(right.factor_err(), Ok(Right(String::new()))); /// ``` // TODO(MSRV): doc(alias) was stabilized in Rust 1.48 // #[doc(alias = "transpose")] pubfn factor_err(self) -> Result<Either<L, R>, E> { matchself {
Left(l) => l.map(Either::Left),
Right(r) => r.map(Either::Right),
}
}
}
impl<T, L, R> Either<Result<T, L>, Result<T, R>> { /// Factors out a homogenous type from an `Either` of [`Result`]. /// /// Here, the homogeneous type is the `Ok` type of the [`Result`]. /// /// ``` /// use either::*; /// let left: Either<_, Result<u32, String>> = Left(Err(vec![0])); /// assert_eq!(left.factor_ok(), Err(Left(vec![0]))); /// /// let right: Either<Result<u32, Vec<u8>>, _> = Right(Err(String::new())); /// assert_eq!(right.factor_ok(), Err(Right(String::new()))); /// ``` // TODO(MSRV): doc(alias) was stabilized in Rust 1.48 // #[doc(alias = "transpose")] pubfn factor_ok(self) -> Result<T, Either<L, R>> { matchself {
Left(l) => l.map_err(Either::Left),
Right(r) => r.map_err(Either::Right),
}
}
}
impl<T, L, R> Either<(T, L), (T, R)> { /// Factor out a homogeneous type from an either of pairs. /// /// Here, the homogeneous type is the first element of the pairs. /// /// ``` /// use either::*; /// let left: Either<_, (u32, String)> = Left((123, vec![0])); /// assert_eq!(left.factor_first().0, 123); /// /// let right: Either<(u32, Vec<u8>), _> = Right((123, String::new())); /// assert_eq!(right.factor_first().0, 123); /// ``` pubfn factor_first(self) -> (T, Either<L, R>) { matchself {
Left((t, l)) => (t, Left(l)),
Right((t, r)) => (t, Right(r)),
}
}
}
impl<T, L, R> Either<(L, T), (R, T)> { /// Factor out a homogeneous type from an either of pairs. /// /// Here, the homogeneous type is the second element of the pairs. /// /// ``` /// use either::*; /// let left: Either<_, (String, u32)> = Left((vec![0], 123)); /// assert_eq!(left.factor_second().1, 123); /// /// let right: Either<(Vec<u8>, u32), _> = Right((String::new(), 123)); /// assert_eq!(right.factor_second().1, 123); /// ``` pubfn factor_second(self) -> (Either<L, R>, T) { matchself {
Left((l, t)) => (Left(l), t),
Right((r, t)) => (Right(r), t),
}
}
}
impl<T> Either<T, T> { /// Extract the value of an either over two equivalent types. /// /// ``` /// use either::*; /// /// let left: Either<_, u32> = Left(123); /// assert_eq!(left.into_inner(), 123); /// /// let right: Either<u32, _> = Right(123); /// assert_eq!(right.into_inner(), 123); /// ``` pubfn into_inner(self) -> T {
for_both!(self, inner => inner)
}
/// Map `f` over the contained value and return the result in the /// corresponding variant. /// /// ``` /// use either::*; /// /// let value: Either<_, i32> = Right(42); /// /// let other = value.map(|x| x * 2); /// assert_eq!(other, Right(84)); /// ``` pubfn map<F, M>(self, f: F) -> Either<M, M> where
F: FnOnce(T) -> M,
{ matchself {
Left(l) => Left(f(l)),
Right(r) => Right(f(r)),
}
}
}
/// Convert from `Result` to `Either` with `Ok => Right` and `Err => Left`. impl<L, R> From<Result<R, L>> for Either<L, R> { fn from(r: Result<R, L>) -> Self { match r {
Err(e) => Left(e),
Ok(o) => Right(o),
}
}
}
/// Convert from `Either` to `Result` with `Right => Ok` and `Left => Err`. #[allow(clippy::from_over_into)] // From requires RFC 2451, Rust 1.41 impl<L, R> Into<Result<R, L>> for Either<L, R> { fn into(self) -> Result<R, L> { matchself {
Left(l) => Err(l),
Right(r) => Ok(r),
}
}
}
impl<L, R, A> Extend<A> for Either<L, R> where
L: Extend<A>,
R: Extend<A>,
{ fn extend<T>(&mutself, iter: T) where
T: IntoIterator<Item = A>,
{
for_both!(*self, refmut inner => inner.extend(iter))
}
}
/// `Either<L, R>` is an iterator if both `L` and `R` are iterators. impl<L, R> Iterator for Either<L, R> where
L: Iterator,
R: Iterator<Item = L::Item>,
{ type Item = L::Item;
impl<L, R> iter::FusedIterator for Either<L, R> where
L: iter::FusedIterator,
R: iter::FusedIterator<Item = L::Item>,
{
}
/// `Either<L, R>` is a future if both `L` and `R` are futures. impl<L, R> Future for Either<L, R> where
L: Future,
R: Future<Output = L::Output>,
{ type Output = L::Output;
// the first read should advance the cursor and return the next 16 bytes thus the `ne`
assert_eq!(reader.read(&mut buf).unwrap(), buf.len());
assert_ne!(buf, mockdata[..buf.len()]);
// if the seek operation fails it should read 16..31 instead of 0..15
reader.seek(io::SeekFrom::Start(0)).unwrap();
assert_eq!(reader.read(&mut buf).unwrap(), buf.len());
assert_eq!(buf, mockdata[..buf.len()]);
}
#[test] fn read_write() { use std::io;
let use_stdio = false; let mockdata = [0xff; 256];
let buf = [1u8; 16];
assert_eq!(writer.write(&buf).unwrap(), buf.len());
}
#[test] #[allow(deprecated)] fn error() { let invalid_utf8 = b"\xff"; let res = iflet Err(error) = ::std::str::from_utf8(invalid_utf8) {
Err(Left(error))
} elseiflet Err(error) = "x".parse::<i32>() {
Err(Right(error))
} else {
Ok(())
};
assert!(res.is_err());
res.unwrap_err().description(); // make sure this can be called
}
/// A helper macro to check if AsRef and AsMut are implemented for a given type.
macro_rules! check_t {
($t:ty) => {{ fn check_ref<T: AsRef<$t>>() {} fn propagate_ref<T1: AsRef<$t>, T2: AsRef<$t>>() {
check_ref::<Either<T1, T2>>()
} fn check_mut<T: AsMut<$t>>() {} fn propagate_mut<T1: AsMut<$t>, T2: AsMut<$t>>() {
check_mut::<Either<T1, T2>>()
}
}};
}
// This "unused" method is here to ensure that compilation doesn't fail on given types. fn _unsized_ref_propagation() {
check_t!(str);
// This "unused" method is here to ensure that compilation doesn't fail on given types. #[cfg(feature = "use_std")] fn _unsized_std_propagation() {
check_t!(::std::path::Path);
check_t!(::std::ffi::OsStr);
check_t!(::std::ffi::CStr);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.