chiark / gitweb /
Move dbgc macro into base
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 10 Jul 2021 17:29:06 +0000 (18:29 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 10 Jul 2021 18:20:09 +0000 (19:20 +0100)
We want it there for hacking about, at least.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
base/misc.rs
base/prelude.rs
src/prelude.rs
src/utils.rs

index c94c3db5cb9c02545123c26b0035fb520000d064..10d9fb1aba18580a9e7a6dd5a7417708cb4a5158 100644 (file)
@@ -105,3 +105,45 @@ macro_rules! if_let {
 impl<T:Debug> T {
   fn to_debug(&self) -> String { format!("{:?}", self) }
 }
+
+pub fn dbgc_helper(file: &'static str, line: u32,
+                   values: &[(&'static str, &dyn Debug)]) {
+  let buf = (||{
+    let mut buf = String::new();
+    write!(buf, "[{}:{}]", file, line)?;
+    for (s, v) in values.iter() {
+      write!(buf, " {}={:?}", s, v)?;
+    }
+    write!(buf, "\n")?;
+    Ok::<_,fmt::Error>(buf)
+  })();
+  let buf = buf.unwrap_or_else(
+    |e| format!("error formatting for dbgc! {}\n", e));
+  eprint!("{}", buf);
+}
+
+#[macro_export]
+macro_rules! dbgc {
+    // NOTE: We cannot use `concat!` to make a static string as a format argument
+    // of `eprintln!` because `file!` could contain a `{` or
+    // `$val` expression could be a block (`{ .. }`), in which case the `eprintln!`
+    // will be malformed.
+    () => {
+      dbgc_helper(std::file!(), std::line!(), &[])
+    };
+    ($val:expr $(,)?) => {
+        // Use of `match` here is intentional because it affects the lifetimes
+        // of temporaries - https://stackoverflow.com/a/48732525/1063961
+        match $val {
+            tmp => {
+                dbgc_helper(std::file!(), std::line!(),
+                            &[(std::stringify!($val), &tmp)]);
+                tmp
+            }
+        }
+    };
+    ($($val:expr),+ $(,)?) => {
+      dbgc_helper(std::file!(), std::line!(),
+                  &[$((std::stringify!($val), &$val)),+])
+    };
+}
index 718cb859274deb45a814a082cc46eb2a7dec8210..91011f71c8b7d14fa68a9cb853a4c86ea3f1ccff 100644 (file)
@@ -5,7 +5,7 @@
 pub use std::borrow::Borrow;
 pub use std::cmp::{max, Ordering};
 pub use std::convert::{TryFrom, TryInto};
-pub use std::fmt::{self, Debug, Display, Formatter};
+pub use std::fmt::{self, Debug, Display, Formatter, Write as _};
 pub use std::hash::{Hash, Hasher};
 pub use std::iter;
 pub use std::mem;
index 138447b19b79cd28d7639ab1301325ee95052199..c420b4b1270c16919e5e574a6acaca022832a579 100644 (file)
@@ -115,9 +115,9 @@ pub use otter_base::geometry::{self,Coord,Pos,PosC,Rect,RectC};
 pub use otter_base::geometry::{CoordinateOverflow,Region};
 pub use otter_base::zcoord::{self, ZCoord};
 pub use otter_base::misc as base_misc;
+pub use otter_base::dbgc;
 pub use base_misc::*;
 
-pub use crate::dbgc;
 pub use crate::{deref_to_field, deref_to_field_mut};
 pub use crate::ensure_eq;
 pub use crate::format_by_fmt_hex;
index ce0b78328e266123f4ead58fde95e3eb650cb87e..b9650988dd1fa16a1428eedff64e0e6ef225855f 100644 (file)
@@ -376,48 +376,6 @@ fn matches_doesnot_test() {
   );
 }
 
-pub fn dbgc_helper(file: &'static str, line: u32,
-                   values: &[(&'static str, &dyn Debug)]) {
-  let buf = (||{
-    let mut buf = String::new();
-    write!(buf, "[{}:{}]", file, line)?;
-    for (s, v) in values.iter() {
-      write!(buf, " {}={:?}", s, v)?;
-    }
-    write!(buf, "\n")?;
-    Ok::<_,fmt::Error>(buf)
-  })();
-  let buf = buf.unwrap_or_else(
-    |e| format!("error formatting for dbgc! {}\n", e));
-  eprint!("{}", buf);
-}
-
-#[macro_export]
-macro_rules! dbgc {
-    // NOTE: We cannot use `concat!` to make a static string as a format argument
-    // of `eprintln!` because `file!` could contain a `{` or
-    // `$val` expression could be a block (`{ .. }`), in which case the `eprintln!`
-    // will be malformed.
-    () => {
-      dbgc_helper(std::file!(), std::line!(), &[])
-    };
-    ($val:expr $(,)?) => {
-        // Use of `match` here is intentional because it affects the lifetimes
-        // of temporaries - https://stackoverflow.com/a/48732525/1063961
-        match $val {
-            tmp => {
-                dbgc_helper(std::file!(), std::line!(),
-                            &[(std::stringify!($val), &tmp)]);
-                tmp
-            }
-        }
-    };
-    ($($val:expr),+ $(,)?) => {
-      dbgc_helper(std::file!(), std::line!(),
-                  &[$((std::stringify!($val), &$val)),+])
-    };
-}
-
 #[macro_export]
 macro_rules! trace_dbg {
   ($msg:expr $(,$val:expr)*) => {