@@ -579,32 +579,62 @@ impl<T> RefCell<T> {
579579 ///
580580 /// This function corresponds to [`std::mem::replace`](../mem/fn.replace.html).
581581 ///
582+ /// # Panics
583+ ///
584+ /// Panics if the value is currently borrowed.
585+ ///
582586 /// # Examples
583587 ///
584588 /// ```
585589 /// #![feature(refcell_replace_swap)]
586590 /// use std::cell::RefCell;
587- /// let c = RefCell::new(5);
588- /// let u = c .replace(6);
589- /// assert_eq!(u , 5);
590- /// assert_eq!(c , RefCell::new(6));
591+ /// let cell = RefCell::new(5);
592+ /// let old_value = cell .replace(6);
593+ /// assert_eq!(old_value , 5);
594+ /// assert_eq!(cell , RefCell::new(6));
591595 /// ```
596+ #[ inline]
597+ #[ unstable( feature = "refcell_replace_swap" , issue="43570" ) ]
598+ pub fn replace ( & self , t : T ) -> T {
599+ mem:: replace ( & mut * self . borrow_mut ( ) , t)
600+ }
601+
602+ /// Replaces the wrapped value with a new one computed from `f`, returning
603+ /// the old value, without deinitializing either one.
604+ ///
605+ /// This function corresponds to [`std::mem::replace`](../mem/fn.replace.html).
592606 ///
593607 /// # Panics
594608 ///
595- /// This function will panic if the `RefCell` has any outstanding borrows,
596- /// whether or not they are full mutable borrows.
609+ /// Panics if the value is currently borrowed.
610+ ///
611+ /// # Examples
612+ ///
613+ /// ```
614+ /// #![feature(refcell_replace_swap)]
615+ /// use std::cell::RefCell;
616+ /// let cell = RefCell::new(5);
617+ /// let old_value = cell.replace_with(|&mut old| old + 1);
618+ /// assert_eq!(old_value, 5);
619+ /// assert_eq!(cell, RefCell::new(6));
620+ /// ```
597621 #[ inline]
598622 #[ unstable( feature = "refcell_replace_swap" , issue="43570" ) ]
599- pub fn replace ( & self , t : T ) -> T {
600- mem:: replace ( & mut * self . borrow_mut ( ) , t)
623+ pub fn replace_with < F : FnOnce ( & mut T ) -> T > ( & self , f : F ) -> T {
624+ let mut_borrow = & mut * self . borrow_mut ( ) ;
625+ let replacement = f ( mut_borrow) ;
626+ mem:: replace ( mut_borrow, replacement)
601627 }
602628
603629 /// Swaps the wrapped value of `self` with the wrapped value of `other`,
604630 /// without deinitializing either one.
605631 ///
606632 /// This function corresponds to [`std::mem::swap`](../mem/fn.swap.html).
607633 ///
634+ /// # Panics
635+ ///
636+ /// Panics if the value in either `RefCell` is currently borrowed.
637+ ///
608638 /// # Examples
609639 ///
610640 /// ```
@@ -616,11 +646,6 @@ impl<T> RefCell<T> {
616646 /// assert_eq!(c, RefCell::new(6));
617647 /// assert_eq!(d, RefCell::new(5));
618648 /// ```
619- ///
620- /// # Panics
621- ///
622- /// This function will panic if either `RefCell` has any outstanding borrows,
623- /// whether or not they are full mutable borrows.
624649 #[ inline]
625650 #[ unstable( feature = "refcell_replace_swap" , issue="43570" ) ]
626651 pub fn swap ( & self , other : & Self ) {
0 commit comments