11use core:: ops:: { Deref , DerefMut } ;
22
3- use crate :: EitherOrBoth :: * ;
4-
53use either:: Either ;
64
75/// Value that either holds a single A or B, or both.
@@ -29,13 +27,13 @@ impl<A, B> EitherOrBoth<A, B> {
2927 /// If `Left`, return true. Otherwise, return false.
3028 /// Exclusive version of [`has_left`](EitherOrBoth::has_left).
3129 pub fn is_left ( & self ) -> bool {
32- matches ! ( self , Left ( _) )
30+ matches ! ( self , EitherOrBoth :: Left ( _) )
3331 }
3432
3533 /// If `Right`, return true. Otherwise, return false.
3634 /// Exclusive version of [`has_right`](EitherOrBoth::has_right).
3735 pub fn is_right ( & self ) -> bool {
38- matches ! ( self , Right ( _) )
36+ matches ! ( self , EitherOrBoth :: Right ( _) )
3937 }
4038
4139 /// If `Both`, return true. Otherwise, return false.
@@ -46,16 +44,16 @@ impl<A, B> EitherOrBoth<A, B> {
4644 /// If `Left`, or `Both`, return `Some` with the left value. Otherwise, return `None`.
4745 pub fn left ( self ) -> Option < A > {
4846 match self {
49- Left ( left) | Both ( left, _) => Some ( left) ,
50- _ => None ,
47+ EitherOrBoth :: Left ( left) | EitherOrBoth :: Both ( left, _) => Some ( left) ,
48+ EitherOrBoth :: Right ( _ ) => None ,
5149 }
5250 }
5351
5452 /// If `Right`, or `Both`, return `Some` with the right value. Otherwise, return `None`.
5553 pub fn right ( self ) -> Option < B > {
5654 match self {
57- Right ( right) | Both ( _, right) => Some ( right) ,
58- _ => None ,
55+ EitherOrBoth :: Right ( right) | EitherOrBoth :: Both ( _, right) => Some ( right) ,
56+ EitherOrBoth :: Left ( _ ) => None ,
5957 }
6058 }
6159
@@ -87,7 +85,7 @@ impl<A, B> EitherOrBoth<A, B> {
8785 /// ```
8886 pub fn just_left ( self ) -> Option < A > {
8987 match self {
90- Left ( left) => Some ( left) ,
88+ EitherOrBoth :: Left ( left) => Some ( left) ,
9189 _ => None ,
9290 }
9391 }
@@ -112,15 +110,15 @@ impl<A, B> EitherOrBoth<A, B> {
112110 /// ```
113111 pub fn just_right ( self ) -> Option < B > {
114112 match self {
115- Right ( right) => Some ( right) ,
113+ EitherOrBoth :: Right ( right) => Some ( right) ,
116114 _ => None ,
117115 }
118116 }
119117
120118 /// If `Both`, return `Some` containing the left and right values. Otherwise, return `None`.
121119 pub fn both ( self ) -> Option < ( A , B ) > {
122120 match self {
123- Both ( a, b) => Some ( ( a, b) ) ,
121+ EitherOrBoth :: Both ( a, b) => Some ( ( a, b) ) ,
124122 _ => None ,
125123 }
126124 }
@@ -131,8 +129,8 @@ impl<A, B> EitherOrBoth<A, B> {
131129 B : Into < A > ,
132130 {
133131 match self {
134- Left ( a) | Both ( a, _) => a,
135- Right ( b) => b. into ( ) ,
132+ EitherOrBoth :: Left ( a) | EitherOrBoth :: Both ( a, _) => a,
133+ EitherOrBoth :: Right ( b) => b. into ( ) ,
136134 }
137135 }
138136
@@ -142,26 +140,26 @@ impl<A, B> EitherOrBoth<A, B> {
142140 A : Into < B > ,
143141 {
144142 match self {
145- Right ( b) | Both ( _, b) => b,
146- Left ( a) => a. into ( ) ,
143+ EitherOrBoth :: Right ( b) | EitherOrBoth :: Both ( _, b) => b,
144+ EitherOrBoth :: Left ( a) => a. into ( ) ,
147145 }
148146 }
149147
150148 /// Converts from `&EitherOrBoth<A, B>` to `EitherOrBoth<&A, &B>`.
151149 pub fn as_ref ( & self ) -> EitherOrBoth < & A , & B > {
152150 match * self {
153- Left ( ref left) => Left ( left) ,
154- Right ( ref right) => Right ( right) ,
155- Both ( ref left, ref right) => Both ( left, right) ,
151+ EitherOrBoth :: Left ( ref left) => EitherOrBoth :: Left ( left) ,
152+ EitherOrBoth :: Right ( ref right) => EitherOrBoth :: Right ( right) ,
153+ EitherOrBoth :: Both ( ref left, ref right) => EitherOrBoth :: Both ( left, right) ,
156154 }
157155 }
158156
159157 /// Converts from `&mut EitherOrBoth<A, B>` to `EitherOrBoth<&mut A, &mut B>`.
160158 pub fn as_mut ( & mut self ) -> EitherOrBoth < & mut A , & mut B > {
161159 match * self {
162- Left ( ref mut left) => Left ( left) ,
163- Right ( ref mut right) => Right ( right) ,
164- Both ( ref mut left, ref mut right) => Both ( left, right) ,
160+ EitherOrBoth :: Left ( ref mut left) => EitherOrBoth :: Left ( left) ,
161+ EitherOrBoth :: Right ( ref mut right) => EitherOrBoth :: Right ( right) ,
162+ EitherOrBoth :: Both ( ref mut left, ref mut right) => EitherOrBoth :: Both ( left, right) ,
165163 }
166164 }
167165
@@ -172,9 +170,9 @@ impl<A, B> EitherOrBoth<A, B> {
172170 B : Deref ,
173171 {
174172 match * self {
175- Left ( ref left) => Left ( left) ,
176- Right ( ref right) => Right ( right) ,
177- Both ( ref left, ref right) => Both ( left, right) ,
173+ EitherOrBoth :: Left ( ref left) => EitherOrBoth :: Left ( left) ,
174+ EitherOrBoth :: Right ( ref right) => EitherOrBoth :: Right ( right) ,
175+ EitherOrBoth :: Both ( ref left, ref right) => EitherOrBoth :: Both ( left, right) ,
178176 }
179177 }
180178
@@ -185,18 +183,18 @@ impl<A, B> EitherOrBoth<A, B> {
185183 B : DerefMut ,
186184 {
187185 match * self {
188- Left ( ref mut left) => Left ( left) ,
189- Right ( ref mut right) => Right ( right) ,
190- Both ( ref mut left, ref mut right) => Both ( left, right) ,
186+ EitherOrBoth :: Left ( ref mut left) => EitherOrBoth :: Left ( left) ,
187+ EitherOrBoth :: Right ( ref mut right) => EitherOrBoth :: Right ( right) ,
188+ EitherOrBoth :: Both ( ref mut left, ref mut right) => EitherOrBoth :: Both ( left, right) ,
191189 }
192190 }
193191
194192 /// Convert `EitherOrBoth<A, B>` to `EitherOrBoth<B, A>`.
195193 pub fn flip ( self ) -> EitherOrBoth < B , A > {
196194 match self {
197- Left ( a) => Right ( a) ,
198- Right ( b) => Left ( b) ,
199- Both ( a, b) => Both ( b, a) ,
195+ EitherOrBoth :: Left ( a) => EitherOrBoth :: Right ( a) ,
196+ EitherOrBoth :: Right ( b) => EitherOrBoth :: Left ( b) ,
197+ EitherOrBoth :: Both ( a, b) => EitherOrBoth :: Both ( b, a) ,
200198 }
201199 }
202200
@@ -207,9 +205,9 @@ impl<A, B> EitherOrBoth<A, B> {
207205 F : FnOnce ( A ) -> M ,
208206 {
209207 match self {
210- Both ( a, b) => Both ( f ( a) , b) ,
211- Left ( a) => Left ( f ( a) ) ,
212- Right ( b) => Right ( b) ,
208+ EitherOrBoth :: Both ( a, b) => EitherOrBoth :: Both ( f ( a) , b) ,
209+ EitherOrBoth :: Left ( a) => EitherOrBoth :: Left ( f ( a) ) ,
210+ EitherOrBoth :: Right ( b) => EitherOrBoth :: Right ( b) ,
213211 }
214212 }
215213
@@ -220,9 +218,9 @@ impl<A, B> EitherOrBoth<A, B> {
220218 F : FnOnce ( B ) -> M ,
221219 {
222220 match self {
223- Left ( a) => Left ( a) ,
224- Right ( b) => Right ( f ( b) ) ,
225- Both ( a, b) => Both ( a, f ( b) ) ,
221+ EitherOrBoth :: Left ( a) => EitherOrBoth :: Left ( a) ,
222+ EitherOrBoth :: Right ( b) => EitherOrBoth :: Right ( f ( b) ) ,
223+ EitherOrBoth :: Both ( a, b) => EitherOrBoth :: Both ( a, f ( b) ) ,
226224 }
227225 }
228226
@@ -235,9 +233,9 @@ impl<A, B> EitherOrBoth<A, B> {
235233 G : FnOnce ( B ) -> R ,
236234 {
237235 match self {
238- Left ( a) => Left ( f ( a) ) ,
239- Right ( b) => Right ( g ( b) ) ,
240- Both ( a, b) => Both ( f ( a) , g ( b) ) ,
236+ EitherOrBoth :: Left ( a) => EitherOrBoth :: Left ( f ( a) ) ,
237+ EitherOrBoth :: Right ( b) => EitherOrBoth :: Right ( g ( b) ) ,
238+ EitherOrBoth :: Both ( a, b) => EitherOrBoth :: Both ( f ( a) , g ( b) ) ,
241239 }
242240 }
243241
@@ -248,8 +246,8 @@ impl<A, B> EitherOrBoth<A, B> {
248246 F : FnOnce ( A ) -> EitherOrBoth < L , B > ,
249247 {
250248 match self {
251- Left ( a) | Both ( a, _) => f ( a) ,
252- Right ( b) => Right ( b) ,
249+ EitherOrBoth :: Left ( a) | EitherOrBoth :: Both ( a, _) => f ( a) ,
250+ EitherOrBoth :: Right ( b) => EitherOrBoth :: Right ( b) ,
253251 }
254252 }
255253
@@ -260,8 +258,8 @@ impl<A, B> EitherOrBoth<A, B> {
260258 F : FnOnce ( B ) -> EitherOrBoth < A , R > ,
261259 {
262260 match self {
263- Left ( a) => Left ( a) ,
264- Right ( b) | Both ( _, b) => f ( b) ,
261+ EitherOrBoth :: Left ( a) => EitherOrBoth :: Left ( a) ,
262+ EitherOrBoth :: Right ( b) | EitherOrBoth :: Both ( _, b) => f ( b) ,
265263 }
266264 }
267265
@@ -286,9 +284,9 @@ impl<A, B> EitherOrBoth<A, B> {
286284 /// ```
287285 pub fn or ( self , l : A , r : B ) -> ( A , B ) {
288286 match self {
289- Left ( inner_l) => ( inner_l, r) ,
290- Right ( inner_r) => ( l, inner_r) ,
291- Both ( inner_l, inner_r) => ( inner_l, inner_r) ,
287+ EitherOrBoth :: Left ( inner_l) => ( inner_l, r) ,
288+ EitherOrBoth :: Right ( inner_r) => ( l, inner_r) ,
289+ EitherOrBoth :: Both ( inner_l, inner_r) => ( inner_l, inner_r) ,
292290 }
293291 }
294292
@@ -323,9 +321,9 @@ impl<A, B> EitherOrBoth<A, B> {
323321 /// ```
324322 pub fn or_else < L : FnOnce ( ) -> A , R : FnOnce ( ) -> B > ( self , l : L , r : R ) -> ( A , B ) {
325323 match self {
326- Left ( inner_l) => ( inner_l, r ( ) ) ,
327- Right ( inner_r) => ( l ( ) , inner_r) ,
328- Both ( inner_l, inner_r) => ( inner_l, inner_r) ,
324+ EitherOrBoth :: Left ( inner_l) => ( inner_l, r ( ) ) ,
325+ EitherOrBoth :: Right ( inner_r) => ( l ( ) , inner_r) ,
326+ EitherOrBoth :: Both ( inner_l, inner_r) => ( inner_l, inner_r) ,
329327 }
330328 }
331329
@@ -348,8 +346,8 @@ impl<A, B> EitherOrBoth<A, B> {
348346 F : FnOnce ( ) -> A ,
349347 {
350348 match self {
351- Left ( left) | Both ( left, _) => left,
352- Right ( _) => self . insert_left ( f ( ) ) ,
349+ EitherOrBoth :: Left ( left) | EitherOrBoth :: Both ( left, _) => left,
350+ EitherOrBoth :: Right ( _) => self . insert_left ( f ( ) ) ,
353351 }
354352 }
355353
@@ -360,8 +358,8 @@ impl<A, B> EitherOrBoth<A, B> {
360358 F : FnOnce ( ) -> B ,
361359 {
362360 match self {
363- Right ( right) | Both ( _, right) => right,
364- Left ( _) => self . insert_right ( f ( ) ) ,
361+ EitherOrBoth :: Right ( right) | EitherOrBoth :: Both ( _, right) => right,
362+ EitherOrBoth :: Left ( _) => self . insert_right ( f ( ) ) ,
365363 }
366364 }
367365
@@ -383,21 +381,21 @@ impl<A, B> EitherOrBoth<A, B> {
383381 /// ```
384382 pub fn insert_left ( & mut self , val : A ) -> & mut A {
385383 match self {
386- Left ( left) | Both ( left, _) => {
384+ EitherOrBoth :: Left ( left) | EitherOrBoth :: Both ( left, _) => {
387385 * left = val;
388386 left
389387 }
390- Right ( right) => {
388+ EitherOrBoth :: Right ( right) => {
391389 // This is like a map in place operation. We move out of the reference,
392390 // change the value, and then move back into the reference.
393391 unsafe {
394392 // SAFETY: We know this pointer is valid for reading since we got it from a reference.
395393 let right = std:: ptr:: read ( right as * mut _ ) ;
396394 // SAFETY: Again, we know the pointer is valid since we got it from a reference.
397- std:: ptr:: write ( self as * mut _ , Both ( val, right) ) ;
395+ std:: ptr:: write ( self as * mut _ , EitherOrBoth :: Both ( val, right) ) ;
398396 }
399397
400- if let Both ( left, _) = self {
398+ if let EitherOrBoth :: Both ( left, _) = self {
401399 left
402400 } else {
403401 // SAFETY: The above pattern will always match, since we just
@@ -425,20 +423,20 @@ impl<A, B> EitherOrBoth<A, B> {
425423 /// ```
426424 pub fn insert_right ( & mut self , val : B ) -> & mut B {
427425 match self {
428- Right ( right) | Both ( _, right) => {
426+ EitherOrBoth :: Right ( right) | EitherOrBoth :: Both ( _, right) => {
429427 * right = val;
430428 right
431429 }
432- Left ( left) => {
430+ EitherOrBoth :: Left ( left) => {
433431 // This is like a map in place operation. We move out of the reference,
434432 // change the value, and then move back into the reference.
435433 unsafe {
436434 // SAFETY: We know this pointer is valid for reading since we got it from a reference.
437435 let left = std:: ptr:: read ( left as * mut _ ) ;
438436 // SAFETY: Again, we know the pointer is valid since we got it from a reference.
439- std:: ptr:: write ( self as * mut _ , Both ( left, val) ) ;
437+ std:: ptr:: write ( self as * mut _ , EitherOrBoth :: Both ( left, val) ) ;
440438 }
441- if let Both ( _, right) = self {
439+ if let EitherOrBoth :: Both ( _, right) = self {
442440 right
443441 } else {
444442 // SAFETY: The above pattern will always match, since we just
@@ -452,8 +450,8 @@ impl<A, B> EitherOrBoth<A, B> {
452450 /// Set `self` to `Both(..)`, containing the specified left and right values,
453451 /// and returns a mutable reference to those values.
454452 pub fn insert_both ( & mut self , left : A , right : B ) -> ( & mut A , & mut B ) {
455- * self = Both ( left, right) ;
456- if let Both ( left, right) = self {
453+ * self = EitherOrBoth :: Both ( left, right) ;
454+ if let EitherOrBoth :: Both ( left, right) = self {
457455 ( left, right)
458456 } else {
459457 // SAFETY: The above pattern will always match, since we just
@@ -487,9 +485,9 @@ impl<T> EitherOrBoth<T, T> {
487485 F : FnOnce ( T , T ) -> T ,
488486 {
489487 match self {
490- Left ( a) => a,
491- Right ( b) => b,
492- Both ( a, b) => f ( a, b) ,
488+ EitherOrBoth :: Left ( a) => a,
489+ EitherOrBoth :: Right ( b) => b,
490+ EitherOrBoth :: Both ( a, b) => f ( a, b) ,
493491 }
494492 }
495493}
@@ -500,7 +498,7 @@ impl<A, B> Into<Option<Either<A, B>>> for EitherOrBoth<A, B> {
500498 match self {
501499 EitherOrBoth :: Left ( l) => Some ( Either :: Left ( l) ) ,
502500 EitherOrBoth :: Right ( r) => Some ( Either :: Right ( r) ) ,
503- _ => None ,
501+ EitherOrBoth :: Both ( .. ) => None ,
504502 }
505503 }
506504}
0 commit comments