Skip to content

Instantly share code, notes, and snippets.

@U007D
Created January 15, 2018 19:59
Show Gist options
  • Save U007D/56aa17fe8d227221d0d4c00fd45f7d0d to your computer and use it in GitHub Desktop.
Save U007D/56aa17fe8d227221d0d4c00fd45f7d0d to your computer and use it in GitHub Desktop.
// Crazy macro by bluss
// Split a stream of tt's into before and after `==`
macro_rules! split_eq {
($sep: tt ($m: ident $($args:tt)*) [$($stack:tt)*] $x: tt) => {
$m ! ($($args)* $($stack)* $x $sep)
};
($sep: tt ($m: ident $($args:tt)*) [$($stack:tt)*] $x: tt == $($rest: tt)*) => {
$m ! ($($args)* $($stack)* $x $sep $($rest)*)
};
($sep: tt ($m:ident $($args:tt)*) [$($stack:tt)*] $x: tt $($rest: tt)*) => {
split_eq!($sep ($m $($args)*) [$($stack)* $x] $($rest)*);
};
}
macro_rules! assert_impl {
($one:expr =>) => ( assert!($one) );
($a:expr => $b:expr) => (
{
let _a = &$a;
let _b = &$b;
assert!(_a == _b,
"Failed assertion {:?} == {:?} (`{}`):\n expected:\n {:?} (`{}`)\n but found:\n {:?} (`{}`)",
_a,
_b,
stringify!($a == $b),
_a,
stringify!($a),
_b,
stringify!($b),
);
assert!(_b == _a, "Failed assertion {:?} == {:?} (`{}`)", _b, _a, stringify!($b == $a));
}
);
}
macro_rules! cleverassert {
($($t:tt)+) => (
split_eq!(=> (assert_impl) [] $($t)+)
);
}
fn main() {
cleverassert!(true);
cleverassert!(2 == 2);
cleverassert!("hi" == "he".to_string());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment