about summary refs log tree commit diff
path: root/library/std/src/path.rs
diff options
context:
space:
mode:
authorschvv31n <tim.kurdov@gmail.com>2024-06-04 11:53:59 +0100
committerschvv31n <tim.kurdov@gmail.com>2024-06-04 11:53:59 +0100
commitfd5777c4c5c8fbc54d33d15afb29479180c532be (patch)
tree7ca803a02c33582b471e37d9acbe280e2d2df405 /library/std/src/path.rs
parenteb5e2449c5a5215927834d67914ce41cccd3f3c9 (diff)
downloadrust-fd5777c4c5c8fbc54d33d15afb29479180c532be.tar.gz
rust-fd5777c4c5c8fbc54d33d15afb29479180c532be.zip
impl OsString::leak & PathBuf::leak
Diffstat (limited to 'library/std/src/path.rs')
-rw-r--r--library/std/src/path.rs19
1 files changed, 19 insertions, 0 deletions
diff --git a/library/std/src/path.rs b/library/std/src/path.rs
index f4e1e2a38c1..adbcdcd2e67 100644
--- a/library/std/src/path.rs
+++ b/library/std/src/path.rs
@@ -1226,6 +1226,25 @@ impl PathBuf {
         self
     }
 
+    /// Consumes and leaks the `PathBuf`, returning a mutable reference to the contents,
+    /// `&'a mut Path`.
+    ///
+    /// The caller has free choice over the returned lifetime, including 'static.
+    /// Indeed, this function is ideally used for data that lives for the remainder of
+    /// the program’s life, as dropping the returned reference will cause a memory leak.
+    ///
+    /// It does not reallocate or shrink the `PathBuf`, so the leaked allocation may include
+    /// unused capacity that is not part of the returned slice. If you want to discard excess
+    /// capacity, call [`into_boxed_path`], and then [`Box::leak`] instead.
+    /// However, keep in mind that trimming the capacity may result in a reallocation and copy.
+    ///
+    /// [`into_boxed_path`]: Self::into_boxed_path
+    #[unstable(feature = "os_string_pathbuf_leak", issue = "125965")]
+    #[inline]
+    pub fn leak<'a>(self) -> &'a mut Path {
+        Path::from_inner_mut(self.inner.leak())
+    }
+
     /// Extends `self` with `path`.
     ///
     /// If `path` is absolute, it replaces the current path.