-
-
Notifications
You must be signed in to change notification settings - Fork 14.2k
added core::mem::reshape #130849
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
added core::mem::reshape #130849
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -874,6 +874,38 @@ pub const fn replace<T>(dest: &mut T, src: T) -> T { | |
| } | ||
| } | ||
|
|
||
| /// Replaces the value in `dest` by the value returned by `f`. | ||
| /// | ||
| /// `f` receives the current referenced value of `dest` as its argument. | ||
| /// | ||
| /// * If you want to replace the values of two variables, see [`swap`]. | ||
| /// * If you want to replace with a default value, see [`take`]. | ||
| /// * If you want replace without the function, see [`replace`]. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// A simple example: | ||
| /// | ||
| /// ``` | ||
| /// use std::mem; | ||
| /// | ||
| /// let mut v: Vec<i32> = vec![1, 2]; | ||
| /// | ||
| /// mem::replace(&mut v, |mut v| { v.push(3); v }); | ||
| /// assert_eq!(vec![1, 2, 3], v); | ||
| /// ``` | ||
| #[inline] | ||
| #[unstable(feature = "mem_reshape", issue = "130848")] | ||
| pub fn reshape<T>(dest: &mut T, f: impl FnOnce(T) -> T) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this is a good name. When I first came across the tracking issue in the issue list, I though this was something transmute related. |
||
| // SAFETY: We read from `dest` but directly write to it afterwards, | ||
| // such that the old value is not duplicated. Nothing is dropped and | ||
| // nothing here can panic. | ||
| unsafe { | ||
| let result = ptr::read(dest); | ||
| ptr::write(dest, f(result)); | ||
|
Comment on lines
+904
to
+905
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is not sound if
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah I just detected that myself and ran into a wall. Sounds good 👍
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. has there been any discussion about a feature to ensure a closure does not panic?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hm. this has probably come up in vague ways in many places but nothing concrete.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. either way it's definitely both that will arrive any time soon, so it probably makes sense to close the PR and tracking issue for now
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thank you for it anyways :3
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you execute the closure inside an extern "C" wrapper func that would abort on unwind? I think I recently saw a PR on a helper that exposed abort on unwind (mirroring the catch_unwind function) using that technique
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're thinking of #130339 |
||
| } | ||
| } | ||
|
|
||
| /// Disposes of a value. | ||
| /// | ||
| /// This does so by calling the argument's implementation of [`Drop`][drop]. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be
reshape?