about summary refs log tree commit diff
path: root/src/liballoc
diff options
context:
space:
mode:
authorMark Rousskov <mark.simulacrum@gmail.com>2019-07-03 09:59:20 -0400
committerGitHub <noreply@github.com>2019-07-03 09:59:20 -0400
commitaa7999aaee49c51bbbc26aeb16f60d4e2902637f (patch)
treef6a60d11ff81f8fce3e22e80e8d7de685d8b0c37 /src/liballoc
parent37c58c63a7a2300cce60fe88fa5449aaf70fffd7 (diff)
parent95275658f26e0e83fb26f946e716fa5de28fe43a (diff)
downloadrust-aa7999aaee49c51bbbc26aeb16f60d4e2902637f.tar.gz
rust-aa7999aaee49c51bbbc26aeb16f60d4e2902637f.zip
Rollup merge of #62196 - cramertj:vec-leak, r=centril,withoutboats
Add Vec::leak
Diffstat (limited to 'src/liballoc')
-rw-r--r--src/liballoc/vec.rs34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs
index 92fe0834dd0..c0544d7469c 100644
--- a/src/liballoc/vec.rs
+++ b/src/liballoc/vec.rs
@@ -1367,6 +1367,40 @@ impl<T> Vec<T> {
             self.truncate(new_len);
         }
     }
+
+    /// Consumes and leaks the `Vec`, returning a mutable reference to the contents,
+    /// `&'a mut [T]`. Note that the type `T` must outlive the chosen lifetime
+    /// `'a`. If the type has only static references, or none at all, then this
+    /// may be chosen to be `'static`.
+    ///
+    /// This function is similar to the `leak` function on `Box`.
+    ///
+    /// This function is mainly useful for data that lives for the remainder of
+    /// the program's life. Dropping the returned reference will cause a memory
+    /// leak.
+    ///
+    /// # Examples
+    ///
+    /// Simple usage:
+    ///
+    /// ```
+    /// #![feature(vec_leak)]
+    ///
+    /// fn main() {
+    ///     let x = vec![1, 2, 3];
+    ///     let static_ref: &'static mut [usize] = Vec::leak(x);
+    ///     static_ref[0] += 1;
+    ///     assert_eq!(static_ref, &[2, 2, 3]);
+    /// }
+    /// ```
+    #[unstable(feature = "vec_leak", issue = "62195")]
+    #[inline]
+    pub fn leak<'a>(vec: Vec<T>) -> &'a mut [T]
+    where
+        T: 'a // Technically not needed, but kept to be explicit.
+    {
+        Box::leak(vec.into_boxed_slice())
+    }
 }
 
 impl<T: Clone> Vec<T> {