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
//! A `no_std` library to simpify chaining methods when you are returning a [`Result`]. It is a
//! trivial library which sould be compatible with all environments.
//!
//! This is specially noticeable when using crates like [`anyhow`](https://crates.io/crates/anyhow)
//! which provide a "catch all" error type, so you need to convert all errors you recieve.
//!
//! It is also helpful when you have many custom errors constructed with
//! [`thiserror`](https://crates.io/crates/thiserror) or
//! [`justerror`](https://crates.io/crates/justerror), or use many libraries with different error
//! types.
//!
//! # Usage
//!
//! Import the traits and you can benefit from it immediately:
//!
//! ```rust
//! use err_into::MapInto;
//! use err_into::ErrorInto;
//! use err_into::ResultInto;
//!
//! // ...
//!
//! let _: Option<i32> = Some(0u8).map_into();
//! let _: Result<i32, ()> = Ok(0u8).map_into();
//! let _: Result<(), i32> = Err(0u8).err_into();
//! let _: Result<u16, i32> = (if false { Ok(0u8) } else { Err(0i8) }).res_into();
//! ```
//!
//! ## Motivating example
//!
//! This is slightly contrived because I don't want to depend on any libraries but showcases where
//! `err_into` excels:
//!
//! ```rust
//! use err_into::ErrorInto;
//!
//! fn process(val: u16) -> Result<u8, u8> {
//! if val > 255 {
//! Err((val >> 8) as u8)
//! } else {
//! Ok(val as _)
//! }
//! }
//!
//! fn long_chain() -> Result<u8, i32> {
//! (0u16..16u16).map(|x| x * x * x * x)
//! .filter(|x| x % 2 == 0)
//! .map(process)
//! .next()
//! .unwrap_or(Err(0))
//! .err_into()
//! }
//!
//! fn long_chain_no_err_into() -> Result<u8, i32> {
//! // May be confusing
//! (0u16..16u16).map(|x| x * x * x * x)
//! .filter(|x| x % 2 == 0)
//! .map(process)
//! .next()
//! .unwrap_or(Err(0))
//! .map_err(Into::into)
//! }
//!
//! fn long_chain_no_map_err() -> Result<u8, i32> {
//! // Please don't do this
//! Ok((0u16..16u16).map(|x| x * x * x * x)
//! .filter(|x| x % 2 == 0)
//! .map(process)
//! .next()
//! .unwrap_or(Err(0))?)
//! }
//! ```
/// Maps an error using [`Into::into`]
///
/// Short version of `Result::map_err(self, Into::into)` that simplifies operation chains like
///
/// ```rust
/// use err_into::ErrorInto;
///
/// fn get_data() -> Result<(), u8> {
/// Err(0u8)
/// }
///
/// fn handle_data_question_mark() -> Result<(), i32> {
/// // Can't be split into multiple lines
/// Ok(get_data()?)
/// }
///
/// fn handle_data_map_err() -> Result<(), i32> {
/// // Slightly harder to read
/// get_data().map_err(Into::into)
/// }
///
/// fn handle_data_err_into() -> Result<(), i32> {
/// get_data().err_into()
/// }
///
/// assert_eq!(handle_data_err_into(), handle_data_question_mark());
/// assert_eq!(handle_data_err_into(), handle_data_map_err());
/// ```
/// Maps both the Value and the Error of a [`Result`] using [`Into::into`]
///
/// Shorthand for `result.map(Into::into).map_err(Into::into)`
///
/// ```rust
/// use err_into::ResultInto;
///
/// let res: Result<u8, i8> = Ok(0);
/// let _: Result<i32, i16> = res.res_into();
/// ```
/// Maps a value using [`Into::into`]
///
/// Shorthand for `Option::map(self, Into::into)` and `Result::map(self, Into::into)`
///
/// ```rust
/// use err_into::MapInto;
///
/// let value = Some(0u8);
/// let map_into: Option<i32> = value.map_into();
/// let map_into_std: Option<i32> = value.map(Into::into);
/// assert_eq!(map_into, map_into_std);
///
/// let result = Ok(0u8);
/// let map_into: Result<i32, ()> = result.map_into();
/// let map_into_std: Result<i32, ()> = result.map(Into::into);
/// assert_eq!(map_into, map_into_std);
/// ```