Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PathBuf::as_mut_os_string and Path::as_mut_os_str #105002

Merged
merged 1 commit into from
Nov 30, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions library/std/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1463,6 +1463,30 @@ impl PathBuf {
true
}

/// Yields a mutable reference to the underlying [`OsString`] instance.
///
/// # Examples
///
/// ```
/// #![feature(path_as_mut_os_str)]
/// use std::path::{Path, PathBuf};
///
/// let mut path = PathBuf::from("/foo");
///
/// path.push("bar");
/// assert_eq!(path, Path::new("/foo/bar"));
///
/// // OsString's `push` does not add a separator.
/// path.as_mut_os_string().push("baz");
/// assert_eq!(path, Path::new("/foo/barbaz"));
/// ```
#[unstable(feature = "path_as_mut_os_str", issue = "105021")]
#[must_use]
#[inline]
pub fn as_mut_os_string(&mut self) -> &mut OsString {
&mut self.inner
}

/// Consumes the `PathBuf`, yielding its internal [`OsString`] storage.
///
/// # Examples
Expand Down Expand Up @@ -1993,6 +2017,28 @@ impl Path {
&self.inner
}

/// Yields a mutable reference to the underlying [`OsStr`] slice.
///
/// # Examples
///
/// ```
/// #![feature(path_as_mut_os_str)]
/// use std::path::{Path, PathBuf};
///
/// let mut path = PathBuf::from("/Foo.TXT").into_boxed_path();
///
/// assert_ne!(&*path, Path::new("/foo.txt"));
///
/// path.as_mut_os_str().make_ascii_lowercase();
/// assert_eq!(&*path, Path::new("/foo.txt"));
/// ```
#[unstable(feature = "path_as_mut_os_str", issue = "105021")]
#[must_use]
#[inline]
pub fn as_mut_os_str(&mut self) -> &mut OsStr {
&mut self.inner
}

/// Yields a [`&str`] slice if the `Path` is valid unicode.
///
/// This conversion may entail doing a check for UTF-8 validity.
Expand Down