Skip to content

Commit f3dc9fa

Browse files
authored
Rollup merge of #105002 - zertosh:acp-140, r=dtolnay
Add `PathBuf::as_mut_os_string` and `Path::as_mut_os_str` Implements rust-lang/libs-team#140 (tracking issue #105021).
2 parents e40eb22 + 7e93e88 commit f3dc9fa

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

std/src/path.rs

+46
Original file line numberDiff line numberDiff line change
@@ -1463,6 +1463,30 @@ impl PathBuf {
14631463
true
14641464
}
14651465

1466+
/// Yields a mutable reference to the underlying [`OsString`] instance.
1467+
///
1468+
/// # Examples
1469+
///
1470+
/// ```
1471+
/// #![feature(path_as_mut_os_str)]
1472+
/// use std::path::{Path, PathBuf};
1473+
///
1474+
/// let mut path = PathBuf::from("/foo");
1475+
///
1476+
/// path.push("bar");
1477+
/// assert_eq!(path, Path::new("/foo/bar"));
1478+
///
1479+
/// // OsString's `push` does not add a separator.
1480+
/// path.as_mut_os_string().push("baz");
1481+
/// assert_eq!(path, Path::new("/foo/barbaz"));
1482+
/// ```
1483+
#[unstable(feature = "path_as_mut_os_str", issue = "105021")]
1484+
#[must_use]
1485+
#[inline]
1486+
pub fn as_mut_os_string(&mut self) -> &mut OsString {
1487+
&mut self.inner
1488+
}
1489+
14661490
/// Consumes the `PathBuf`, yielding its internal [`OsString`] storage.
14671491
///
14681492
/// # Examples
@@ -1993,6 +2017,28 @@ impl Path {
19932017
&self.inner
19942018
}
19952019

2020+
/// Yields a mutable reference to the underlying [`OsStr`] slice.
2021+
///
2022+
/// # Examples
2023+
///
2024+
/// ```
2025+
/// #![feature(path_as_mut_os_str)]
2026+
/// use std::path::{Path, PathBuf};
2027+
///
2028+
/// let mut path = PathBuf::from("/Foo.TXT").into_boxed_path();
2029+
///
2030+
/// assert_ne!(&*path, Path::new("/foo.txt"));
2031+
///
2032+
/// path.as_mut_os_str().make_ascii_lowercase();
2033+
/// assert_eq!(&*path, Path::new("/foo.txt"));
2034+
/// ```
2035+
#[unstable(feature = "path_as_mut_os_str", issue = "105021")]
2036+
#[must_use]
2037+
#[inline]
2038+
pub fn as_mut_os_str(&mut self) -> &mut OsStr {
2039+
&mut self.inner
2040+
}
2041+
19962042
/// Yields a [`&str`] slice if the `Path` is valid unicode.
19972043
///
19982044
/// This conversion may entail doing a check for UTF-8 validity.

0 commit comments

Comments
 (0)