Skip to content

Instantly share code, notes, and snippets.

@solson
Last active March 17, 2017 16:40
Show Gist options
  • Save solson/b60fa29782197bb6b6f9 to your computer and use it in GitHub Desktop.
Save solson/b60fa29782197bb6b6f9 to your computer and use it in GitHub Desktop.
"WIP crate" example
/// Debug printing for lazy people.
///
/// `p!(a, b, c)` prints the values of the expressions `a`, `b`, and `b` along
/// with their source code.
///
/// # Example
///
/// ```
/// #[derive(Debug)]
/// struct Point { x: i32, y: i32 }
/// let point = Point { x: 42, y: 100 };
/// p!(point.x, point.y * 2);
/// // Prints:
/// // point.x = 42
/// // point.y * 2 = 200
/// ```
macro_rules! p {
($($e:expr),+) => ({
$(
println!("{} = {:?}", stringify!($e), $e);
)+
})
}
/// Breakpoints.
///
/// `b!()` causes an unconditional breakpoint.
/// `b!(cond)` causes a breakpoint if `cond` evaluates to `true`.
///
/// In nightly only (needs unstable features).
///
/// # Example
///
/// ```
/// fn foo(x: i32) {
/// // Break in the debugger whenever `foo(0)` is called.
/// b!(x == 0);
/// }
/// ```
macro_rules! b {
() => (::std::intrinsics::breakpoint());
($cond:expr) => (if cond { b!(); });
}
/// A placeholder for soon-to-be-implemented bits of code.
///
/// For example, I often want to test one branch of a match
/// without implementing the other cases, but I also want to
/// crash if another case comes up, because it would be bad
/// to have the other cases silently ignored and then forget
/// about it.
///
/// ```
/// match expr {
/// Pattern(a, b) => test_me(a, b),
/// Pattern2 => todo!("Pattern2"),
/// Pattern3(a) => todo!("Pattern3 with {:?}", a),
/// _ => todo!("the rest"),
/// }
/// ```
macro_rules! todo {
() => (panic!("TODO"));
($message:expr) => (panic!(concat!("TODO: ", $message)));
($format:expr, $($arg:expr),+) => (panic!(concat!("TODO: ", $format), $($arg),+));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment