`fn unparse_expr` -> `UnparseExpr::new` by ShaharNaveh · Pull Request #6121 · RustPython/RustPython
write!(self, ...) requires implementing fmt::Write for Unparser (compile error otherwise).
write! calls target std::fmt::Write::write_fmt, so the destination must implement fmt::Write. Unparser currently does not, which will fail to compile.
Apply this diff to implement fmt::Write and forward to the inner formatter:
@@
impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
@@
fn write_fmt(&mut self, f: fmt::Arguments<'_>) -> fmt::Result {
self.f.write_fmt(f)
}
}
+impl<'a, 'b, 'c> fmt::Write for Unparser<'a, 'b, 'c> {
+ fn write_str(&mut self, s: &str) -> fmt::Result {
+ self.f.write_str(s)
+ }
+ fn write_char(&mut self, c: char) -> fmt::Result {
+ self.f.write_char(c)
+ }
+}Alternatively, replace each write!(self, "...", ...) with self.write_fmt(format_args!("...", ...))?, but the trait impl keeps call sites cleaner.
Also applies to: 197-199, 275-277, 285-289, 483-486, 492-494
🤖 Prompt for AI Agents
In compiler/codegen/src/unparse.rs around lines 172-173 (also applies to
197-199, 275-277, 285-289, 483-486, 492-494): the code uses write!(self, ...)
but Unparser does not implement std::fmt::Write so compilation fails; implement
the fmt::Write trait for Unparser and forward its methods to the inner
formatter/collector (i.e., implement fn write_str(&mut self, s: &str) ->
fmt::Result and any other required fmt::Write methods by delegating to the inner
formatter or its write methods), so existing write! call sites compile unchanged
(alternatively replace write! calls with self.write_fmt(format_args!(...)) at
each site if you prefer not to add the trait impl).