Skip to content

Commit 12bccb4

Browse files
Rollup merge of rust-lang#58539 - aaronstillwell:master, r=Mark-Simulacrum
Add alias methods to PathBuf for underlying OsString (rust-lang#58234) Implemented the following methods on PathBuf which forward to the underlying OsString. - capacity - with_capacity - clear - reserve - reserve_exact - shrink_to_fit - shrink_to These methods have been documented with reference to the original docs for `OsString`. @Mark-Simulacrum please let me know if you feel this does not suffice. Further, I've not yet included tests for these definitions - please advise on how comprehensive tests need to be for methods such as these that simply alias other (already tested) methods. (This PR addresses issue rust-lang#58234)
2 parents 2fabaab + 35d8c44 commit 12bccb4

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

src/libstd/path.rs

+80
Original file line numberDiff line numberDiff line change
@@ -1145,6 +1145,32 @@ impl PathBuf {
11451145
PathBuf { inner: OsString::new() }
11461146
}
11471147

1148+
/// Creates a new `PathBuf` with a given capacity used to create the
1149+
/// internal [`OsString`]. See [`with_capacity`] defined on [`OsString`].
1150+
///
1151+
/// # Examples
1152+
///
1153+
/// ```
1154+
/// use std::path::PathBuf;
1155+
///
1156+
/// let path = PathBuf::with_capacity(10);
1157+
/// let capacity = path.capacity();
1158+
///
1159+
/// // This push is done without reallocating
1160+
/// path.push(r"C:\");
1161+
///
1162+
/// assert_eq!(capacity, path.capacity());
1163+
/// ```
1164+
///
1165+
/// [`with_capacity`]: ../ffi/struct.OsString.html#method.with_capacity
1166+
/// [`OsString`]: ../ffi/struct.OsString.html
1167+
#[unstable(feature = "path_buf_capacity", issue = "58234")]
1168+
pub fn with_capacity(capacity: usize) -> PathBuf {
1169+
PathBuf {
1170+
inner: OsString::with_capacity(capacity)
1171+
}
1172+
}
1173+
11481174
/// Coerces to a [`Path`] slice.
11491175
///
11501176
/// [`Path`]: struct.Path.html
@@ -1373,6 +1399,60 @@ impl PathBuf {
13731399
let rw = Box::into_raw(self.inner.into_boxed_os_str()) as *mut Path;
13741400
unsafe { Box::from_raw(rw) }
13751401
}
1402+
1403+
/// Invokes [`capacity`] on the underlying instance of [`OsString`].
1404+
///
1405+
/// [`capacity`]: ../ffi/struct.OsString.html#method.capacity
1406+
/// [`OsString`]: ../ffi/struct.OsString.html
1407+
#[unstable(feature = "path_buf_capacity", issue = "58234")]
1408+
pub fn capacity(&self) -> usize {
1409+
self.inner.capacity()
1410+
}
1411+
1412+
/// Invokes [`clear`] on the underlying instance of [`OsString`].
1413+
///
1414+
/// [`clear`]: ../ffi/struct.OsString.html#method.clear
1415+
/// [`OsString`]: ../ffi/struct.OsString.html
1416+
#[unstable(feature = "path_buf_capacity", issue = "58234")]
1417+
pub fn clear(&mut self) {
1418+
self.inner.clear()
1419+
}
1420+
1421+
/// Invokes [`reserve`] on the underlying instance of [`OsString`].
1422+
///
1423+
/// [`reserve`]: ../ffi/struct.OsString.html#method.reserve
1424+
/// [`OsString`]: ../ffi/struct.OsString.html
1425+
#[unstable(feature = "path_buf_capacity", issue = "58234")]
1426+
pub fn reserve(&mut self, additional: usize) {
1427+
self.inner.reserve(additional)
1428+
}
1429+
1430+
/// Invokes [`reserve_exact`] on the underlying instance of [`OsString`].
1431+
///
1432+
/// [`reserve_exact`]: ../ffi/struct.OsString.html#method.reserve_exact
1433+
/// [`OsString`]: ../ffi/struct.OsString.html
1434+
#[unstable(feature = "path_buf_capacity", issue = "58234")]
1435+
pub fn reserve_exact(&mut self, additional: usize) {
1436+
self.inner.reserve_exact(additional)
1437+
}
1438+
1439+
/// Invokes [`shrink_to_fit`] on the underlying instance of [`OsString`].
1440+
///
1441+
/// [`shrink_to_fit`]: ../ffi/struct.OsString.html#method.shrink_to_fit
1442+
/// [`OsString`]: ../ffi/struct.OsString.html
1443+
#[unstable(feature = "path_buf_capacity", issue = "58234")]
1444+
pub fn shrink_to_fit(&mut self) {
1445+
self.inner.shrink_to_fit()
1446+
}
1447+
1448+
/// Invokes [`shrink_to`] on the underlying instance of [`OsString`].
1449+
///
1450+
/// [`shrink_to`]: ../ffi/struct.OsString.html#method.shrink_to
1451+
/// [`OsString`]: ../ffi/struct.OsString.html
1452+
#[unstable(feature = "path_buf_capacity", issue = "58234")]
1453+
pub fn shrink_to(&mut self, min_capacity: usize) {
1454+
self.inner.shrink_to(min_capacity)
1455+
}
13761456
}
13771457

13781458
#[stable(feature = "box_from_path", since = "1.17.0")]

0 commit comments

Comments
 (0)