-
Notifications
You must be signed in to change notification settings - Fork 12.9k
/
result.rs
423 lines (346 loc) · 12.3 KB
/
result.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
use core::ops::DerefMut;
use core::option::*;
fn op1() -> Result<isize, &'static str> {
Ok(666)
}
fn op2() -> Result<isize, &'static str> {
Err("sadface")
}
#[test]
fn test_and() {
assert_eq!(op1().and(Ok(667)).unwrap(), 667);
assert_eq!(op1().and(Err::<i32, &'static str>("bad")).unwrap_err(), "bad");
assert_eq!(op2().and(Ok(667)).unwrap_err(), "sadface");
assert_eq!(op2().and(Err::<i32, &'static str>("bad")).unwrap_err(), "sadface");
}
#[test]
fn test_and_then() {
assert_eq!(op1().and_then(|i| Ok::<isize, &'static str>(i + 1)).unwrap(), 667);
assert_eq!(op1().and_then(|_| Err::<isize, &'static str>("bad")).unwrap_err(), "bad");
assert_eq!(op2().and_then(|i| Ok::<isize, &'static str>(i + 1)).unwrap_err(), "sadface");
assert_eq!(op2().and_then(|_| Err::<isize, &'static str>("bad")).unwrap_err(), "sadface");
}
#[test]
fn test_or() {
assert_eq!(op1().or(Ok::<_, &'static str>(667)).unwrap(), 666);
assert_eq!(op1().or(Err("bad")).unwrap(), 666);
assert_eq!(op2().or(Ok::<_, &'static str>(667)).unwrap(), 667);
assert_eq!(op2().or(Err("bad")).unwrap_err(), "bad");
}
#[test]
fn test_or_else() {
assert_eq!(op1().or_else(|_| Ok::<isize, &'static str>(667)).unwrap(), 666);
assert_eq!(op1().or_else(|e| Err::<isize, &'static str>(e)).unwrap(), 666);
assert_eq!(op2().or_else(|_| Ok::<isize, &'static str>(667)).unwrap(), 667);
assert_eq!(op2().or_else(|e| Err::<isize, &'static str>(e)).unwrap_err(), "sadface");
}
#[test]
fn test_impl_map() {
assert!(Ok::<isize, isize>(1).map(|x| x + 1) == Ok(2));
assert!(Err::<isize, isize>(1).map(|x| x + 1) == Err(1));
}
#[test]
fn test_impl_map_err() {
assert!(Ok::<isize, isize>(1).map_err(|x| x + 1) == Ok(1));
assert!(Err::<isize, isize>(1).map_err(|x| x + 1) == Err(2));
}
#[test]
fn test_collect() {
let v: Result<Vec<isize>, ()> = (0..0).map(|_| Ok::<isize, ()>(0)).collect();
assert!(v == Ok(vec![]));
let v: Result<Vec<isize>, ()> = (0..3).map(|x| Ok::<isize, ()>(x)).collect();
assert!(v == Ok(vec![0, 1, 2]));
let v: Result<Vec<isize>, isize> = (0..3).map(|x| if x > 1 { Err(x) } else { Ok(x) }).collect();
assert!(v == Err(2));
// test that it does not take more elements than it needs
let mut functions: [Box<dyn Fn() -> Result<(), isize>>; 3] =
[Box::new(|| Ok(())), Box::new(|| Err(1)), Box::new(|| panic!())];
let v: Result<Vec<()>, isize> = functions.iter_mut().map(|f| (*f)()).collect();
assert!(v == Err(1));
}
#[test]
fn test_fmt_default() {
let ok: Result<isize, &'static str> = Ok(100);
let err: Result<isize, &'static str> = Err("Err");
let s = format!("{ok:?}");
assert_eq!(s, "Ok(100)");
let s = format!("{err:?}");
assert_eq!(s, "Err(\"Err\")");
}
#[test]
fn test_unwrap_or() {
let ok: Result<isize, &'static str> = Ok(100);
let ok_err: Result<isize, &'static str> = Err("Err");
assert_eq!(ok.unwrap_or(50), 100);
assert_eq!(ok_err.unwrap_or(50), 50);
}
#[test]
fn test_unwrap_or_else() {
fn handler(msg: &'static str) -> isize {
if msg == "I got this." { 50 } else { panic!("BadBad") }
}
let ok: Result<isize, &'static str> = Ok(100);
let ok_err: Result<isize, &'static str> = Err("I got this.");
assert_eq!(ok.unwrap_or_else(handler), 100);
assert_eq!(ok_err.unwrap_or_else(handler), 50);
}
#[test]
#[should_panic]
pub fn test_unwrap_or_else_panic() {
fn handler(msg: &'static str) -> isize {
if msg == "I got this." { 50 } else { panic!("BadBad") }
}
let bad_err: Result<isize, &'static str> = Err("Unrecoverable mess.");
let _: isize = bad_err.unwrap_or_else(handler);
}
#[test]
fn test_unwrap_unchecked() {
let ok: Result<isize, &'static str> = Ok(100);
assert_eq!(unsafe { ok.unwrap_unchecked() }, 100);
}
#[test]
fn test_unwrap_err_unchecked() {
let ok_err: Result<isize, &'static str> = Err("Err");
assert_eq!(unsafe { ok_err.unwrap_err_unchecked() }, "Err");
}
#[test]
pub fn test_expect_ok() {
let ok: Result<isize, &'static str> = Ok(100);
assert_eq!(ok.expect("Unexpected error"), 100);
}
#[test]
#[should_panic(expected = "Got expected error: \"All good\"")]
pub fn test_expect_err() {
let err: Result<isize, &'static str> = Err("All good");
err.expect("Got expected error");
}
#[test]
pub fn test_expect_err_err() {
let ok: Result<&'static str, isize> = Err(100);
assert_eq!(ok.expect_err("Unexpected ok"), 100);
}
#[test]
#[should_panic(expected = "Got expected ok: \"All good\"")]
pub fn test_expect_err_ok() {
let err: Result<&'static str, isize> = Ok("All good");
err.expect_err("Got expected ok");
}
#[test]
pub fn test_iter() {
let ok: Result<isize, &'static str> = Ok(100);
let mut it = ok.iter();
assert_eq!(it.size_hint(), (1, Some(1)));
assert_eq!(it.next(), Some(&100));
assert_eq!(it.size_hint(), (0, Some(0)));
assert!(it.next().is_none());
assert_eq!((&ok).into_iter().next(), Some(&100));
let err: Result<isize, &'static str> = Err("error");
assert_eq!(err.iter().next(), None);
}
#[test]
#[allow(for_loops_over_fallibles)]
pub fn test_iter_mut() {
let mut ok: Result<isize, &'static str> = Ok(100);
for loc in ok.iter_mut() {
*loc = 200;
}
assert_eq!(ok, Ok(200));
for loc in &mut ok {
*loc = 300;
}
assert_eq!(ok, Ok(300));
let mut err: Result<isize, &'static str> = Err("error");
for loc in err.iter_mut() {
*loc = 200;
}
assert_eq!(err, Err("error"));
}
#[test]
pub fn test_unwrap_or_default() {
assert_eq!(op1().unwrap_or_default(), 666);
assert_eq!(op2().unwrap_or_default(), 0);
}
#[test]
#[allow(non_local_definitions)]
pub fn test_into_ok() {
fn infallible_op() -> Result<isize, !> {
Ok(666)
}
assert_eq!(infallible_op().into_ok(), 666);
enum MyNeverToken {}
impl From<MyNeverToken> for ! {
fn from(never: MyNeverToken) -> ! {
match never {}
}
}
fn infallible_op2() -> Result<isize, MyNeverToken> {
Ok(667)
}
assert_eq!(infallible_op2().into_ok(), 667);
}
#[test]
#[allow(non_local_definitions)]
pub fn test_into_err() {
fn until_error_op() -> Result<!, isize> {
Err(666)
}
assert_eq!(until_error_op().into_err(), 666);
enum MyNeverToken {}
impl From<MyNeverToken> for ! {
fn from(never: MyNeverToken) -> ! {
match never {}
}
}
fn until_error_op2() -> Result<MyNeverToken, isize> {
Err(667)
}
assert_eq!(until_error_op2().into_err(), 667);
}
#[test]
fn test_try() {
fn try_result_ok() -> Result<u8, u32> {
let result: Result<u8, u8> = Ok(1);
let val = result?;
Ok(val)
}
assert_eq!(try_result_ok(), Ok(1));
fn try_result_err() -> Result<u8, u32> {
let result: Result<u8, u8> = Err(1);
let val = result?;
Ok(val)
}
assert_eq!(try_result_err(), Err(1));
}
#[test]
fn test_result_as_deref() {
// &Result<T: Deref, E>::Ok(T).as_deref() ->
// Result<&T::Deref::Target, &E>::Ok(&*T)
let ref_ok = &Result::Ok::<&i32, u8>(&42);
let expected_result = Result::Ok::<&i32, &u8>(&42);
assert_eq!(ref_ok.as_deref(), expected_result);
let ref_ok = &Result::Ok::<String, u32>(String::from("a result"));
let expected_result = Result::Ok::<&str, &u32>("a result");
assert_eq!(ref_ok.as_deref(), expected_result);
let ref_ok = &Result::Ok::<Vec<i32>, u32>(vec![1, 2, 3, 4, 5]);
let expected_result = Result::Ok::<&[i32], &u32>([1, 2, 3, 4, 5].as_slice());
assert_eq!(ref_ok.as_deref(), expected_result);
// &Result<T: Deref, E>::Err(T).as_deref() ->
// Result<&T::Deref::Target, &E>::Err(&*E)
let val = 41;
let ref_err = &Result::Err::<&u8, i32>(val);
let expected_result = Result::Err::<&u8, &i32>(&val);
assert_eq!(ref_err.as_deref(), expected_result);
let s = String::from("an error");
let ref_err = &Result::Err::<&u32, String>(s.clone());
let expected_result = Result::Err::<&u32, &String>(&s);
assert_eq!(ref_err.as_deref(), expected_result);
let v = vec![5, 4, 3, 2, 1];
let ref_err = &Result::Err::<&u32, Vec<i32>>(v.clone());
let expected_result = Result::Err::<&u32, &Vec<i32>>(&v);
assert_eq!(ref_err.as_deref(), expected_result);
}
#[test]
fn test_result_as_deref_mut() {
// &mut Result<T: DerefMut, E>::Ok(T).as_deref_mut() ->
// Result<&mut T::DerefMut::Target, &mut E>::Ok(&mut *T)
let mut val = 42;
let mut expected_val = 42;
let mut_ok = &mut Result::Ok::<&mut i32, u8>(&mut val);
let expected_result = Result::Ok::<&mut i32, &mut u8>(&mut expected_val);
assert_eq!(mut_ok.as_deref_mut(), expected_result);
let mut expected_string = String::from("a result");
let mut_ok = &mut Result::Ok::<String, u32>(expected_string.clone());
let expected_result = Result::Ok::<&mut str, &mut u32>(expected_string.deref_mut());
assert_eq!(mut_ok.as_deref_mut(), expected_result);
let mut expected_vec = vec![1, 2, 3, 4, 5];
let mut_ok = &mut Result::Ok::<Vec<i32>, u32>(expected_vec.clone());
let expected_result = Result::Ok::<&mut [i32], &mut u32>(expected_vec.as_mut_slice());
assert_eq!(mut_ok.as_deref_mut(), expected_result);
// &mut Result<T: DerefMut, E>::Err(T).as_deref_mut() ->
// Result<&mut T, &mut E>::Err(&mut *E)
let mut val = 41;
let mut_err = &mut Result::Err::<&mut u8, i32>(val);
let expected_result = Result::Err::<&mut u8, &mut i32>(&mut val);
assert_eq!(mut_err.as_deref_mut(), expected_result);
let mut expected_string = String::from("an error");
let mut_err = &mut Result::Err::<&mut u32, String>(expected_string.clone());
let expected_result = Result::Err::<&mut u32, &mut String>(&mut expected_string);
assert_eq!(mut_err.as_deref_mut(), expected_result);
let mut expected_vec = vec![5, 4, 3, 2, 1];
let mut_err = &mut Result::Err::<&mut u32, Vec<i32>>(expected_vec.clone());
let expected_result = Result::Err::<&mut u32, &mut Vec<i32>>(&mut expected_vec);
assert_eq!(mut_err.as_deref_mut(), expected_result);
}
#[test]
fn result_const() {
// test that the methods of `Result` are usable in a const context
const RESULT: Result<usize, bool> = Ok(32);
const REF: Result<&usize, &bool> = RESULT.as_ref();
assert_eq!(REF, Ok(&32));
const IS_OK: bool = RESULT.is_ok();
assert!(IS_OK);
const IS_ERR: bool = RESULT.is_err();
assert!(!IS_ERR)
}
#[test]
const fn result_const_mut() {
let mut result: Result<usize, bool> = Ok(32);
{
let as_mut = result.as_mut();
match as_mut {
Ok(v) => *v = 42,
Err(_) => unreachable!(),
}
}
let mut result_err: Result<usize, bool> = Err(false);
{
let as_mut = result_err.as_mut();
match as_mut {
Ok(_) => unreachable!(),
Err(v) => *v = true,
}
}
}
#[test]
fn result_opt_conversions() {
#[derive(Copy, Clone, Debug, PartialEq)]
struct BadNumErr;
fn try_num(x: i32) -> Result<i32, BadNumErr> {
if x <= 5 { Ok(x + 1) } else { Err(BadNumErr) }
}
type ResOpt = Result<Option<i32>, BadNumErr>;
type OptRes = Option<Result<i32, BadNumErr>>;
let mut x: ResOpt = Ok(Some(5));
let mut y: OptRes = Some(Ok(5));
assert_eq!(x, y.transpose());
assert_eq!(x.transpose(), y);
x = Ok(None);
y = None;
assert_eq!(x, y.transpose());
assert_eq!(x.transpose(), y);
x = Err(BadNumErr);
y = Some(Err(BadNumErr));
assert_eq!(x, y.transpose());
assert_eq!(x.transpose(), y);
let res: Result<Vec<i32>, BadNumErr> = (0..10)
.map(|x| {
let y = try_num(x)?;
Ok(if y % 2 == 0 { Some(y - 1) } else { None })
})
.filter_map(Result::transpose)
.collect();
assert_eq!(res, Err(BadNumErr))
}
#[test]
fn result_try_trait_v2_branch() {
use core::num::NonZero;
use core::ops::ControlFlow::*;
use core::ops::Try;
assert_eq!(Ok::<i32, i32>(4).branch(), Continue(4));
assert_eq!(Err::<i32, i32>(4).branch(), Break(Err(4)));
let one = NonZero::new(1).unwrap();
assert_eq!(Ok::<(), NonZero<u32>>(()).branch(), Continue(()));
assert_eq!(Err::<(), NonZero<u32>>(one).branch(), Break(Err(one)));
assert_eq!(Ok::<NonZero<u32>, ()>(one).branch(), Continue(one));
assert_eq!(Err::<NonZero<u32>, ()>(()).branch(), Break(Err(())));
}