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

Add recipe to building custom format-like macros #679

Open
nyurik opened this issue Nov 27, 2022 · 1 comment
Open

Add recipe to building custom format-like macros #679

nyurik opened this issue Nov 27, 2022 · 1 comment

Comments

@nyurik
Copy link

nyurik commented Nov 27, 2022

There seem to be a lot of "gotchas" when creating a new macro that accepts format arguments. I propose to add a recipe for such cases. Something like this, although my code might also need some improvements. Points of interest:

  • best way to capture format arguments - comma capturing is actually fairly tricky because foo!("{}", bar) can be auto-replaced with foo("{bar}"), which has no commas.
  • proper way to use those captured arguments
#[macro_export]
macro_rules! err_to_io {
    ($error:ident $(, $arg:expr)* $(,)?) => {
        ::std::io::Error::new(
            ::std::io::ErrorKind::Other,
            ::std::format!("{}: {}", ::std::format_args!($($arg,)+), $error))
    };
}
@AndyGauge
Copy link
Collaborator

seems like a rust by example use, we highlight crates interoperability

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants