From 2c541786cf6f54d55b12dd649f322a4b7af5770c Mon Sep 17 00:00:00 2001 From: Andres Suarez Date: Mon, 28 Nov 2022 11:48:43 -0500 Subject: [PATCH] Implement DerefMut for PathBuf --- library/std/src/path.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/library/std/src/path.rs b/library/std/src/path.rs index af88b9070c189..59d72e54736a8 100644 --- a/library/std/src/path.rs +++ b/library/std/src/path.rs @@ -1724,6 +1724,14 @@ impl ops::Deref for PathBuf { } } +#[stable(feature = "path_buf_deref_mut", since = "CURRENT_RUSTC_VERSION")] +impl ops::DerefMut for PathBuf { + #[inline] + fn deref_mut(&mut self) -> &mut Path { + Path::from_inner_mut(&mut self.inner) + } +} + #[stable(feature = "rust1", since = "1.0.0")] impl Borrow for PathBuf { #[inline] @@ -1976,6 +1984,12 @@ impl Path { unsafe { &*(s.as_ref() as *const OsStr as *const Path) } } + fn from_inner_mut(inner: &mut OsStr) -> &mut Path { + // SAFETY: Path is just a wrapper around OsStr, + // therefore converting &mut OsStr to &mut Path is safe. + unsafe { &mut *(inner as *mut OsStr as *mut Path) } + } + /// Yields the underlying [`OsStr`] slice. /// /// # Examples