-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Include fn take_mut<T, F: FnOnce(T) -> T>(&mut T, F)
in libstd
#1508
Comments
How about automatically implementing a |
I'll mention a concern that was raised: This API allows a free |
As long as there's some |
you can always manually implement the unsafe |
@oli-obk Unfortunately, functions aren't currently types. |
The problem with ideas like Another option alongside "just abort" that could be offered in case the function panics is to replace the stolen
This type signature may be surprising at first. How it works is, if Another option is to produce the backup value only on demand:
Now in this case, the And there could be a convenience (The reason all of these propagate the panic from So for anyone not keeping track, that's 4 different variants on this functionality it potentially makes sense to provide. (I did not put any thought into naming.) |
I'm not sure if the never-aborting |
With the current formulation of drop flags, a convenient backup value is the drop flag. If you overwrite the T with the drop flag by calling |
While this doesn't call |
It is possible to write a function to temporarily take, by value, a
T
out of a&mut T
, replacing it by the end of the closure. Panics can be handled by killing the process. Crate to do this: https://github.com/Sgeo/take_mut/tree/v0.1.3 ,take_mut::take
.This might be reasonable to include in
std::mem
, possibly under a different name. It's a function that effectively extends what it's possible to do with a&mut T
in safe code, similarly tostd::mem::swap
andstd::mem::replace
.Drawbacks include that we might not want to encourage use of functions that, in the worst case, do things more extreme than panicking (exiting or aborting the process).
More bikeshedding opportunities on whether it should exit or abort on panic,
The text was updated successfully, but these errors were encountered: