Skip to content

Commit

Permalink
Auto merge of rust-lang#105018 - zertosh:path_buf_deref_mut, r=dtolnay
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
bors committed Dec 16, 2022
2 parents 63b3bac + 2c54178 commit 9c07efe
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions library/std/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9c07efe

Please sign in to comment.