Skip to content

Commit

Permalink
Itertools::format[_with] docs mention it can panic if used in loggi…
Browse files Browse the repository at this point in the history
…ng 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.
  • Loading branch information
Philippe-Cholet committed Jun 18, 2024
1 parent 9f5256f commit c3f335b
Showing 1 changed file with 39 additions and 4 deletions.
43 changes: 39 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2326,8 +2326,6 @@ pub trait Itertools: Iterator {
/// All elements are formatted (any formatting trait)
/// with `sep` inserted between each element.
///
/// **Panics** if the formatter helper is formatted more than once.
///
/// ```
/// use itertools::Itertools;
///
Expand All @@ -2336,6 +2334,26 @@ pub trait Itertools: Iterator {
/// format!("{:.2}", data.iter().format(", ")),
/// "1.10, 2.72, -3.00");
/// ```
///
/// # Panics
/// When the formatter helper is formatted more than once.
///
/// ⚠ This can happen unexpectedly and be hard to debug if used in
/// _macros of some logging frameworks_ like `tracing`! ⚠
///
/// ```should_panic
/// # macro_rules! tracing_info {
/// # ($s:literal, $arg0:expr) => {
/// # let arg = $arg0;
/// # let _1 = format!($s, arg);
/// # let _2 = format!($s, arg);
/// # };
/// # }
/// use itertools::Itertools;
///
/// let data = [1.1, 2.71828, -3.];
/// tracing_info!("values: {:.2}", data.iter().format(", "));
/// ```
fn format(self, sep: &str) -> Format<Self>
where
Self: Sized,
Expand All @@ -2354,8 +2372,6 @@ pub trait Itertools: Iterator {
/// Using `&format_args!(...)` is the most versatile way to apply custom
/// element formatting. The callback can be called multiple times if needed.
///
/// **Panics** if the formatter helper is formatted more than once.
///
/// ```
/// use itertools::Itertools;
///
Expand All @@ -2372,8 +2388,27 @@ pub trait Itertools: Iterator {
/// });
/// assert_eq!(format!("{}", matrix_formatter),
/// "1, 2, 3\n4, 5, 6");
/// ```
///
/// # Panics
/// When the formatter helper is formatted more than once.
///
/// ⚠ This can happen unexpectedly and be hard to debug if used in
/// _macros of some logging frameworks_ like `tracing`! ⚠
///
/// ```should_panic
/// # macro_rules! tracing_info {
/// # ($s:literal, $arg0:expr) => {
/// # let arg = $arg0;
/// # let _1 = format!($s, arg);
/// # let _2 = format!($s, arg);
/// # };
/// # }
/// use itertools::Itertools;
///
/// let data = [1.1, 2.71828, -3.];
/// let data_formatter = data.iter().format_with(", ", |elt, f| f(&format_args!("{:.2}", elt)));
/// tracing_info!("values: {:.2}", data_formatter);
/// ```
fn format_with<F>(self, sep: &str, format: F) -> FormatWith<Self, F>
where
Expand Down

0 comments on commit c3f335b

Please sign in to comment.