Skip to content

Commit

Permalink
add as_deref{_mut} methods to EitherOrBoth
Browse files Browse the repository at this point in the history
  • Loading branch information
JoJoJet committed Jul 7, 2022
1 parent a9fc412 commit 4b15974
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/either_or_both.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use core::ops::{Deref, DerefMut};

use crate::EitherOrBoth::*;

use either::Either;
Expand Down Expand Up @@ -161,6 +163,32 @@ impl<A, B> EitherOrBoth<A, B> {
}
}

/// Converts from `&EitherOrBoth<A, B>` to `EitherOrBoth<&_, &_>` using the [`Deref`] trait.
pub fn as_deref(&self) -> EitherOrBoth<&A::Target, &B::Target>
where
A: Deref,
B: Deref,
{
match *self {
Left(ref left) => Left(left),
Right(ref right) => Right(right),
Both(ref left, ref right) => Both(left, right),
}
}

/// Converts from `&mut EitherOrBoth<A, B>` to `EitherOrBoth<&mut _, &mut _>` using the [`DerefMut`] trait.
pub fn as_deref_mut(&mut self) -> EitherOrBoth<&mut A::Target, &mut B::Target>
where
A: DerefMut,
B: DerefMut,
{
match *self {
Left(ref mut left) => Left(left),
Right(ref mut right) => Right(right),
Both(ref mut left, ref mut right) => Both(left, right),
}
}

/// Convert `EitherOrBoth<A, B>` to `EitherOrBoth<B, A>`.
pub fn flip(self) -> EitherOrBoth<B, A> {
match self {
Expand Down

0 comments on commit 4b15974

Please sign in to comment.