Skip to content

Commit c3f335b

Browse files
Itertools::format[_with] docs mention it can panic if used in logging macros
I failed to make the (invisible) macro usable as `tracing::info!` with some private module. Renamed it `tracing_info` instead. I add `should_panic` but checked it panics for the right reason.
1 parent 9f5256f commit c3f335b

File tree

1 file changed

+39
-4
lines changed

1 file changed

+39
-4
lines changed

src/lib.rs

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2326,8 +2326,6 @@ pub trait Itertools: Iterator {
23262326
/// All elements are formatted (any formatting trait)
23272327
/// with `sep` inserted between each element.
23282328
///
2329-
/// **Panics** if the formatter helper is formatted more than once.
2330-
///
23312329
/// ```
23322330
/// use itertools::Itertools;
23332331
///
@@ -2336,6 +2334,26 @@ pub trait Itertools: Iterator {
23362334
/// format!("{:.2}", data.iter().format(", ")),
23372335
/// "1.10, 2.72, -3.00");
23382336
/// ```
2337+
///
2338+
/// # Panics
2339+
/// When the formatter helper is formatted more than once.
2340+
///
2341+
/// ⚠ This can happen unexpectedly and be hard to debug if used in
2342+
/// _macros of some logging frameworks_ like `tracing`! ⚠
2343+
///
2344+
/// ```should_panic
2345+
/// # macro_rules! tracing_info {
2346+
/// # ($s:literal, $arg0:expr) => {
2347+
/// # let arg = $arg0;
2348+
/// # let _1 = format!($s, arg);
2349+
/// # let _2 = format!($s, arg);
2350+
/// # };
2351+
/// # }
2352+
/// use itertools::Itertools;
2353+
///
2354+
/// let data = [1.1, 2.71828, -3.];
2355+
/// tracing_info!("values: {:.2}", data.iter().format(", "));
2356+
/// ```
23392357
fn format(self, sep: &str) -> Format<Self>
23402358
where
23412359
Self: Sized,
@@ -2354,8 +2372,6 @@ pub trait Itertools: Iterator {
23542372
/// Using `&format_args!(...)` is the most versatile way to apply custom
23552373
/// element formatting. The callback can be called multiple times if needed.
23562374
///
2357-
/// **Panics** if the formatter helper is formatted more than once.
2358-
///
23592375
/// ```
23602376
/// use itertools::Itertools;
23612377
///
@@ -2372,8 +2388,27 @@ pub trait Itertools: Iterator {
23722388
/// });
23732389
/// assert_eq!(format!("{}", matrix_formatter),
23742390
/// "1, 2, 3\n4, 5, 6");
2391+
/// ```
23752392
///
2393+
/// # Panics
2394+
/// When the formatter helper is formatted more than once.
2395+
///
2396+
/// ⚠ This can happen unexpectedly and be hard to debug if used in
2397+
/// _macros of some logging frameworks_ like `tracing`! ⚠
2398+
///
2399+
/// ```should_panic
2400+
/// # macro_rules! tracing_info {
2401+
/// # ($s:literal, $arg0:expr) => {
2402+
/// # let arg = $arg0;
2403+
/// # let _1 = format!($s, arg);
2404+
/// # let _2 = format!($s, arg);
2405+
/// # };
2406+
/// # }
2407+
/// use itertools::Itertools;
23762408
///
2409+
/// let data = [1.1, 2.71828, -3.];
2410+
/// let data_formatter = data.iter().format_with(", ", |elt, f| f(&format_args!("{:.2}", elt)));
2411+
/// tracing_info!("values: {:.2}", data_formatter);
23772412
/// ```
23782413
fn format_with<F>(self, sep: &str, format: F) -> FormatWith<Self, F>
23792414
where

0 commit comments

Comments
 (0)