Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Itertools::format[_with] docs mention panic within logging macro #941

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Itertools::format[_with] docs mention it can panic if used in loggi…
…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
commit c3f335b2cd0cd30dbbfb0597468a54e61e8de90b
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