aboutsummaryrefslogtreecommitdiff
path: root/src/macros.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/macros.rs')
-rw-r--r--src/macros.rs33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/macros.rs b/src/macros.rs
index 558a4a9..55977a4 100644
--- a/src/macros.rs
+++ b/src/macros.rs
@@ -31,3 +31,36 @@ macro_rules! time {
(result, dt)
}};
}
+
+/// Conditionally perform string formatting.
+///
+/// If `$enabled` is true, then do the formatting and return a `Cow::Owned`.
+///
+/// Otherwise, just return the borrowed (often `'static`) string
+/// `$borrowed`.
+///
+/// When `$enabled` is false, this avoids the overhead of allocating
+/// and writing to a buffer, as well as any overhead or side effects
+/// of the format arguments.
+#[macro_export]
+macro_rules! format_if {
+ ($enabled:expr, $borrowed:expr, $fmt:expr, $($args:expr),*) => {
+ if $enabled {
+ ::std::borrow::Cow::Owned(format!($fmt, $($args),*)) as ::std::borrow::Cow<str>
+ } else {
+ ::std::borrow::Cow::Borrowed($borrowed)
+ }
+ }
+}
+
+/// Generate a test function `$name` which asserts that `$left` and `$right`
+/// are equal.
+#[macro_export]
+macro_rules! test_eq {
+ ($name:ident, $left:expr, $right:expr) => {
+ #[test]
+ fn $name() {
+ assert_eq!($left, $right);
+ }
+ }
+} \ No newline at end of file