From: Ian Jackson Date: Sat, 10 Jul 2021 17:29:06 +0000 (+0100) Subject: Move dbgc macro into base X-Git-Tag: otter-0.7.2~154 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=61d3577fdc9d49cc78fb48df3f56961b0f7853c5;p=otter.git Move dbgc macro into base We want it there for hacking about, at least. Signed-off-by: Ian Jackson --- diff --git a/base/misc.rs b/base/misc.rs index c94c3db5..10d9fb1a 100644 --- a/base/misc.rs +++ b/base/misc.rs @@ -105,3 +105,45 @@ macro_rules! if_let { impl 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)),+]) + }; +} diff --git a/base/prelude.rs b/base/prelude.rs index 718cb859..91011f71 100644 --- a/base/prelude.rs +++ b/base/prelude.rs @@ -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; diff --git a/src/prelude.rs b/src/prelude.rs index 138447b1..c420b4b1 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -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; diff --git a/src/utils.rs b/src/utils.rs index ce0b7832..b9650988 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -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)*) => {