From 89f41839e4942a051305c62c0ea78e18d9b401f7 Mon Sep 17 00:00:00 2001 From: Tobias Bucher Date: Thu, 9 Jun 2022 14:27:01 +0200 Subject: [PATCH 1/2] Implement `fmt::Write` for `OsString` This allows to format into an `OsString` without unnecessary allocations. E.g. ``` let mut temp_filename = path.into_os_string(); write!(&mut temp_filename, ".tmp.{}", process::id()); ``` --- library/std/src/ffi/os_str.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/library/std/src/ffi/os_str.rs b/library/std/src/ffi/os_str.rs index 247bdd954589c..8ce4c2a601722 100644 --- a/library/std/src/ffi/os_str.rs +++ b/library/std/src/ffi/os_str.rs @@ -615,6 +615,14 @@ impl Hash for OsString { } } +#[stable(feature = "os_string_fmt_write", since = "1.63.0")] +impl fmt::Write for OsString { + fn write_str(&mut self, s: &str) -> fmt::Result { + self.push(s); + Ok(()) + } +} + impl OsStr { /// Coerces into an `OsStr` slice. /// From 3855e868738a847f1e38a6157590aa1a197604ea Mon Sep 17 00:00:00 2001 From: Josh Triplett Date: Sat, 16 Jul 2022 14:36:48 -0700 Subject: [PATCH 2/2] Update `since` version to 1.64 --- library/std/src/ffi/os_str.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/src/ffi/os_str.rs b/library/std/src/ffi/os_str.rs index 8ce4c2a601722..f171f142b4237 100644 --- a/library/std/src/ffi/os_str.rs +++ b/library/std/src/ffi/os_str.rs @@ -615,7 +615,7 @@ impl Hash for OsString { } } -#[stable(feature = "os_string_fmt_write", since = "1.63.0")] +#[stable(feature = "os_string_fmt_write", since = "1.64.0")] impl fmt::Write for OsString { fn write_str(&mut self, s: &str) -> fmt::Result { self.push(s);