-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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 std::mem::take #61129
Comments
Seems like a reasonable addition to me 👍 |
I've written this helper in projects before as |
Implemented in #61130 |
"Something cheap but valid" is not necessarily trait Steal {
fn steal(&mut self) -> Self;
} Default implementations for |
@petrochenkov not sure what you mean? I don't think you can do this with As for adding a trait, we could totally do that, though |
Trivial example - for any |
Hopefully that assignment will be eliminated by the optimizer |
The name `swap_default` was suggested but rejected. @SimonSapin observed that this operation isn't really a `swap` in the same sense as `mem::swap`; it is a `replace`. Since `replace_default` is a bit misleading, the "correct" name would be `replace_with_default`, which is quite verbose. @czipperz observed that we have precedence for using `take` to refer to methods that replace with `Default` in `Cell::take` and `Option::take`, so this reverts commit 99c00591c29b472c8a87c4a9342d0e0c508647a3 to return to the original `take` method name. The name `replace_with_default` was suggested, but was deemed too verbose, especially given that we use `take` for methods that replace with `Default` elsewhere.
The implementation PR #61130 is landing, I’ve edited the issue description and labels to make it a tracking issue. |
Add std::mem::take as suggested in rust-lang#61129 This PR implements rust-lang#61129 by adding `std::mem::take`. The added function is equivalent to: ```rust std::mem::replace(dest, Default::default()) ``` This particular pattern is fairly common, especially when implementing `Future::poll`, where you often need to yield an owned value in `Async::Ready`. This change allows you to write ```rust return Async::Ready(std::mem::take(self.result)); ``` instead of ```rust return Async::Ready(std::mem::replace(self.result, Vec::new())); ``` EDIT: Changed name from `take` to `swap_default`. EDIT: Changed name back to `take`.
Add std::mem::take as suggested in #61129 This PR implements #61129 by adding `std::mem::take`. The added function is equivalent to: ```rust std::mem::replace(dest, Default::default()) ``` This particular pattern is fairly common, especially when implementing `Future::poll`, where you often need to yield an owned value in `Async::Ready`. This change allows you to write ```rust return Async::Ready(std::mem::take(self.result)); ``` instead of ```rust return Async::Ready(std::mem::replace(self.result, Vec::new())); ``` EDIT: Changed name from `take` to `swap_default`. EDIT: Changed name back to `take`.
To be replaced when the std one is stable rust-lang/rust#61129
To be replaced when the std one is stable rust-lang/rust#61129
At some point, we should revert commit ac21b60, which doesn't currently build. (This will convert compiletest usage of replace to take). |
^This is tracked in #62306. It should be done once this is stabilized. |
@jonhoo would you like to send a stabilization PR? I feel like this is pretty uncontroversial and useful, to the point that I first type |
Tracking issue: rust-lang#61129
Stabilized in #64716. |
This tracks the stabilization of this function added by PR #61130 in
std::mem
:Original feature request:
I decently often find myself in a position where I have a
HashMap
,Vec
, or some other non-Copy
type I want to "take" from its current location, but cannot because I only have&mut self
. A particularly common place for this to happen is when finally returningAsync::Ready
fromFuture::poll
. Usually in these cases, I end up writing:This works pretty well, since
Vec::new
does not allocate (as is the case for most collections).This pattern is common enough that it'd be nice if there was a more standard way to do it. In particular, something like:
A variant of this was suggested back in #33564, though that modified
Default
instead, which seems like overkill.If this is something others agree would be useful, I'd be happy to submit a PR.
EDIT: Changed name from
take
toswap_default
.EDIT: Changed name back to
take
.The text was updated successfully, but these errors were encountered: