cxx icon indicating copy to clipboard operation
cxx copied to clipboard

Consider implementing an inherent `write_fmt` method on CxxString

Open dtolnay opened this issue 2 years ago • 1 comments

CxxString implements both std::fmt::Write and std::io::Write. In situations where both traits are in scope, applying write! becomes ambiguous.

This would be fixed by having a write_fmt inherent method, which would take precedence over either of the trait methods.

use cxx::let_cxx_string;
use std::fmt::Write as _;
use std::io::Write as _;

fn main() {
    let_cxx_string!(string = "");
    write!(string, "...").unwrap();
}
error[E0034]: multiple applicable items in scope
 --> src/main.rs:7:5
  |
7 |     write!(string, "...").unwrap();
  |     ^^^^^^^^^^^^^^^^^^^^^ multiple `write_fmt` found
  |
  = note: candidate #1 is defined in an impl of the trait `std::fmt::Write` for the type `Pin<&mut CxxString>`
  = note: candidate #2 is defined in an impl of the trait `std::io::Write` for the type `Pin<&mut CxxString>`
  = note: this error originates in the macro `write` (in Nightly builds, run with -Z macro-backtrace for more info)
help: disambiguate the method for candidate #1
 --> nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/macros/mod.rs:518:9
  |
51|         std::fmt::Write::write_fmt(&mut string, $crate::format_args!($($arg)*))
  |
help: disambiguate the method for candidate #2
 --> nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/macros/mod.rs:518:9
  |
51|         std::io::Write::write_fmt(&mut string, $crate::format_args!($($arg)*))
  |

dtolnay avatar Aug 27 '23 17:08 dtolnay