1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#![no_std]

extern crate alloc;

use alloc::fmt::{Debug, Formatter, Result};

pub mod crypto;
pub mod payload;
pub mod sys;
pub mod transaction;

pub fn log(msg: &str) {
    unsafe {
        let msg = msg.as_bytes();
        sys::_log(msg.as_ptr(), msg.len());
    }
}

#[allow(dead_code)]
#[doc(hidden)]
pub struct WrapDebug<T>(pub T);

impl<T: Debug> Debug for WrapDebug<T> {
    fn fmt(&self, f: &mut Formatter) -> Result {
        self.0.fmt(f)
    }
}

#[macro_export]
macro_rules! debug {
    // Handle `debug!()` <-- literal
    () => {
        debug!( () );
    };
    // Handle trailing comma:
    ($($val: expr),+,) => {
        debug!( $($val),+ )
    };
    ($($lab: expr => $val: expr),+,) => {
        debug!( $($lab => $val),+ )
    };
    // Without label, use source of $val:
    ($valf: expr $(, $val: expr)*) => {{
        // in order: for panics, clarification on: debug!(expr);, debug!(expr)
        #[allow(unreachable_code, unused_must_use, unused_parens)]
        let _r = {
            {

            use ::std::io::Write;
            let mut buf = ::std::io::BufWriter::new(vec![]);

            write!(&mut buf, "[{}:{}] ", file!(), line!()).unwrap();

            // Foreach label and expression:
            //     1. Evaluate each expression,
            //     2. Print out $lab = value of expression
            let _ret = (
                {
                    // Evaluate, tmp is value:
                    let _tmp = $crate::WrapDebug($valf);
                    // Won't get further if $val panics.

                    // Print out $lab = _tmo:
                    write!(&mut buf, "{} = {:?}", stringify!($valf), _tmp).unwrap();

                    // Yield tmp:
                    _tmp.0
                }
                $(, {
                    // Comma separator:
                    write!(&mut buf, ", ").unwrap();

                    // Print out $lab = :
                    write!(&mut buf, "{} = ", stringify!($val)).unwrap();

                    // Evaluate, tmp is value:
                    let _tmp = $crate::WrapDebug($val);
                    // Won't get further if $val panics.

                    // Print out tmp:
                    write!(&mut buf, "{:?}" , _tmp).unwrap();

                    // Yield tmp:
                    _tmp.0
                } )*
            );

            $crate::log(::std::str::from_utf8(&buf.into_inner().unwrap()).unwrap());

            // Return the expression:
            _ret
        }
        };
        _r
    }};
    // With label:
    ($labf: expr => $valf: expr $(, $lab: expr => $val: expr)*) => {{
        // in order: for panics, clarification on: debug!(expr);, debug!(expr)
        #[allow(unreachable_code, unused_must_use, unused_parens)]
        let _r = {
            {
            use ::std::io::Write;
            let mut buf = ::std::io::BufWriter::new(vec![]);

            write!(&mut buf, "[{}:{}] ", file!(), line!());

            // Foreach label and expression:
            //     1. Evaluate each expression,
            //     2. Print out $lab = value of expression
            let _ret = (
                {
                    // Enforce is_literal_string($lab):
                    let _ = concat!($labf, "");
                    let _ : &'static str = $labf;

                    // Print out $lab = :
                    write!(&mut buf, "{} = ", stringify!($labf)).unwrap();

                    // Evaluate, tmp is value:
                    let _tmp = $crate::WrapDebug($valf);
                    // Won't get further if $val panics.

                    // Print out tmp:
                    write!(&mut buf, "{:?}" , _tmp).unwrap();

                    // Yield tmp:
                    _tmp.0
                }
                $(, {
                    // Comma separator:
                    write!(&mut buf, ", ").unwrap();

                    // Enforce is_literal_string($lab):
                    let _ = concat!($lab, "");
                    let _ : &'static str = $lab;

                    // Print out $lab = :
                    write!(&mut buf, "{} = ", stringify!($lab)).unwrap();

                    // Evaluate, tmp is value:
                    let _tmp = $crate::WrapDebug($val);
                    // Won't get further if $val panics.

                    // Print out tmp:
                    write!(&mut buf, "{:?}" , _tmp).unwrap();

                    // Yield tmp:
                    _tmp.0
                } )*
            );

            $crate::log(::std::str::from_utf8(&buf.into_inner().unwrap()).unwrap());

            // Return the expression:
            _ret
        }
        };
        _r
    }};
}