about summary refs log tree commit diff
path: root/src/libstd/macros.rs
blob: d72494b625535a3dec74e0ceeba01d56560ca782 (plain)
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
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#[macro_escape];
#[doc(hidden)];

macro_rules! rterrln (
    ($( $arg:expr),+) => ( {
        ::rt::util::dumb_println(fmt!( $($arg),+ ));
    } )
)

// Some basic logging. Enabled by passing `--cfg rtdebug` to the libstd build.
macro_rules! rtdebug (
    ($( $arg:expr),+) => ( {
        if cfg!(rtdebug) {
            rterrln!( $($arg),+ )
        }
    })
)

macro_rules! rtassert (
    ( $arg:expr ) => ( {
        if ::rt::util::ENFORCE_SANITY {
            if !$arg {
                rtabort!("assertion failed: %s", stringify!($arg));
            }
        }
    } )
)


macro_rules! rtabort(
    ($( $msg:expr),+) => ( {
        ::rt::util::abort(fmt!($($msg),+));
    } )
)

macro_rules! assert_once_ever(
    ($( $msg:expr),+) => ( {
        // FIXME(#8472) extra function should not be needed to hide unsafe
        fn assert_once_ever() {
            unsafe {
                static mut already_happened: int = 0;
                // Double-check lock to avoid a swap in the common case.
                if already_happened != 0 ||
                    ::unstable::intrinsics::atomic_xchg_relaxed(&mut already_happened, 1) != 0 {
                        fail!(fmt!("assert_once_ever happened twice: %s", fmt!($($msg),+)));
                }
            }
        }
        assert_once_ever();
    } )
)

#[cfg(test)]
mod tests {
    #[test]
    fn test_assert_once_ever_ok() {
        assert_once_ever!("help i'm stuck in an");
        assert_once_ever!("assertion error message");
    }

    #[test] #[ignore(cfg(windows))] #[should_fail]
    fn test_assert_once_ever_fail() {
        use task;

        fn f() { assert_once_ever!("if you're seeing this... good!") }

        // linked & watched, naturally
        task::spawn(f);
        task::spawn(f);
    }
}