-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tracking issue for Vec::leak
#62195
Comments
Implementation in #62196 |
This is less relevant to @cramertj Do you think it should still be an associated function? |
I don't have any particularly strong opinion. |
Is there any reason to not have this method return an |
I find weird that there is a IMHO, if |
Seems fine to me. Sadly, I don't think there's a trait that covers these, so they'll all have to be added independently. |
I don't think the lack of trait is a problem at all. For example I would expect any standard type that has a |
@Lucretiel the lifetime parameter is necessary in case
|
What do people think about changing the argument type to be more generic: pub fn leak<'a>(vec: impl Into<Vec<T>>) -> &'a mut [T]
where
T: 'a, I'm thinking about adding a similar |
If we were to change it in this way, wouldn't it be preferable to use the |
I’d prefer to go in the opposite direction and change
|
You and I have had this discussion for other types so I won’t restate my previous positions against this.
Vec::leak itself doesn’t need to exist and can be a separate (chained) call — vec -> boxed slice -> box::leak |
I’m not following how this would work; would you mind expanding a bit? |
So, your proposal is that we have Though, now that I'm thinking about it, I think it also does |
@shepmaster I’m sorry, I don’t remember the specifics so if you have a link to those discussions I wouldn’t mind. By "chained" I was referring specifically to method chaining syntax |
Ah, sorry, that's not what I meant. I mean to add another method: impl String {
pub fn leak(s: impl Into<String>) -> &'a mut str {}
} |
Ah, yes, I definitely can support that. |
But wait, so- that means (for example) that something like
Additionally, can I ask you to elaborate on this a bit? It makes perfect sense to me for smart-pointer types like |
I'd rather not as I've always lost that particular argument. No reason to believe that this time will be any different. |
I’ve proposed FCP to stabilize with a signature change at #74605 |
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.leak
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.leak
Similar to
Box::leak
, this function would allow leaking aVec
and getting back an&'a mut [T]
.The text was updated successfully, but these errors were encountered: