Skip to content

Commit

Permalink
Implement fmt::Write for OsString
Browse files Browse the repository at this point in the history
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());
```
  • Loading branch information
tbu- committed Jun 9, 2022
1 parent 6dc598a commit 89f4183
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions library/std/src/ffi/os_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down

0 comments on commit 89f4183

Please sign in to comment.