I want to make write!
work on my own struct implementing fmt::Write
, and I don't have any heap allocation. To do so, I'd need to implement fmt::Write::write_fmt(&mut self, args: fmt::Arguments<'_>) -> fmt::Result
However, this means that I need to access the data in a fmt::Arguments<'_>
without using &args.to_string()
, as .to_string()
does not exist without heap allocation.
There is Arguments::as_str
, however it doesn't work as soon as I need to format elements as i32
.
Is there a way to get the arguments' strings without using .to_string()
?
An
Arguments<'_>
contains a pre-parsed argument string and the arguments passed with it. You cannot break it up or mess around with the internals because that would be unsafe, so the only reasonable things you can do with it are:core::fmt::write
(orstd::fmt::format
, but that returns aString
)write!
) along with a"{}"
or"{:?}"
format string.The second option doesn't work in your case because you're implementing
write_fmt
, andwrite!
callswrite_fmt
, so this would likely lead to infinite recursion. But you can still just callwrite(&mut self, args)
.write
's implementation only useswrite_str
, notwrite_fmt
.In fact, the provided implementation of
Write::write_fmt
does exactly this:Since this already exists, and there's no reasonable alternative implementation, you probably should not override
write_fmt
at all unless you have very specific requirements.