Skip to content

Commit 4b15974

Browse files
committed
add as_deref{_mut} methods to EitherOrBoth
1 parent a9fc412 commit 4b15974

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/either_or_both.rs

+28
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use core::ops::{Deref, DerefMut};
2+
13
use crate::EitherOrBoth::*;
24

35
use either::Either;
@@ -161,6 +163,32 @@ impl<A, B> EitherOrBoth<A, B> {
161163
}
162164
}
163165

166+
/// Converts from `&EitherOrBoth<A, B>` to `EitherOrBoth<&_, &_>` using the [`Deref`] trait.
167+
pub fn as_deref(&self) -> EitherOrBoth<&A::Target, &B::Target>
168+
where
169+
A: Deref,
170+
B: Deref,
171+
{
172+
match *self {
173+
Left(ref left) => Left(left),
174+
Right(ref right) => Right(right),
175+
Both(ref left, ref right) => Both(left, right),
176+
}
177+
}
178+
179+
/// Converts from `&mut EitherOrBoth<A, B>` to `EitherOrBoth<&mut _, &mut _>` using the [`DerefMut`] trait.
180+
pub fn as_deref_mut(&mut self) -> EitherOrBoth<&mut A::Target, &mut B::Target>
181+
where
182+
A: DerefMut,
183+
B: DerefMut,
184+
{
185+
match *self {
186+
Left(ref mut left) => Left(left),
187+
Right(ref mut right) => Right(right),
188+
Both(ref mut left, ref mut right) => Both(left, right),
189+
}
190+
}
191+
164192
/// Convert `EitherOrBoth<A, B>` to `EitherOrBoth<B, A>`.
165193
pub fn flip(self) -> EitherOrBoth<B, A> {
166194
match self {

0 commit comments

Comments
 (0)