Skip to main content

map_both

Macro map_both 

Source
macro_rules! map_both {
    ($value:expr, $pattern:pat => $result:expr) => { ... };
    ($name:ident => $result:expr) => { ... };
}
Expand description

Evaluate the provided expression for both Either::Left and Either::Right, returning an Either with the results.

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::map_both!( expression , pattern => expression )

Unlike for_both!, this macro returns an Either with the results of the expressions.

§Example

use either::Either;

struct Wrapper<T>(T);

fn wrap(
    owned_or_borrowed: Either<String, &'static str>,
) -> Either<Wrapper<String>, Wrapper<&'static str>> {
    either::map_both!(owned_or_borrowed, s => Wrapper(s))
}
use either::Either;

fn widen(x: Either<i32, u32>) -> Either<i64, u64> {
    either::map_both!(x => x.into())
}