Skip to content

Commit

Permalink
Auto merge of #74605 - rust-lang:vec-leak, r=Amanieu
Browse files Browse the repository at this point in the history
Stabilize Vec::leak as a method

Closes #62195

The signature is changed to a method rather than an associated function:

```diff
-pub fn leak<'a>(vec: Vec<T>) -> &'a mut [T]
+pub fn leak<'a>(self) -> &'a mut [T]
```

The reason for `Box::leak` not to be a method (`Deref` to an arbitrary `T` which might have its own, different `leak` method) does not apply.
  • Loading branch information
bors committed Aug 1, 2020
2 parents 05762e3 + 7d759f5 commit 5ef872f
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions library/alloc/src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1510,20 +1510,18 @@ impl<T> Vec<T> {
/// Simple usage:
///
/// ```
/// #![feature(vec_leak)]
///
/// let x = vec![1, 2, 3];
/// let static_ref: &'static mut [usize] = Vec::leak(x);
/// let static_ref: &'static mut [usize] = x.leak();
/// static_ref[0] += 1;
/// assert_eq!(static_ref, &[2, 2, 3]);
/// ```
#[unstable(feature = "vec_leak", issue = "62195")]
#[stable(feature = "vec_leak", since = "1.47.0")]
#[inline]
pub fn leak<'a>(vec: Vec<T>) -> &'a mut [T]
pub fn leak<'a>(self) -> &'a mut [T]
where
T: 'a, // Technically not needed, but kept to be explicit.
{
Box::leak(vec.into_boxed_slice())
Box::leak(self.into_boxed_slice())
}
}

Expand Down

0 comments on commit 5ef872f

Please sign in to comment.