@@ -87,4 +87,73 @@ impl<A, B> EitherOrBoth<A, B> {
8787 Both ( ref mut left, ref mut right) => Both ( left, right) ,
8888 }
8989 }
90+
91+ /// Convert `EitherOrBoth<A, B>` to `EitherOrBoth<B, A>`.
92+ pub fn flip ( self ) -> EitherOrBoth < B , A > {
93+ match self {
94+ Left ( a) => Right ( a) , Right ( b) => Left ( b) , Both ( a, b) => Both ( b, a) , }
95+ }
96+
97+ /// Apply the function `f` on the value `a` in `Left(a)` or `Both(a, b)` variants. If it is
98+ /// present rewrapping the result in `self`'s original variant.
99+ pub fn map_left < F , M > ( self , f : F ) -> EitherOrBoth < M , B >
100+ where
101+ F : FnOnce ( A ) -> M ,
102+ {
103+ match self {
104+ Both ( a, b) => Both ( f ( a) , b) ,
105+ Left ( a) => Left ( f ( a) ) , Right ( b) => Right ( b) , }
106+ }
107+
108+ /// Apply the function `f` on the value `b` in `Right(b)` or `Both(a, b)` variants.
109+ /// If it is present rewrapping the result in `self`'s original variant.
110+ pub fn map_right < F , M > ( self , f : F ) -> EitherOrBoth < A , M >
111+ where
112+ F : FnOnce ( B ) -> M ,
113+ {
114+ match self {
115+ Left ( a) => Left ( a) ,
116+ Right ( b) => Right ( f ( b) ) ,
117+ Both ( a, b) => Both ( a, f ( b) ) ,
118+ }
119+ }
120+
121+ /// Apply the functions `f` and `g` on the value `a` and `b` respectively;
122+ /// found in `Left(a)`, `Right(b)`, or `Both(a, b)` variants.
123+ /// The Result is rewrapped `self`'s original variant.
124+ pub fn map_any < F , L , G , R > ( self , f : F , g : G ) -> EitherOrBoth < L , R >
125+ where
126+ F : FnOnce ( A ) -> L ,
127+ G : FnOnce ( B ) -> R ,
128+ {
129+ match self {
130+ Left ( a) => Left ( f ( a) ) ,
131+ Right ( b) => Right ( g ( b) ) ,
132+ Both ( a, b) => Both ( f ( a) , g ( b) ) ,
133+ }
134+ }
135+
136+ /// Apply the function `f` on the value `b` in `Right(b)` or `Both(a, _)` variants if it is
137+ /// present.
138+ pub fn left_and_then < F , L > ( self , f : F ) -> EitherOrBoth < L , B >
139+ where
140+ F : FnOnce ( A ) -> EitherOrBoth < L , B > ,
141+ {
142+ match self {
143+ Left ( a) | Both ( a, _) => f ( a) ,
144+ Right ( b) => Right ( b) ,
145+ }
146+ }
147+
148+ /// Apply the function `f` on the value `a`
149+ /// in `Left(a)` or `Both(a, _)` variants if it is present.
150+ pub fn right_and_then < F , R > ( self , f : F ) -> EitherOrBoth < A , R >
151+ where
152+ F : FnOnce ( B ) -> EitherOrBoth < A , R > ,
153+ {
154+ match self {
155+ Left ( a) => Left ( a) ,
156+ Right ( b) | Both ( _, b) => f ( b) ,
157+ }
158+ }
90159}
0 commit comments