1
1
use core:: ops:: { Deref , DerefMut } ;
2
2
3
- use crate :: EitherOrBoth :: * ;
4
-
5
3
use either:: Either ;
6
4
7
5
/// Value that either holds a single A or B, or both.
@@ -29,13 +27,13 @@ impl<A, B> EitherOrBoth<A, B> {
29
27
/// If `Left`, return true. Otherwise, return false.
30
28
/// Exclusive version of [`has_left`](EitherOrBoth::has_left).
31
29
pub fn is_left ( & self ) -> bool {
32
- matches ! ( self , Left ( _) )
30
+ matches ! ( self , EitherOrBoth :: Left ( _) )
33
31
}
34
32
35
33
/// If `Right`, return true. Otherwise, return false.
36
34
/// Exclusive version of [`has_right`](EitherOrBoth::has_right).
37
35
pub fn is_right ( & self ) -> bool {
38
- matches ! ( self , Right ( _) )
36
+ matches ! ( self , EitherOrBoth :: Right ( _) )
39
37
}
40
38
41
39
/// If `Both`, return true. Otherwise, return false.
@@ -46,16 +44,16 @@ impl<A, B> EitherOrBoth<A, B> {
46
44
/// If `Left`, or `Both`, return `Some` with the left value. Otherwise, return `None`.
47
45
pub fn left ( self ) -> Option < A > {
48
46
match self {
49
- Left ( left) | Both ( left, _) => Some ( left) ,
50
- _ => None ,
47
+ EitherOrBoth :: Left ( left) | EitherOrBoth :: Both ( left, _) => Some ( left) ,
48
+ EitherOrBoth :: Right ( _ ) => None ,
51
49
}
52
50
}
53
51
54
52
/// If `Right`, or `Both`, return `Some` with the right value. Otherwise, return `None`.
55
53
pub fn right ( self ) -> Option < B > {
56
54
match self {
57
- Right ( right) | Both ( _, right) => Some ( right) ,
58
- _ => None ,
55
+ EitherOrBoth :: Right ( right) | EitherOrBoth :: Both ( _, right) => Some ( right) ,
56
+ EitherOrBoth :: Left ( _ ) => None ,
59
57
}
60
58
}
61
59
@@ -87,7 +85,7 @@ impl<A, B> EitherOrBoth<A, B> {
87
85
/// ```
88
86
pub fn just_left ( self ) -> Option < A > {
89
87
match self {
90
- Left ( left) => Some ( left) ,
88
+ EitherOrBoth :: Left ( left) => Some ( left) ,
91
89
_ => None ,
92
90
}
93
91
}
@@ -112,15 +110,15 @@ impl<A, B> EitherOrBoth<A, B> {
112
110
/// ```
113
111
pub fn just_right ( self ) -> Option < B > {
114
112
match self {
115
- Right ( right) => Some ( right) ,
113
+ EitherOrBoth :: Right ( right) => Some ( right) ,
116
114
_ => None ,
117
115
}
118
116
}
119
117
120
118
/// If `Both`, return `Some` containing the left and right values. Otherwise, return `None`.
121
119
pub fn both ( self ) -> Option < ( A , B ) > {
122
120
match self {
123
- Both ( a, b) => Some ( ( a, b) ) ,
121
+ EitherOrBoth :: Both ( a, b) => Some ( ( a, b) ) ,
124
122
_ => None ,
125
123
}
126
124
}
@@ -131,8 +129,8 @@ impl<A, B> EitherOrBoth<A, B> {
131
129
B : Into < A > ,
132
130
{
133
131
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 ( ) ,
136
134
}
137
135
}
138
136
@@ -142,26 +140,26 @@ impl<A, B> EitherOrBoth<A, B> {
142
140
A : Into < B > ,
143
141
{
144
142
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 ( ) ,
147
145
}
148
146
}
149
147
150
148
/// Converts from `&EitherOrBoth<A, B>` to `EitherOrBoth<&A, &B>`.
151
149
pub fn as_ref ( & self ) -> EitherOrBoth < & A , & B > {
152
150
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) ,
156
154
}
157
155
}
158
156
159
157
/// Converts from `&mut EitherOrBoth<A, B>` to `EitherOrBoth<&mut A, &mut B>`.
160
158
pub fn as_mut ( & mut self ) -> EitherOrBoth < & mut A , & mut B > {
161
159
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) ,
165
163
}
166
164
}
167
165
@@ -172,9 +170,9 @@ impl<A, B> EitherOrBoth<A, B> {
172
170
B : Deref ,
173
171
{
174
172
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) ,
178
176
}
179
177
}
180
178
@@ -185,18 +183,18 @@ impl<A, B> EitherOrBoth<A, B> {
185
183
B : DerefMut ,
186
184
{
187
185
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) ,
191
189
}
192
190
}
193
191
194
192
/// Convert `EitherOrBoth<A, B>` to `EitherOrBoth<B, A>`.
195
193
pub fn flip ( self ) -> EitherOrBoth < B , A > {
196
194
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) ,
200
198
}
201
199
}
202
200
@@ -207,9 +205,9 @@ impl<A, B> EitherOrBoth<A, B> {
207
205
F : FnOnce ( A ) -> M ,
208
206
{
209
207
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) ,
213
211
}
214
212
}
215
213
@@ -220,9 +218,9 @@ impl<A, B> EitherOrBoth<A, B> {
220
218
F : FnOnce ( B ) -> M ,
221
219
{
222
220
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) ) ,
226
224
}
227
225
}
228
226
@@ -235,9 +233,9 @@ impl<A, B> EitherOrBoth<A, B> {
235
233
G : FnOnce ( B ) -> R ,
236
234
{
237
235
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) ) ,
241
239
}
242
240
}
243
241
@@ -248,8 +246,8 @@ impl<A, B> EitherOrBoth<A, B> {
248
246
F : FnOnce ( A ) -> EitherOrBoth < L , B > ,
249
247
{
250
248
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) ,
253
251
}
254
252
}
255
253
@@ -260,8 +258,8 @@ impl<A, B> EitherOrBoth<A, B> {
260
258
F : FnOnce ( B ) -> EitherOrBoth < A , R > ,
261
259
{
262
260
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) ,
265
263
}
266
264
}
267
265
@@ -286,9 +284,9 @@ impl<A, B> EitherOrBoth<A, B> {
286
284
/// ```
287
285
pub fn or ( self , l : A , r : B ) -> ( A , B ) {
288
286
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) ,
292
290
}
293
291
}
294
292
@@ -323,9 +321,9 @@ impl<A, B> EitherOrBoth<A, B> {
323
321
/// ```
324
322
pub fn or_else < L : FnOnce ( ) -> A , R : FnOnce ( ) -> B > ( self , l : L , r : R ) -> ( A , B ) {
325
323
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) ,
329
327
}
330
328
}
331
329
@@ -348,8 +346,8 @@ impl<A, B> EitherOrBoth<A, B> {
348
346
F : FnOnce ( ) -> A ,
349
347
{
350
348
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 ( ) ) ,
353
351
}
354
352
}
355
353
@@ -360,8 +358,8 @@ impl<A, B> EitherOrBoth<A, B> {
360
358
F : FnOnce ( ) -> B ,
361
359
{
362
360
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 ( ) ) ,
365
363
}
366
364
}
367
365
@@ -383,21 +381,21 @@ impl<A, B> EitherOrBoth<A, B> {
383
381
/// ```
384
382
pub fn insert_left ( & mut self , val : A ) -> & mut A {
385
383
match self {
386
- Left ( left) | Both ( left, _) => {
384
+ EitherOrBoth :: Left ( left) | EitherOrBoth :: Both ( left, _) => {
387
385
* left = val;
388
386
left
389
387
}
390
- Right ( right) => {
388
+ EitherOrBoth :: Right ( right) => {
391
389
// This is like a map in place operation. We move out of the reference,
392
390
// change the value, and then move back into the reference.
393
391
unsafe {
394
392
// SAFETY: We know this pointer is valid for reading since we got it from a reference.
395
393
let right = std:: ptr:: read ( right as * mut _ ) ;
396
394
// 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) ) ;
398
396
}
399
397
400
- if let Both ( left, _) = self {
398
+ if let EitherOrBoth :: Both ( left, _) = self {
401
399
left
402
400
} else {
403
401
// SAFETY: The above pattern will always match, since we just
@@ -425,20 +423,20 @@ impl<A, B> EitherOrBoth<A, B> {
425
423
/// ```
426
424
pub fn insert_right ( & mut self , val : B ) -> & mut B {
427
425
match self {
428
- Right ( right) | Both ( _, right) => {
426
+ EitherOrBoth :: Right ( right) | EitherOrBoth :: Both ( _, right) => {
429
427
* right = val;
430
428
right
431
429
}
432
- Left ( left) => {
430
+ EitherOrBoth :: Left ( left) => {
433
431
// This is like a map in place operation. We move out of the reference,
434
432
// change the value, and then move back into the reference.
435
433
unsafe {
436
434
// SAFETY: We know this pointer is valid for reading since we got it from a reference.
437
435
let left = std:: ptr:: read ( left as * mut _ ) ;
438
436
// 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) ) ;
440
438
}
441
- if let Both ( _, right) = self {
439
+ if let EitherOrBoth :: Both ( _, right) = self {
442
440
right
443
441
} else {
444
442
// SAFETY: The above pattern will always match, since we just
@@ -452,8 +450,8 @@ impl<A, B> EitherOrBoth<A, B> {
452
450
/// Set `self` to `Both(..)`, containing the specified left and right values,
453
451
/// and returns a mutable reference to those values.
454
452
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 {
457
455
( left, right)
458
456
} else {
459
457
// SAFETY: The above pattern will always match, since we just
@@ -487,9 +485,9 @@ impl<T> EitherOrBoth<T, T> {
487
485
F : FnOnce ( T , T ) -> T ,
488
486
{
489
487
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) ,
493
491
}
494
492
}
495
493
}
@@ -500,7 +498,7 @@ impl<A, B> Into<Option<Either<A, B>>> for EitherOrBoth<A, B> {
500
498
match self {
501
499
EitherOrBoth :: Left ( l) => Some ( Either :: Left ( l) ) ,
502
500
EitherOrBoth :: Right ( r) => Some ( Either :: Right ( r) ) ,
503
- _ => None ,
501
+ EitherOrBoth :: Both ( .. ) => None ,
504
502
}
505
503
}
506
504
}
0 commit comments