claims/
assert_ready_err.rs1#[macro_export]
67macro_rules! assert_ready_err {
68 ($cond:expr $(,)?) => {
69 match $cond {
70 ::core::task::Poll::Ready(::core::result::Result::Err(e)) => e,
71 ::core::task::Poll::Ready(::core::result::Result::Ok(t)) => ::core::panic!("assertion failed, expected Ready(Err(_)), got Ready(Ok({:?}))", t),
72 ::core::task::Poll::Pending => ::core::panic!("assertion failed, expected Ready(Err(_)), got Pending"),
73 }
74 };
75 ($cond:expr, $($arg:tt)+) => {
76 match $cond {
77 ::core::task::Poll::Ready(::core::result::Result::Err(e)) => e,
78 ::core::task::Poll::Ready(::core::result::Result::Ok(t)) => ::core::panic!("assertion failed, expected Ready(Err(_)), got Ready(Ok({:?})): {}", t, ::core::format_args!($($arg)+)),
79 ::core::task::Poll::Pending => ::core::panic!("assertion failed, expected Ready(Err(_)), got Pending: {}", ::core::format_args!($($arg)+)),
80 }
81 };
82}
83
84#[macro_export]
91macro_rules! debug_assert_ready_err {
92 ($($arg:tt)*) => {
93 #[cfg(debug_assertions)]
94 $crate::assert_ready_err!($($arg)*);
95 }
96}
97
98#[cfg(test)]
99mod tests {
100 use core::task::Poll::{Pending, Ready};
101
102 #[test]
103 fn ready_err() {
104 assert_ready_err!(Ready(Err::<(), _>(())));
105 }
106
107 #[test]
108 #[should_panic(expected = "assertion failed, expected Ready(Err(_)), got Ready(Ok(()))")]
109 fn ready_ok() {
110 assert_ready_err!(Ready(Ok::<_, ()>(())));
111 }
112
113 #[test]
114 #[should_panic(expected = "assertion failed, expected Ready(Err(_)), got Pending")]
115 fn not_ready() {
116 assert_ready_err!(Pending::<Result<(), ()>>);
117 }
118
119 #[test]
120 #[should_panic(expected = "assertion failed, expected Ready(Err(_)), got Ready(Ok(())): foo")]
121 fn ready_ok_custom_message() {
122 assert_ready_err!(Ready(Ok::<_, ()>(())), "foo");
123 }
124
125 #[test]
126 #[should_panic(expected = "assertion failed, expected Ready(Err(_)), got Pending: foo")]
127 fn not_ready_custom_message() {
128 assert_ready_err!(Pending::<Result<(), ()>>, "foo");
129 }
130
131 #[test]
132 fn ready_err_value_returned() {
133 let value = assert_ready_err!(Ready(Err::<(), _>(42)));
134 assert_eq!(value, 42);
135 }
136
137 #[test]
138 #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")]
139 fn debug_ready_err() {
140 debug_assert_ready_err!(Ready(Err::<(), _>(())));
141 }
142
143 #[test]
144 #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")]
145 #[should_panic(expected = "assertion failed, expected Ready(Err(_)), got Ready(Ok(()))")]
146 fn debug_ready_ok() {
147 debug_assert_ready_err!(Ready(Ok::<_, ()>(())));
148 }
149
150 #[test]
151 #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")]
152 #[should_panic(expected = "assertion failed, expected Ready(Err(_)), got Pending")]
153 fn debug_not_ready() {
154 debug_assert_ready_err!(Pending::<Result<(), ()>>);
155 }
156
157 #[test]
158 #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")]
159 #[should_panic(expected = "assertion failed, expected Ready(Err(_)), got Ready(Ok(())): foo")]
160 fn debug_ready_ok_custom_message() {
161 debug_assert_ready_err!(Ready(Ok::<_, ()>(())), "foo");
162 }
163
164 #[test]
165 #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")]
166 #[should_panic(expected = "assertion failed, expected Ready(Err(_)), got Pending: foo")]
167 fn debug_not_ready_custom_message() {
168 debug_assert_ready_err!(Pending::<Result<(), ()>>, "foo");
169 }
170
171 #[test]
172 #[cfg_attr(debug_assertions, ignore = "only run in release mode")]
173 fn debug_release_ready_ok() {
174 debug_assert_ready_err!(Ready(Ok::<_, ()>(())));
175 }
176
177 #[test]
178 #[cfg_attr(debug_assertions, ignore = "only run in release mode")]
179 fn debug_release_not_ready() {
180 debug_assert_ready_err!(Pending::<Result<(), ()>>);
181 }
182
183 #[test]
184 fn does_not_require_err_to_impl_debug() {
185 enum Foo {
186 Bar,
187 }
188
189 assert_ready_err!(Ready(Err::<(), _>(Foo::Bar)));
190 }
191
192 #[test]
193 fn debug_does_not_require_err_to_impl_debug() {
194 #[allow(dead_code)]
195 enum Foo {
196 Bar,
197 }
198
199 debug_assert_ready_err!(Ready(Err::<(), _>(Foo::Bar)));
200 }
201
202 #[test]
203 fn does_not_require_err_to_impl_debug_custom_message() {
204 enum Foo {
205 Bar,
206 }
207
208 assert_ready_err!(Ready(Err::<(), _>(Foo::Bar)), "foo");
209 }
210
211 #[test]
212 fn debug_does_not_require_err_to_impl_debug_custom_message() {
213 #[allow(dead_code)]
214 enum Foo {
215 Bar,
216 }
217
218 debug_assert_ready_err!(Ready(Err::<(), _>(Foo::Bar)), "foo");
219 }
220}