- 
                Notifications
    You must be signed in to change notification settings 
- Fork 13.9k
          Implement replace_with, a function similar to the one provided by the take_mut crate.
          #36186
        
          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
          
     Closed
      
      
    
  
     Closed
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            4 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -415,6 +415,71 @@ pub fn replace<T>(dest: &mut T, mut src: T) -> T { | |
| src | ||
| } | ||
|  | ||
| /// A guarding type which will abort upon drop. | ||
| /// | ||
| /// This is used for catching unwinding and transforming it into abort. | ||
| /// | ||
| /// The destructor should never be called naturally (use `mem::forget()`), and only when unwinding. | ||
| struct ExitGuard; | ||
|  | ||
| impl Drop for ExitGuard { | ||
| fn drop(&mut self) { | ||
| // To avoid unwinding, we abort (we panic, which is equivalent to abort inside an unwinding | ||
| // destructor) the program, which ensures that the destructor of the invalidated value | ||
| // isn't run, since this destructor ought to be called only if unwinding happens. | ||
| panic!("`replace_with` closure unwinded. For safety reasons, this will \ | ||
| abort your program. Check the documentation"); | ||
| } | ||
| } | ||
|  | ||
| /// Temporarily takes ownership of a value at a mutable location, and replace it with a new value | ||
| /// based on the old one. | ||
| /// | ||
| /// We move out of reference temporarily, to apply a closure, returning a new value, which is then | ||
| /// placed at the original value's location. | ||
| /// | ||
| /// # An important note | ||
| /// | ||
| /// The behavior on panic (or to be more precise, unwinding) is specified to match the behavior of | ||
| /// panicking inside a destructor, which itself is simply specified to not unwind. | ||
| /// | ||
| /// # Example | ||
| /// | ||
| /// ``` | ||
| /// use std::mem; | ||
| /// | ||
| /// // Dump some stuff into a box. | ||
| /// let mut bx: Box<i32> = Box::new(200); | ||
| /// | ||
| /// // Temporarily steal ownership. | ||
| /// mem::replace_with(&mut bx, |mut owned| { | ||
| /// owner = 5; | ||
| /// | ||
| /// // The returned value is placed back in `&mut bx`. | ||
| /// Box::new(owner) | ||
| /// }); | ||
| /// ``` | ||
| #[inline] | ||
| #[unstable(feature = "replace_with", issue = "...")] | ||
| pub fn replace_with<T, F>(val: &mut T, closure: F) | ||
| where F: FnOnce(T) -> T { | ||
| // Guard against unwinding. Note that this is critical to safety, to avoid the value behind the | ||
| // reference `val` is not dropped twice during unwinding. | ||
| let guard = ExitGuard; | ||
|  | ||
| unsafe { | ||
| // Take out the value behind the pointer. | ||
| let old = ptr::read(val); | ||
| // Run the closure. | ||
| let new = closure(old); | ||
| 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. 
 | ||
| // Put the result back. | ||
| ptr::write(val, new); | ||
| } | ||
|  | ||
| // Forget the guard, to avoid panicking. | ||
| mem::forget(guard); | ||
| 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. Fails with "Use of undeclared type or module  | ||
| } | ||
|  | ||
| /// Disposes of a value. | ||
| /// | ||
| /// While this does call the argument's implementation of `Drop`, it will not | ||
|  | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
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.
Instead of "to avoid the value...is not dropped", maybe " to avoid the value...being dropped" or just "to avoid dropping the value..."
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.
your example in the comment uses mem::replace instead of mem::replace_with