Skip to content

Commit

Permalink
Implement DerefMut for PathBuf
Browse files Browse the repository at this point in the history
  • Loading branch information
zertosh committed Nov 28, 2022
1 parent 8a09420 commit 2c54178
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 @@ -1724,6 +1724,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 @@ -1976,6 +1984,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 2c54178

Please sign in to comment.