about summary refs log tree commit diff
path: root/library/std/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-12-16 18:06:10 +0000
committerbors <bors@rust-lang.org>2022-12-16 18:06:10 +0000
commit9c07efe84f28a44f3044237696acc295aa407ee5 (patch)
tree26383502472f7d2d3a4a624a1170b75f304b0572 /library/std/src
parent63b3bac77cece3b4efb3e481ebc9139fedaa0535 (diff)
parent2c541786cf6f54d55b12dd649f322a4b7af5770c (diff)
downloadrust-9c07efe84f28a44f3044237696acc295aa407ee5.tar.gz
rust-9c07efe84f28a44f3044237696acc295aa407ee5.zip
Auto merge of #105018 - zertosh:path_buf_deref_mut, r=dtolnay
Implement DerefMut for PathBuf

Without this, there's no way to get a `&mut Path` from `PathBuf` without
going through `into_boxed_path`. This is relevant now that #105002 adds
`PathBuf::as_mut_os_string` and `Path::as_mut_os_str`.
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 6c957c2fa90..a835b855ddd 100644
--- a/library/std/src/path.rs
+++ b/library/std/src/path.rs
@@ -1748,6 +1748,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]
@@ -2000,6 +2008,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