assertables/assert_ready/assert_ready.rs
1//! Assert an expression is Ready.
2//!
3//! Pseudocode:<br>
4//! a is Ready
5//!
6//! # Example
7//!
8//! ```rust
9//! use assertables::*;
10//! use std::task::Poll;
11//! use std::task::Poll::*;
12//!
13//! let a: Poll<i8> = Ready(1);
14//! assert_ready!(a);
15//! ```
16//!
17//! # Module macros
18//!
19//! * [`assert_ready`](macro@crate::assert_ready)
20//! * [`assert_ready_as_result`](macro@crate::assert_ready_as_result)
21//! * [`debug_assert_ready`](macro@crate::debug_assert_ready)
22
23/// Assert an expression is Ready.
24///
25/// Pseudocode:<br>
26/// a is Ready(a1)
27///
28/// * If true, return Result `Ok(a1)`.
29///
30/// * Otherwise, return Result `Err(message)`.
31///
32/// This macro is useful for runtime checks, such as checking parameters,
33/// or sanitizing inputs, or handling different results in different ways.
34///
35/// # Module macros
36///
37/// * [`assert_ready`](macro@crate::assert_ready)
38/// * [`assert_ready_as_result`](macro@crate::assert_ready_as_result)
39/// * [`debug_assert_ready`](macro@crate::debug_assert_ready)
40///
41#[macro_export]
42macro_rules! assert_ready_as_result {
43 ($a:expr $(,)?) => {
44 match ($a) {
45 Ready(a1) => Ok(a1),
46 _ => Err(format!(
47 concat!(
48 "assertion failed: `assert_ready!(a)`\n",
49 "https://docs.rs/assertables/9.8.3/assertables/macro.assert_ready.html\n",
50 " a label: `{}`,\n",
51 " a debug: `{:?}`",
52 ),
53 stringify!($a),
54 $a,
55 )),
56 }
57 };
58}
59
60#[cfg(test)]
61mod test_assert_ready_as_result {
62 // use std::sync::Once;
63 use std::task::Poll;
64 use std::task::Poll::*;
65
66 #[test]
67 fn success() {
68 let a: Poll<i8> = Ready(1);
69 for _ in 0..1 {
70 let actual = assert_ready_as_result!(a);
71 assert_eq!(actual.unwrap(), 1);
72 }
73 }
74
75 #[test]
76 fn failure() {
77 let a: Poll<i8> = Pending;
78 let actual = assert_ready_as_result!(a);
79 let message = concat!(
80 "assertion failed: `assert_ready!(a)`\n",
81 "https://docs.rs/assertables/9.8.3/assertables/macro.assert_ready.html\n",
82 " a label: `a`,\n",
83 " a debug: `Pending`",
84 );
85 assert_eq!(actual.unwrap_err(), message);
86 }
87}
88
89/// Assert an expression is Ready.
90///
91/// Pseudocode:<br>
92/// a is Ready(a1)
93///
94/// * If true, return `(a1)`.
95///
96/// * Otherwise, call [`panic!`] with a message and the values of the
97/// expressions with their debug representations.
98///
99/// # Examples
100///
101/// ```rust
102/// use assertables::*;
103/// # use std::panic;
104/// use std::task::Poll;
105/// use std::task::Poll::*;
106/// # fn main() {
107/// let a: Poll<i8> = Ready(1);
108/// assert_ready!(a);
109///
110/// # let result = panic::catch_unwind(|| {
111/// // This will panic
112/// let a: Poll<i8> = Pending;
113/// assert_ready!(a);
114/// # });
115/// // assertion failed: `assert_ready!(a)`
116/// // https://docs.rs/assertables/…/assertables/macro.assert_ready.html
117/// // a label: `a`,
118/// // a debug: `Pending`
119/// # let actual = result.unwrap_err().downcast::<String>().unwrap().to_string();
120/// # let message = concat!(
121/// # "assertion failed: `assert_ready!(a)`\n",
122/// # "https://docs.rs/assertables/9.8.3/assertables/macro.assert_ready.html\n",
123/// # " a label: `a`,\n",
124/// # " a debug: `Pending`",
125/// # );
126/// # assert_eq!(actual, message);
127/// # }
128/// ```
129///
130/// # Module macros
131///
132/// * [`assert_ready`](macro@crate::assert_ready)
133/// * [`assert_ready_as_result`](macro@crate::assert_ready_as_result)
134/// * [`debug_assert_ready`](macro@crate::debug_assert_ready)
135///
136#[macro_export]
137macro_rules! assert_ready {
138 ($a:expr $(,)?) => {
139 match $crate::assert_ready_as_result!($a) {
140 Ok(x) => x,
141 Err(err) => panic!("{}", err),
142 }
143 };
144 ($a:expr, $($message:tt)+) => {
145 match $crate::assert_ready_as_result!($a) {
146 Ok(x) => x,
147 Err(err) => panic!("{}\n{}", format_args!($($message)+), err),
148 }
149 };
150}
151
152#[cfg(test)]
153mod test_assert_ready {
154 use std::panic;
155 use std::task::Poll;
156 use std::task::Poll::*;
157
158 #[test]
159 fn success() {
160 let a: Poll<i8> = Ready(1);
161 for _ in 0..1 {
162 let actual = assert_ready!(a);
163 assert_eq!(actual, 1);
164 }
165 }
166
167 #[test]
168 fn failure() {
169 let a: Poll<i8> = Pending;
170 let result = panic::catch_unwind(|| {
171 let _actual = assert_ready!(a);
172 });
173 let message = concat!(
174 "assertion failed: `assert_ready!(a)`\n",
175 "https://docs.rs/assertables/9.8.3/assertables/macro.assert_ready.html\n",
176 " a label: `a`,\n",
177 " a debug: `Pending`",
178 );
179 assert_eq!(
180 result
181 .unwrap_err()
182 .downcast::<String>()
183 .unwrap()
184 .to_string(),
185 message
186 );
187 }
188}
189
190/// Assert an expression is Ready.
191///
192/// Pseudocode:<br>
193/// a is Ready(a1)
194///
195/// This macro provides the same statements as [`assert_ready`](macro.assert_ready.html),
196/// except this macro's statements are only enabled in non-optimized
197/// builds by default. An optimized build will not execute this macro's
198/// statements unless `-C debug-assertions` is passed to the compiler.
199///
200/// This macro is useful for checks that are too expensive to be present
201/// in a release build but may be helpful during development.
202///
203/// The result of expanding this macro is always type checked.
204///
205/// An unchecked assertion allows a program in an inconsistent state to
206/// keep running, which might have unexpected consequences but does not
207/// introduce unsafety as long as this only happens in safe code. The
208/// performance cost of assertions, however, is not measurable in general.
209/// Replacing `assert*!` with `debug_assert*!` is thus only encouraged
210/// after thorough profiling, and more importantly, only in safe code!
211///
212/// This macro is intended to work in a similar way to
213/// [`::std::debug_assert`](https://doc.rust-lang.org/std/macro.debug_assert.html).
214///
215/// # Module macros
216///
217/// * [`assert_ready`](macro@crate::assert_ready)
218/// * [`assert_ready`](macro@crate::assert_ready)
219/// * [`debug_assert_ready`](macro@crate::debug_assert_ready)
220///
221#[macro_export]
222macro_rules! debug_assert_ready {
223 ($($arg:tt)*) => {
224 if $crate::cfg!(debug_assertions) {
225 $crate::assert_ready!($($arg)*);
226 }
227 };
228}