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
//! _Sometimes you want to throw errors away._
//!
//! This crate does exactly one thing, it adds a new method to the `Result`
//! type that throws away the current error, if there is one, and replaces
//! it with a new value. You can see it in action here:
//!
//! ```
//! use replace_err::ReplaceErr as _;
//!
//! let result = Err(1);
//! let result: Result<(), _> = result.replace_err("hello");
//! assert_eq!(result.unwrap_err(), "hello");
//! ```
//!
//! This is exactly equivalent to calling `Result::map_err` with a closure
//! which ignores the input and returns something else. In fact, that's how
//! `replace_err` is implemented.
//!
//! Most of the time, you _do not_ want to do this. Usually you want to
//! wrap prior errors with new layers to add context, giving you a chain
//! of increasingly-specific and low-level explanations which error
//! reporters can present to the user, or based on which higher-level code
//! can take action.
//!
//! However, there _are_ some cases where you really don't need the
//! underlying error, and `replace_err` provides a convenient way to
//! express that need.
/// Extend `Result` with a `replace_err` method.