Skip to content

Commit a23c40e

Browse files
Add alias methods to PathBuf for underlying OsString
Implemented the following methods on PathBuf which forward to the underlying OsString. - capacity - with_capacity - clear - reserve - reserve_exact - shrink_to_fit - shrink_to
1 parent 8af675a commit a23c40e

File tree

1 file changed

+81
-1
lines changed

1 file changed

+81
-1
lines changed

src/libstd/path.rs

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1137,14 +1137,40 @@ impl PathBuf {
11371137
///
11381138
/// ```
11391139
/// use std::path::PathBuf;
1140-
///
1140+
///
11411141
/// let path = PathBuf::new();
11421142
/// ```
11431143
#[stable(feature = "rust1", since = "1.0.0")]
11441144
pub fn new() -> 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+
#[stable(feature = "path_buf_os_string_methods", since = "1.33.0")]
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+
#[stable(feature = "path_buf_os_string_methods", since = "1.33.0")]
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+
#[stable(feature = "path_buf_os_string_methods", since = "1.33.0")]
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+
#[stable(feature = "path_buf_os_string_methods", since = "1.33.0")]
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+
#[stable(feature = "path_buf_os_string_methods", since = "1.33.0")]
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+
#[stable(feature = "path_buf_os_string_methods", since = "1.33.0")]
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+
#[stable(feature = "path_buf_os_string_methods", since = "1.33.0")]
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)