diff options
| author | Tobias Bucher <tobiasbucher5991@gmail.com> | 2022-06-09 14:27:01 +0200 |
|---|---|---|
| committer | Tobias Bucher <tobiasbucher5991@gmail.com> | 2022-06-09 14:27:01 +0200 |
| commit | 89f41839e4942a051305c62c0ea78e18d9b401f7 (patch) | |
| tree | 7cd09480bdd1ef4ea8292304d497db2c26a6b82b | |
| parent | 6dc598a01b8f7619826b7152be5162e6d37a1754 (diff) | |
| download | rust-89f41839e4942a051305c62c0ea78e18d9b401f7.tar.gz rust-89f41839e4942a051305c62c0ea78e18d9b401f7.zip | |
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());
```
| -rw-r--r-- | library/std/src/ffi/os_str.rs | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/library/std/src/ffi/os_str.rs b/library/std/src/ffi/os_str.rs index 247bdd95458..8ce4c2a6017 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. /// |
