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