about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAaron Stillwell <aaron@stillwell.tech>2019-02-17 15:17:46 +0000
committerAaron Stillwell <aaron@stillwell.tech>2019-02-17 15:17:46 +0000
commita23c40ec9435dd7b784e833e876a133c627aff92 (patch)
tree7d5dc844c53141ca5d1c1e1433745bb70e7fcc73
parent8af675a07576940ba24e3d91abd10b029b937946 (diff)
downloadrust-a23c40ec9435dd7b784e833e876a133c627aff92.tar.gz
rust-a23c40ec9435dd7b784e833e876a133c627aff92.zip
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
-rw-r--r--src/libstd/path.rs82
1 files changed, 81 insertions, 1 deletions
diff --git a/src/libstd/path.rs b/src/libstd/path.rs
index 0a9796d1a9c..3e794956550 100644
--- a/src/libstd/path.rs
+++ b/src/libstd/path.rs
@@ -1137,7 +1137,7 @@ impl PathBuf {
     ///
     /// ```
     /// use std::path::PathBuf;
-    ///
+    /// 
     /// let path = PathBuf::new();
     /// ```
     #[stable(feature = "rust1", since = "1.0.0")]
@@ -1145,6 +1145,32 @@ 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
+    /// 
+    /// ```
+    /// use std::path::PathBuf;
+    /// 
+    /// let 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
+    #[stable(feature = "path_buf_os_string_methods", since = "1.33.0")]
+    pub fn with_capacity(capacity: usize) -> PathBuf {
+        PathBuf {
+            inner: OsString::with_capacity(capacity)
+        }
+    }
+
     /// Coerces to a [`Path`] slice.
     ///
     /// [`Path`]: struct.Path.html
@@ -1373,6 +1399,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
+    #[stable(feature = "path_buf_os_string_methods", since = "1.33.0")]
+    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
+    #[stable(feature = "path_buf_os_string_methods", since = "1.33.0")]
+    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
+    #[stable(feature = "path_buf_os_string_methods", since = "1.33.0")]
+    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
+    #[stable(feature = "path_buf_os_string_methods", since = "1.33.0")]
+    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
+    #[stable(feature = "path_buf_os_string_methods", since = "1.33.0")]
+    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
+    #[stable(feature = "path_buf_os_string_methods", since = "1.33.0")]
+    pub fn shrink_to(mut self, min_capacity: usize) {
+        self.inner.shrink_to(min_capacity)
+    }
 }
 
 #[stable(feature = "box_from_path", since = "1.17.0")]