about summary refs log tree commit diff
path: root/library/std/src
diff options
context:
space:
mode:
authorAndres Suarez <zertosh@gmail.com>2022-11-28 11:48:43 -0500
committerAndres Suarez <zertosh@gmail.com>2022-11-28 11:55:49 -0500
commit2c541786cf6f54d55b12dd649f322a4b7af5770c (patch)
treeebd3cbafb215b23bd9cced73562ae5f06fe1e8f8 /library/std/src
parent8a09420ac48658cad726e0a6997687ceac4151e3 (diff)
downloadrust-2c541786cf6f54d55b12dd649f322a4b7af5770c.tar.gz
rust-2c541786cf6f54d55b12dd649f322a4b7af5770c.zip
Implement DerefMut for PathBuf
Diffstat (limited to 'library/std/src')
-rw-r--r--library/std/src/path.rs14
1 files changed, 14 insertions, 0 deletions
diff --git a/library/std/src/path.rs b/library/std/src/path.rs
index af88b9070c1..59d72e54736 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<Path> 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