Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // I was really annoyed that I couldn't easily assert that a
- // straightforward enum was being matched. I'm sure there's a better
- // way to do this but, hey, this is my first Rust macro.
- macro_rules! assert_is_enum {
- ($left:expr, $right:path) => ({
- match &$left {
- left_val =>
- if let $right = *left_val { }
- else {
- panic!(r#"assertion failed: `(left == right)`
- left: `{:?}`,
- right: `{:?}`"#, $left, $right)
- }
- }
- });
- ($left:expr, $right:path, $($arg:tt)+) => ({
- match &$left {
- left_val =>
- if let $right = *left_val { }
- else {
- panic!(r#"assertion failed: `(left == right)`
- left: `{:?}`,
- right: `{:?}`: {}"#, $left, $right,
- format_args!($($arg)+))
- }
- }
- });
- }
- macro_rules! assert_is_not_enum {
- ($left:expr, $right:path) => ({
- match &$left {
- left_val =>
- if let $right = *left_val {
- panic!(r#"assertion failed: `(left == right)`
- left: `{:?}`,
- right: `{:?}`"#, $left, $right)
- }
- else { }
- }
- });
- ($left:expr, $right:path, $($arg:tt)+) => ({
- match &$left {
- left_val =>
- if let $right = *left_val {
- panic!(r#"assertion failed: `(left == right)`
- left: `{:?}`,
- right: `{:?}`: {}"#, $left, $right,
- format_args!($($arg)+))
- }
- else { }
- }
- });
- }
- #[cfg(test)]
- mod tests {
- #[derive(Debug)]
- pub enum Demo {
- This,
- Test
- }
- use self::Demo::*;
- #[test]
- pub fn test() {
- let x: Demo = This;
- assert_is_enum!(x, This);
- assert_is_not_enum!(x, Test);
- }
- }
Add Comment
Please, Sign In to add comment