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`.
This commit is contained in:
bors 2022-12-16 18:06:10 +00:00
commit 9c07efe84f

View file

@ -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