claim/
assert_ready_err.rs

1/// Asserts that expression returns [`Poll::Ready(Err(E))`] variant.
2///
3/// This macro is available for Rust 1.36+.
4///
5/// ## Uses
6///
7/// Assertions are always checked in both debug and release builds, and cannot be disabled.
8/// See [`debug_assert_ready_err!`] for assertions that are not enabled in release builds by default.
9///
10/// ## Custom messages
11///
12/// This macro has a second form, where a custom panic message can be provided
13/// with or without arguments for formatting. See [`std::fmt`] for syntax for this form.
14///
15/// ## Examples
16///
17/// ```rust
18/// # #[macro_use] extern crate claim;
19/// # #[cfg(feature = "std")] extern crate std as core;
20/// # use std::task::Poll;
21/// # fn main() {
22/// let res: Poll<Result<i32, ()>> = Poll::Ready(Err(()));
23///
24/// assert_ready_err!(res);
25/// # }
26/// ```
27///
28/// Value of `E` type from the `Poll::Ready(Err(E))` will also be returned from this macro call:
29///
30/// ```rust
31/// # #[macro_use] extern crate claim;
32/// # use std::task::Poll;
33/// # fn main() {
34/// let res: Poll<Result<i32, String>> = Poll::Ready(Err("Something went wrong".to_string()));
35///
36/// let message = assert_ready_err!(res);
37/// assert_eq!("Something went wrong", message);
38/// # }
39/// ```
40///
41/// Both `Poll::Ready(Ok(..))` and [`Poll::Pending`] variants will cause panic:
42///
43/// ```rust,should_panic
44/// # #[macro_use] extern crate claim;
45/// # use std::task::Poll;
46/// # fn main() {
47/// let res: Poll<Result<i32, ()>> = Poll::Ready(Ok(42));
48///
49/// assert_ready_err!(res);  // Will panic
50/// # }
51/// ```
52
53/// ```rust,should_panic
54/// # #[macro_use] extern crate claim;
55/// # use std::task::Poll;
56/// # fn main() {
57/// let res: Poll<Result<i32, ()>> = Poll::Pending;
58///
59/// assert_ready_err!(res);  // Will panic
60/// # }
61/// ```
62///
63/// [`Poll::Ready(Err(E))`]: https://doc.rust-lang.org/core/task/enum.Poll.html#variant.Ready
64/// [`Poll::Pending`]: https://doc.rust-lang.org/core/task/enum.Poll.html#variant.Pending
65/// [`std::fmt`]: https://doc.rust-lang.org/std/fmt/index.html
66/// [`debug_assert_ready_err!`]: ./macro.debug_assert_ready_err.html
67#[macro_export]
68macro_rules! assert_ready_err {
69    ($cond:expr,) => {
70        $crate::assert_ready_err!($cond);
71    };
72    ($cond:expr) => {
73        match $cond {
74            core::task::Poll::Ready(Err(e)) => e,
75            ok_or_pending => {
76                panic!("assertion failed, expected Ready(Err(..)), got {:?}", ok_or_pending);
77            }
78        }
79    };
80    ($cond:expr, $($arg:tt)+) => {
81        match $cond {
82            core::task::Poll::Ready(Err(e)) => e,
83            ok_or_pending => {
84                panic!("assertion failed, expected Ready(Err(..)), got {:?}: {}", ok_or_pending, format_args!($($arg)+));
85            }
86        }
87    };
88}
89
90/// Asserts that expression returns [`Poll::Ready(Err(E))`] variant in runtime.
91///
92/// Like [`assert_ready_err!`], this macro also has a second version,
93/// where a custom panic message can be provided.
94///
95/// ## Uses
96///
97/// See [`debug_assert!`] documentation for possible use cases.
98/// The same applies to this macro.
99///
100/// [`Poll::Ready(Err(E))`]: https://doc.rust-lang.org/core/task/enum.Poll.html#variant.Ready
101/// [`debug_assert!`]: https://doc.rust-lang.org/std/macro.debug_assert.html
102/// [`assert_ready_err!`]: ./macro.assert_ready_err.html
103#[macro_export]
104macro_rules! debug_assert_ready_err {
105    ($($arg:tt)*) => (if core::cfg!(debug_assertions) { $crate::assert_ready_err!($($arg)*); })
106}