Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::fmt;
- /// Extends a (possibly unsized) value with a Debug string.
- // (This type is unsized when T is unsized)
- pub struct Debuggable<T: ?Sized> {
- text: &'static str,
- value: T,
- }
- /// Produce a Debuggable<T> from an expression for T
- macro_rules! dbg {
- ($($body:tt)+) => {
- Debuggable {
- text: stringify!($($body)+),
- value: $($body)+,
- }
- };
- }
- impl<T: ?Sized> fmt::Debug for Debuggable<T> {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- write!(f, "{}", self.text)
- }
- }
- // This makes Debuggable have most methods of the thing it wraps.
- // It also lets you call it when T is a function.
- impl<T: ?Sized> ::std::ops::Deref for Debuggable<T> {
- type Target = T;
- fn deref(&self) -> &T {
- &self.value
- }
- }
- fn main() {
- let d = &dbg!(|x| {
- let _ = "random code so you can see how it's formatted";
- assert_eq!(3 * (1 + 2), 9);
- x * x
- });
- println!("{:?}", d);
- println!("{}", d(100));
- let f = 100;
- let e = dbg!(100 + f);
- println!("{:?} = {}", e, *e);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement