Getting the pieces that make up an `std::fmt::Arguments` instance?

Viewed 110

When I have an Arguments instance, is it possible to read the parts that make it up?

It seems like the only thing I can do with it is pass it to format! and friends.

So in the example below, is it possible to read "{} hello {}", 1 and 2?

The reason I ask is that I want to implement a custom fern log format, and I want to determine if any of the strings are valid JSON so that they can go into a separate meta JSON key for JSON-lines logging. This way the JSON data does not need to parsed by the end user from the message.

https://doc.rust-lang.org/std/fmt/struct.Arguments.html

let x: Arguments = format_args!("{} hello {}", 1, 2);

// Can I read back the parts from `x`?
1 Answers

No, Arguments does not expose its internals in a way that can be inspected.

And even if you could access the internals (as seen in the source), the elements are stored in a type-erased form, making it difficult to use beyond running it through its associated formatting function.

Related