Implement DerefMut for PathBuf

This commit is contained in:
Andres Suarez 2022-11-28 11:48:43 -05:00
parent 8a09420ac4
commit 2c541786cf

View file

@ -1724,6 +1724,14 @@ fn deref(&self) -> &Path {
}
}
#[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 @@ pub fn new<S: AsRef<OsStr> + ?Sized>(s: &S) -> &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