You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Programmers would rarely implement this directly, but rely on the blanket implementation to kick in when they've implemented From for each arm of an Either on their type.
Why would this be useful? Consider an Iterator of Either values. One may with to transform those values into another enum:
If the arms are the same, we could use Either::into_inner as the function passed to map(). FromEither is useful specifically if the arms are different but still convertible to a common type.
My current workaround is to use map(|ab| ab.either(C::from, C::from)) in situations like this, which works, but is a little more verbose than map(C::from_either).
The text was updated successfully, but these errors were encountered:
I think either_into<T> broadly meets this need -- your example can write .map(Either::either_into::<C>), or drive the type inference for C elsewhere like .map(Either::either_into).for_each(|c: C| ...). It's a little wordy, perhaps, but at least you don't need to import an extra trait to make it work.
There is an
into_inner()
method onEither<T, T>
that can "unwrap" the inner value as long as both arms have the same type.This is useful, but a more generic conversion could be implemented for any
Either<A, B>
to any typeT
that implementsFrom<A> + From<B>
.Such a trait would be simple to blanket implement:
Programmers would rarely implement this directly, but rely on the blanket implementation to kick in when they've implemented
From
for each arm of anEither
on their type.Why would this be useful? Consider an
Iterator
ofEither
values. One may with to transform those values into another enum:If the arms are the same, we could use
Either::into_inner
as the function passed tomap()
.FromEither
is useful specifically if the arms are different but still convertible to a common type.My current workaround is to use
map(|ab| ab.either(C::from, C::from))
in situations like this, which works, but is a little more verbose thanmap(C::from_either)
.The text was updated successfully, but these errors were encountered: