From a7886506ad109a1d8c823fd3f32ecab2c6dfca3c Mon Sep 17 00:00:00 2001 From: Josh Triplett Date: Wed, 24 Aug 2022 14:41:22 +0200 Subject: [PATCH 1/4] Remove some documentation duplicated between `writeln!` and `write!` `writeln!` already includes a reference to `write!` for more information, so remove duplicated information. --- library/core/src/macros/mod.rs | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/library/core/src/macros/mod.rs b/library/core/src/macros/mod.rs index 3a115a8b8b6c6..0d4cb990d1e97 100644 --- a/library/core/src/macros/mod.rs +++ b/library/core/src/macros/mod.rs @@ -526,25 +526,6 @@ macro_rules! write { /// Ok(()) /// } /// ``` -/// -/// A module can import both `std::fmt::Write` and `std::io::Write` and call `write!` on objects -/// implementing either, as objects do not typically implement both. However, the module must -/// import the traits qualified so their names do not conflict: -/// -/// ``` -/// use std::fmt::Write as FmtWrite; -/// use std::io::Write as IoWrite; -/// -/// fn main() -> Result<(), Box> { -/// let mut s = String::new(); -/// let mut v = Vec::new(); -/// -/// writeln!(&mut s, "{} {}", "abc", 123)?; // uses fmt::Write::write_fmt -/// writeln!(&mut v, "s = {:?}", s)?; // uses io::Write::write_fmt -/// assert_eq!(v, b"s = \"abc 123\\n\"\n"); -/// Ok(()) -/// } -/// ``` #[macro_export] #[stable(feature = "rust1", since = "1.0.0")] #[cfg_attr(not(test), rustc_diagnostic_item = "writeln_macro")] From 3c8618fd82baa24523246f1380952fc7f42ec30a Mon Sep 17 00:00:00 2001 From: Josh Triplett Date: Wed, 24 Aug 2022 14:42:25 +0200 Subject: [PATCH 2/4] Update `write!` docs: can now import traits as `_` to avoid conflicts --- library/core/src/macros/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/core/src/macros/mod.rs b/library/core/src/macros/mod.rs index 0d4cb990d1e97..8ead64808ca83 100644 --- a/library/core/src/macros/mod.rs +++ b/library/core/src/macros/mod.rs @@ -460,8 +460,8 @@ macro_rules! r#try { /// import the traits qualified so their names do not conflict: /// /// ``` -/// use std::fmt::Write as FmtWrite; -/// use std::io::Write as IoWrite; +/// use std::fmt::Write as _; +/// use std::io::Write as _; /// /// fn main() -> Result<(), Box> { /// let mut s = String::new(); From 589db1f73af1837d633024c297abbe36074d4aca Mon Sep 17 00:00:00 2001 From: Josh Triplett Date: Wed, 24 Aug 2022 14:51:56 +0200 Subject: [PATCH 3/4] Expand example to show how to implement qualified trait names --- library/core/src/macros/mod.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/library/core/src/macros/mod.rs b/library/core/src/macros/mod.rs index 8ead64808ca83..60d809c54899f 100644 --- a/library/core/src/macros/mod.rs +++ b/library/core/src/macros/mod.rs @@ -474,6 +474,23 @@ macro_rules! r#try { /// } /// ``` /// +/// If you also need the trait names themselves, such as to implement one or both on your types, +/// import the containing module and then name them with a prefix: +/// +/// ``` +/// # #![allow(unused_imports)] +/// use std::fmt::{self, Write as _}; +/// use std::io::{self, Write as _}; +/// +/// struct Example; +/// +/// impl fmt::Write for Example { +/// fn write_str(&mut self, _s: &str) -> core::fmt::Result { +/// unimplemented!(); +/// } +/// } +/// ``` +/// /// Note: This macro can be used in `no_std` setups as well. /// In a `no_std` setup you are responsible for the implementation details of the components. /// From ae937cc347b1f1290a9a8208d1896ed366247109 Mon Sep 17 00:00:00 2001 From: Josh Triplett Date: Thu, 25 Aug 2022 00:22:40 +0200 Subject: [PATCH 4/4] Clarify comment to fit `as _` better --- library/core/src/macros/mod.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/library/core/src/macros/mod.rs b/library/core/src/macros/mod.rs index 60d809c54899f..0bd9c8e9acfcb 100644 --- a/library/core/src/macros/mod.rs +++ b/library/core/src/macros/mod.rs @@ -457,7 +457,8 @@ macro_rules! r#try { /// /// A module can import both `std::fmt::Write` and `std::io::Write` and call `write!` on objects /// implementing either, as objects do not typically implement both. However, the module must -/// import the traits qualified so their names do not conflict: +/// avoid conflict between the trait names, such as by importing them as `_` or otherwise renaming +/// them: /// /// ``` /// use std::fmt::Write as _;