Commit 265fef4
committed
Auto merge of rust-lang#89337 - mbrubeck:vec-leak, r=m-ou-se
Avoid allocations and copying in Vec::leak
The [`Vec::leak`] method (rust-lang#62195) is currently implemented by calling `Vec::into_boxed_slice` and `Box::leak`. This shrinks the vector before leaking it, which potentially causes a reallocation and copies the vector's contents.
By avoiding the conversion to `Box`, we can instead leak the vector without any expensive operations, just by returning a slice reference and forgetting the `Vec`. Users who *want* to shrink the vector first can still do so by calling `shrink_to_fit` explicitly.
**Note:** This could break code that uses `Box::from_raw` to “un-leak” the slice returned by `Vec::leak`. However, the `Vec::leak` docs explicitly forbid this, so such code is already incorrect.
[`Vec::leak`]: https://doc.rust-lang.org/stable/std/vec/struct.Vec.html#method.leak1 file changed
+5
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1973 | 1973 | | |
1974 | 1974 | | |
1975 | 1975 | | |
1976 | | - | |
1977 | | - | |
| 1976 | + | |
| 1977 | + | |
| 1978 | + | |
1978 | 1979 | | |
1979 | 1980 | | |
1980 | 1981 | | |
| |||
1997 | 1998 | | |
1998 | 1999 | | |
1999 | 2000 | | |
2000 | | - | |
| 2001 | + | |
| 2002 | + | |
2001 | 2003 | | |
2002 | 2004 | | |
2003 | 2005 | | |
| |||
0 commit comments