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
//! Prototype of the `std::io::ensure` family of macros.
//!
//! # Examples
//!
//! ```
//! use io_ensure::*;
//! use std::io::ErrorKind;
//!
//! # fn main() -> std::io::Result<()> {
//! let a = 3;
//! let b = 1 + 2;
//! ensure!(a == b, ErrorKind::Other);
//! ensure!(a == b, ErrorKind::Interrupted, "we are testing addition with {} and {}", a, b);
//! # Ok(()) }
//! ```
/// Creates an [`io::Error`] using optional interpolation of runtime expressions.
///
/// Arguments to `format_err!` can either be literals which are passed to
/// `io::Error::new`, or directly interpolated strings constructed through the
/// `format!` macro.
///
/// See [`std::fmt`] for more information.
///
/// [`io::Error`]: https://doc.rust-lang.org/std/io/struct.Error.html
/// [`std::fmt`]: ../std/fmt/index.html
/// [`print!`]: ../std/macro.print.html
/// [`write!`]: core::write
/// [`to_string`]: crate::string::ToString
/// [`Display`]: core::fmt::Display
///
/// # Panics
///
/// `format_err!` panics if a formatting trait implementation returns an error.
/// This indicates an incorrect implementation since `fmt::Write for String`
/// never returns an error itself.
///
/// # Examples
///
/// ```
/// use io_ensure::format_err;
/// use std::io::ErrorKind;
///
/// // errors can be created from format strings
/// let custom_error = format_err!(ErrorKind::Other, "hello {}", "world!");
///
/// // errors can also be created from other errors
/// let custom_error2 = format_err!(ErrorKind::Interrupted, custom_error);
///
/// // errors can also be created without payload (and without memory allocation)
/// let eof_error = format_err!(ErrorKind::UnexpectedEof);
/// ```
/// Exits a function early with an [`io::Error`] if the condition is not satisfied.
///
/// Similar to [`assert!`], `ensure!` takes a condition and exits the function
/// if the condition fails. Unlike `assert!`, `ensure!` returns an `io::Error`,
/// it does not panic.
///
/// [`io::Error`]: https://doc.rust-lang.org/std/io/struct.Error.html
/// [`PartialEq`]: https://doc.rust-lang.org/std/cmp/trait.PartialEq.html
/// [`assert!`]: https://doc.rust-lang.org/std/macro.assert.html
///
/// # Examples
///
/// ```
/// use io_ensure::*;
/// use std::io::ErrorKind;
///
/// # fn main() -> std::io::Result<()> {
/// let a = 3;
/// let b = 1 + 2;
/// ensure!(a == b, ErrorKind::Other);
/// ensure!(a == b, ErrorKind::Interrupted, "we are testing addition with {} and {}", a, b);
/// # Ok(()) }
/// ```
/// Exits a function early with an [`io::Error`] if two expressions are not equal
/// to each other.
///
/// The comparison is performed using [`PartialEq`]. Similar to [`assert_eq!`],
/// `ensure_eq!` takes two expressions and exits the function if the comparison
/// fails. Unlike `assert_eq!`, `ensure!` returns an `io::Error`, it does not panic.
///
/// [`io::Error`]: https://doc.rust-lang.org/std/io/struct.Error.html
/// [`PartialEq`]: https://doc.rust-lang.org/std/cmp/trait.PartialEq.html
/// [`assert_eq!`]: https://doc.rust-lang.org/std/macro.assert_eq.html
///
/// # Examples
///
/// ```
/// use io_ensure::*;
/// use std::io::ErrorKind;
///
/// # fn main() -> std::io::Result<()> {
/// let a = 2;
/// let b = 1 + 1;
/// ensure_eq!(a, b, ErrorKind::Other);
///
/// ensure_eq!(a, b, ErrorKind::Interrupted, "we are testing the values {} and {} are equal", a, b);
/// # Ok(()) }
/// ```
/// Exits a function early with an [`io::Error`] if two expressions are equal to
/// each other.
///
/// The comparison is performed using [`PartialEq`]. Similar to [`assert_eq!`],
/// `ensure_eq!` takes two expressions and exits the function if the comparison
/// fails. Unlike `assert_eq!`, `ensure!` returns an `io::Error`, it does not panic.
///
/// [`io::Error`]: https://doc.rust-lang.org/std/io/struct.Error.html
/// [`PartialEq`]: https://doc.rust-lang.org/std/cmp/trait.PartialEq.html
/// [`assert_ne!`]: https://doc.rust-lang.org/std/macro.assert_ne.html
///
/// # Examples
///
/// ```
/// use io_ensure::*;
/// use std::io::ErrorKind;
///
/// # fn main() -> std::io::Result<()> {
/// let a = 2;
/// let b = 3;
/// ensure_ne!(a, b, ErrorKind::Other);
/// ensure_ne!(a, b, ErrorKind::Interrupted, "we are testing the values {} and {} are not equal", a, b);
/// # Ok(()) }
/// ```