Skip to content

Commit 9c07efe

Browse files
committed
Auto merge of rust-lang#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 rust-lang#105002 adds `PathBuf::as_mut_os_string` and `Path::as_mut_os_str`.
2 parents 63b3bac + 2c54178 commit 9c07efe

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

library/std/src/path.rs

+14
Original file line numberDiff line numberDiff line change
@@ -1748,6 +1748,14 @@ impl ops::Deref for PathBuf {
17481748
}
17491749
}
17501750

1751+
#[stable(feature = "path_buf_deref_mut", since = "CURRENT_RUSTC_VERSION")]
1752+
impl ops::DerefMut for PathBuf {
1753+
#[inline]
1754+
fn deref_mut(&mut self) -> &mut Path {
1755+
Path::from_inner_mut(&mut self.inner)
1756+
}
1757+
}
1758+
17511759
#[stable(feature = "rust1", since = "1.0.0")]
17521760
impl Borrow<Path> for PathBuf {
17531761
#[inline]
@@ -2000,6 +2008,12 @@ impl Path {
20002008
unsafe { &*(s.as_ref() as *const OsStr as *const Path) }
20012009
}
20022010

2011+
fn from_inner_mut(inner: &mut OsStr) -> &mut Path {
2012+
// SAFETY: Path is just a wrapper around OsStr,
2013+
// therefore converting &mut OsStr to &mut Path is safe.
2014+
unsafe { &mut *(inner as *mut OsStr as *mut Path) }
2015+
}
2016+
20032017
/// Yields the underlying [`OsStr`] slice.
20042018
///
20052019
/// # Examples

0 commit comments

Comments
 (0)