1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
/// Creates either [`UnwindContextFunc`] or [`UnwindContextArgs`] wrapper with
/// the given function arguments or scope variables.
///
/// Passed arguments are lazily formatted. The created wrapper takes ownership
/// of the given arguments, so it may be necessary to use value references,
/// clones, or pass the pre-prepared string representation. It also supports the
/// `...` placeholder to show that some values have been omitted.
///
/// There are three forms of this macro:
/// - Create [`UnwindContextFunc`] with an automatically determined function
/// name and the given attributes as function attributes. The arguments do not
/// have to be the real function arguments.
///
/// ```rust
/// use unwind_context::build_unwind_context_data;
///
/// fn func(a: u32, b: String, c: bool) {
/// let _data = build_unwind_context_data!(fn());
/// let _data = build_unwind_context_data!(fn(a, &b, c));
/// let _data = build_unwind_context_data!(fn(a, b.clone(), c));
/// let _data = build_unwind_context_data!(fn(..., c));
/// let _data = build_unwind_context_data!(fn(a, ...));
/// let _data = build_unwind_context_data!(fn(a, ..., c));
/// let _data = build_unwind_context_data!(fn(a, &b, c, "step 1"));
/// // ...
/// }
/// ```
///
/// - Create [`UnwindContextFunc`] with a specific function names and the given
/// attributes as function attributes. Note that only ident-like function
/// names are supported is unquoted. Path names should be enclosed in quotes.
/// The arguments do not have to be the real function arguments.
///
/// ```rust
/// use unwind_context::build_unwind_context_data;
///
/// fn func(a: u32, b: String, c: bool) {
/// let _data = build_unwind_context_data!(fn func());
/// let _data = build_unwind_context_data!(fn func(a, &b, c));
/// let _data = build_unwind_context_data!(fn func(a, b.clone(), c));
/// let _data = build_unwind_context_data!(fn func(..., c));
/// let _data = build_unwind_context_data!(fn func(a, ...));
/// let _data = build_unwind_context_data!(fn func(a, ..., c));
/// let _data = build_unwind_context_data!(fn func(a, &b, c, "step 1"));
/// let _data = build_unwind_context_data!(fn "func"());
/// let _data = build_unwind_context_data!(fn "mod1::mod2::func"());
/// let _data = build_unwind_context_data!(fn "mod1::mod2::func"(a, &b, c));
/// // ...
/// }
/// ```
///
/// - Create [`UnwindContextArgs`] with the given scope attributes.
///
/// ```rust
/// use unwind_context::build_unwind_context_data;
///
/// fn func(a: u32) {
/// let b = a.to_string();
/// let c = a > 100;
/// let _data = build_unwind_context_data!(a, &b, c);
/// let _data = build_unwind_context_data!(a, b.clone(), c);
/// let _data = build_unwind_context_data!(..., c);
/// let _data = build_unwind_context_data!(a, ...);
/// let _data = build_unwind_context_data!(a, ..., c);
/// let _data = build_unwind_context_data!(a, &b, c, "step 1");
/// // ...
/// }
/// ```
///
/// [`UnwindContextFunc`]: crate::UnwindContextFunc
/// [`UnwindContextArgs`]: crate::UnwindContextArgs
#[macro_export]
macro_rules! build_unwind_context_data {
( fn $name:ident ( $( $args:tt )* ) ) => {
$crate::build_unwind_context_data_impl!( @fn ::core::stringify!($name), $($args)* )
};
( fn $name:literal ( $( $args:tt )* ) ) => {
$crate::build_unwind_context_data_impl!( @fn $name, $($args)* )
};
( fn ( $( $args:tt )* ) ) => {
$crate::build_unwind_context_data_impl!( @fn $crate::func_name!(), $($args)* )
};
( $( $vars:tt )* ) => {
$crate::UnwindContextArgs::new(
$crate::build_unwind_context_data_impl!( @args $($vars)* )
)
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! build_unwind_context_data_impl {
( @fn $name:expr, $( $args:tt )* ) => {
$crate::UnwindContextFunc::new(
$name,
$crate::build_unwind_context_data_impl!( @args $($args)* )
)
};
( @args ... $(, $( $args:tt )* )? ) => {
(
$crate::UnwindContextArg::new( None, $crate::NonExhaustiveMarker ),
$crate::build_unwind_context_data_impl!( @args $( $($args)* )? ),
)
};
( @args $value:literal $(, $( $args:tt )* )? ) => {
(
$crate::UnwindContextArg::new( None, $value ),
$crate::build_unwind_context_data_impl!( @args $( $($args)* )? ),
)
};
( @args $arg:expr $(, $( $args:tt )* )? ) => {
(
$crate::UnwindContextArg::new( Some(::core::stringify!($arg)), $arg ),
$crate::build_unwind_context_data_impl!( @args $( $($args)* )? ),
)
};
( @args ) => {
()
};
}
#[cfg(test)]
mod tests {
use core::fmt::Debug;
use crate::test_util::buf_fmt;
#[allow(clippy::similar_names)]
#[test]
fn test_unwind_context_data() {
fn inner_context1(foo: i32, bar: &str) -> impl '_ + Debug {
build_unwind_context_data!(fn(foo, bar))
}
fn inner_context2(foo: i32, bar: &str, _extra_data: ()) -> impl '_ + Debug {
build_unwind_context_data!(fn(foo, bar, ...))
}
let mut buffer = [0; 128];
let foo = 123;
let bar = "value";
let context = build_unwind_context_data!(foo);
let formatted = buf_fmt(&mut buffer, format_args!("{context:?}")).unwrap();
assert_eq!(formatted, "foo: 123");
let context = build_unwind_context_data!(foo, 234);
let formatted = buf_fmt(&mut buffer, format_args!("{context:?}")).unwrap();
assert_eq!(formatted, "foo: 123, 234");
let context = build_unwind_context_data!(foo, 234, bar);
let formatted = buf_fmt(&mut buffer, format_args!("{context:?}")).unwrap();
assert_eq!(formatted, "foo: 123, 234, bar: \"value\"");
let context = build_unwind_context_data!(fn func(foo, 234, bar));
let formatted = buf_fmt(&mut buffer, format_args!("{context:?}")).unwrap();
assert_eq!(formatted, "fn func(foo: 123, 234, bar: \"value\")");
let context = build_unwind_context_data!(fn "mod::func"(foo, 234, bar));
let formatted = buf_fmt(&mut buffer, format_args!("{context:?}")).unwrap();
assert_eq!(formatted, "fn mod::func(foo: 123, 234, bar: \"value\")");
let context = build_unwind_context_data!(fn func(..., foo, 234, bar));
let formatted = buf_fmt(&mut buffer, format_args!("{context:?}")).unwrap();
assert_eq!(formatted, "fn func(..., foo: 123, 234, bar: \"value\")");
let context = build_unwind_context_data!(fn func(foo, ..., 234, bar));
let formatted = buf_fmt(&mut buffer, format_args!("{context:?}")).unwrap();
assert_eq!(formatted, "fn func(foo: 123, ..., 234, bar: \"value\")");
let context = build_unwind_context_data!(fn func(foo, 234, bar, ...));
let formatted = buf_fmt(&mut buffer, format_args!("{context:?}")).unwrap();
assert_eq!(formatted, "fn func(foo: 123, 234, bar: \"value\", ...)");
let context = inner_context1(foo, bar);
let formatted = buf_fmt(&mut buffer, format_args!("{context:?}")).unwrap();
assert!(formatted.starts_with("fn "));
assert!(formatted.contains("inner_context1"));
assert!(formatted.ends_with("(foo: 123, bar: \"value\")"));
let context = inner_context2(foo, bar, ());
let formatted = buf_fmt(&mut buffer, format_args!("{context:?}")).unwrap();
assert!(formatted.starts_with("fn "));
assert!(formatted.contains("inner_context2"));
assert!(formatted.ends_with("(foo: 123, bar: \"value\", ...)"));
}
}