aboutsummaryrefslogtreecommitdiff
path: root/src/macros.rs
diff options
context:
space:
mode:
authorMartin Fischer <martin@push-f.com>2021-04-08 14:51:11 +0200
committerMartin Fischer <martin@push-f.com>2021-04-08 15:40:48 +0200
commit6a37a2432efda67aa681338251a0d47d6336f9e3 (patch)
treec29903a58c857297615f72ba3d2367cefcbcdd47 /src/macros.rs
parente0bef0105e0cc64bb610889b6921fd94897431d9 (diff)
drop mac dependency
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