Rollup merge of #58539 - aaronstillwell:master, r=Mark-Simulacrum
Add alias methods to PathBuf for underlying OsString (#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 #58234)
This commit is contained in:
commit
c965858e20
1 changed files with 81 additions and 0 deletions
|
@ -1145,6 +1145,33 @@ impl PathBuf {
|
|||
PathBuf { inner: OsString::new() }
|
||||
}
|
||||
|
||||
/// Creates a new `PathBuf` with a given capacity used to create the
|
||||
/// internal [`OsString`]. See [`with_capacity`] defined on [`OsString`].
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(path_buf_capacity)]
|
||||
/// use std::path::PathBuf;
|
||||
///
|
||||
/// let mut path = PathBuf::with_capacity(10);
|
||||
/// let capacity = path.capacity();
|
||||
///
|
||||
/// // This push is done without reallocating
|
||||
/// path.push(r"C:\");
|
||||
///
|
||||
/// assert_eq!(capacity, path.capacity());
|
||||
/// ```
|
||||
///
|
||||
/// [`with_capacity`]: ../ffi/struct.OsString.html#method.with_capacity
|
||||
/// [`OsString`]: ../ffi/struct.OsString.html
|
||||
#[unstable(feature = "path_buf_capacity", issue = "58234")]
|
||||
pub fn with_capacity(capacity: usize) -> PathBuf {
|
||||
PathBuf {
|
||||
inner: OsString::with_capacity(capacity)
|
||||
}
|
||||
}
|
||||
|
||||
/// Coerces to a [`Path`] slice.
|
||||
///
|
||||
/// [`Path`]: struct.Path.html
|
||||
|
@ -1373,6 +1400,60 @@ impl PathBuf {
|
|||
let rw = Box::into_raw(self.inner.into_boxed_os_str()) as *mut Path;
|
||||
unsafe { Box::from_raw(rw) }
|
||||
}
|
||||
|
||||
/// Invokes [`capacity`] on the underlying instance of [`OsString`].
|
||||
///
|
||||
/// [`capacity`]: ../ffi/struct.OsString.html#method.capacity
|
||||
/// [`OsString`]: ../ffi/struct.OsString.html
|
||||
#[unstable(feature = "path_buf_capacity", issue = "58234")]
|
||||
pub fn capacity(&self) -> usize {
|
||||
self.inner.capacity()
|
||||
}
|
||||
|
||||
/// Invokes [`clear`] on the underlying instance of [`OsString`].
|
||||
///
|
||||
/// [`clear`]: ../ffi/struct.OsString.html#method.clear
|
||||
/// [`OsString`]: ../ffi/struct.OsString.html
|
||||
#[unstable(feature = "path_buf_capacity", issue = "58234")]
|
||||
pub fn clear(&mut self) {
|
||||
self.inner.clear()
|
||||
}
|
||||
|
||||
/// Invokes [`reserve`] on the underlying instance of [`OsString`].
|
||||
///
|
||||
/// [`reserve`]: ../ffi/struct.OsString.html#method.reserve
|
||||
/// [`OsString`]: ../ffi/struct.OsString.html
|
||||
#[unstable(feature = "path_buf_capacity", issue = "58234")]
|
||||
pub fn reserve(&mut self, additional: usize) {
|
||||
self.inner.reserve(additional)
|
||||
}
|
||||
|
||||
/// Invokes [`reserve_exact`] on the underlying instance of [`OsString`].
|
||||
///
|
||||
/// [`reserve_exact`]: ../ffi/struct.OsString.html#method.reserve_exact
|
||||
/// [`OsString`]: ../ffi/struct.OsString.html
|
||||
#[unstable(feature = "path_buf_capacity", issue = "58234")]
|
||||
pub fn reserve_exact(&mut self, additional: usize) {
|
||||
self.inner.reserve_exact(additional)
|
||||
}
|
||||
|
||||
/// Invokes [`shrink_to_fit`] on the underlying instance of [`OsString`].
|
||||
///
|
||||
/// [`shrink_to_fit`]: ../ffi/struct.OsString.html#method.shrink_to_fit
|
||||
/// [`OsString`]: ../ffi/struct.OsString.html
|
||||
#[unstable(feature = "path_buf_capacity", issue = "58234")]
|
||||
pub fn shrink_to_fit(&mut self) {
|
||||
self.inner.shrink_to_fit()
|
||||
}
|
||||
|
||||
/// Invokes [`shrink_to`] on the underlying instance of [`OsString`].
|
||||
///
|
||||
/// [`shrink_to`]: ../ffi/struct.OsString.html#method.shrink_to
|
||||
/// [`OsString`]: ../ffi/struct.OsString.html
|
||||
#[unstable(feature = "path_buf_capacity", issue = "58234")]
|
||||
pub fn shrink_to(&mut self, min_capacity: usize) {
|
||||
self.inner.shrink_to(min_capacity)
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "box_from_path", since = "1.17.0")]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue