Browse Source

Amend nilset's commit

ticki 8 years ago
parent
commit
638e861b65
4 changed files with 99 additions and 63 deletions
  1. 2 0
      Cargo.toml
  2. 0 60
      src/assertions.rs
  3. 93 0
      src/debug.rs
  4. 4 3
      src/lib.rs

+ 2 - 0
Cargo.toml

@@ -27,8 +27,10 @@ codegen-units = 1
 
 [features]
 default = ["allocator", "clippy"]
+
 allocator = []
 debug_tools = []
+internals = []
 security = []
 unsafe_no_brk_lock = []
 unsafe_no_mutex_lock = []

+ 0 - 60
src/assertions.rs

@@ -1,60 +0,0 @@
-use core::fmt::{Write, Result};
-
-pub struct LibcWriter {
-    pub fd: i32,
-}
-
-extern "C" {
-    fn write(fd: i32, buff: *const u8, size: usize) -> isize;
-}
-
-impl Write for LibcWriter {
-    fn write_str(&mut self, s: &str) -> Result {
-        unsafe { write(self.fd, s.as_ptr(), s.len()) };
-        Ok(()) //TODO write until done and return Error on error code
-    }
-}
-
-macro_rules! assert {
-    ($e:expr) => {{
-        if !$e {
-            use core::intrinsics;
-            use assertions;
-            use core::fmt::Write;
-
-            let _ = writeln!(assertions::LibcWriter{fd: 2}, "assertion failed at {}:{}: {}", file!(), line!(),stringify!($e));
-            #[allow(unused_unsafe)]
-            unsafe{ intrinsics::abort()}
-        }
-    }};
-    ($e:expr, $( $arg:tt )+) => {{
-        if !$e {
-            use core::intrinsics;
-            use assertions;
-            use core::fmt::Write;
-
-            let _ = writeln!(assertions::LibcWriter{fd: 2}, "assertion failed at {}:{}: {}", file!(), line!(), stringify!($e));
-            let _ = writeln!(assertions::LibcWriter{fd: 2}, $($arg)+);
-            #[allow(unused_unsafe)]
-            unsafe{ intrinsics::abort()}
-        }
-    }}
-}
-
-macro_rules! debug_assert {
-    ($($arg:tt)*) => (if cfg!(debug_assertions) { assert!($($arg)*); })
-}
-
-macro_rules! assert_eq {
-    ($left:expr , $right:expr) => ({
-        match (&$left, &$right) {
-            (left_val, right_val) => {
-                assert!(*left_val == *right_val, "(left: `{:?}`, right: `{:?}`)", left_val, right_val)
-            }
-        }
-    })
-}
-
-macro_rules! debug_assert {
-    ($($arg:tt)*) => (if cfg!(debug_assertions) { assert!($($arg)*); })
-}

+ 93 - 0
src/debug.rs

@@ -0,0 +1,93 @@
+//! Debugging primitives.
+
+use core::fmt;
+
+extern {
+    fn write(fd: i32, buff: *const u8, size: usize) -> isize;
+}
+
+/// A direct writer.
+///
+/// This writes directly to some file descriptor through the `write` symbol.
+pub struct Writer {
+    /// The file descriptor.
+    fd: i32,
+}
+
+impl Writer {
+    pub fn stderr() -> Writer {
+        Writer {
+            fd: 2,
+        }
+    }
+}
+
+impl fmt::Write for Writer {
+    fn write_str(&mut self, s: &str) -> fmt::Result {
+        if unsafe { write(self.fd, s.as_ptr(), s.len()) } == !0 {
+            Err(fmt::Error)
+        } else { Ok(()) }
+    }
+}
+
+/// Make a runtime assertion.
+///
+/// The only way it differs from the one provided by `libcore` is the panicking strategy, which
+/// allows for aborting, non-allocating panics when running the tests.
+#[macro_export]
+macro_rules! assert {
+    ($e:expr) => {{
+        use debug;
+        use core::intrinsics;
+        use core::fmt::Write;
+
+        if !$e {
+            let _ = write!(debug::Writer::stderr(), "assertion failed at {}:{}: {}", file!(),
+                           line!(), stringify!($e));
+
+            #[allow(unused_unsafe)]
+            unsafe { intrinsics::abort() }
+        }
+    }};
+    ($e:expr, $( $arg:expr ),*) => {{
+        use debug;
+        use core::intrinsics;
+        use core::fmt::Write;
+
+        if !$e {
+            let _ = write!(debug::Writer::stderr(), "assertion failed at {}:{}: `{}` - ", file!(),
+                           line!(), stringify!($e));
+            let _ = writeln!(debug::Writer::stderr(), $( $arg ),*);
+
+            #[allow(unused_unsafe)]
+            unsafe { intrinsics::abort() }
+        }
+    }}
+}
+
+/// Make a runtime assertion in debug mode.
+///
+/// The only way it differs from the one provided by `libcore` is the panicking strategy, which
+/// allows for aborting, non-allocating panics when running the tests.
+#[macro_export]
+macro_rules! debug_assert {
+    ($($arg:tt)*) => {{
+        #[cfg(debug_assertions)]
+        assert!($($arg)*);
+    }}
+}
+
+/// Make a runtime equality assertion in debug mode.
+///
+/// The only way it differs from the one provided by `libcore` is the panicking strategy, which
+/// allows for aborting, non-allocating panics when running the tests.
+#[macro_export]
+macro_rules! assert_eq {
+    ($left:expr, $right:expr) => ({
+        // We evaluate _once_.
+        let left = &$left;
+        let right = &$right;
+
+        assert!(left == right, "(left: `{:?}`, right: `{:?}`)", left, right)
+    })
+}

+ 4 - 3
src/lib.rs

@@ -4,8 +4,8 @@
 //! efficiency.
 
 #![cfg_attr(feature = "allocator", allocator)]
-#![cfg_attr(feature="clippy", feature(plugin))]
-#![cfg_attr(feature="clippy", plugin(clippy))]
+#![cfg_attr(feature = "clippy", feature(plugin))]
+#![cfg_attr(feature = "clippy", plugin(clippy))]
 
 #![no_std]
 
@@ -13,8 +13,9 @@
            nonzero, optin_builtin_traits, type_ascription)]
 #![warn(missing_docs)]
 
+#[cfg(feature = "internals")]
 #[macro_use]
-mod assertions;
+mod debug;
 
 mod block;
 mod bookkeeper;