@@ -579,32 +579,62 @@ impl<T> RefCell<T> {
579
579
///
580
580
/// This function corresponds to [`std::mem::replace`](../mem/fn.replace.html).
581
581
///
582
+ /// # Panics
583
+ ///
584
+ /// Panics if the value is currently borrowed.
585
+ ///
582
586
/// # Examples
583
587
///
584
588
/// ```
585
589
/// #![feature(refcell_replace_swap)]
586
590
/// 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));
591
595
/// ```
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).
592
606
///
593
607
/// # Panics
594
608
///
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
+ /// ```
597
621
#[ inline]
598
622
#[ 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)
601
627
}
602
628
603
629
/// Swaps the wrapped value of `self` with the wrapped value of `other`,
604
630
/// without deinitializing either one.
605
631
///
606
632
/// This function corresponds to [`std::mem::swap`](../mem/fn.swap.html).
607
633
///
634
+ /// # Panics
635
+ ///
636
+ /// Panics if the value in either `RefCell` is currently borrowed.
637
+ ///
608
638
/// # Examples
609
639
///
610
640
/// ```
@@ -616,11 +646,6 @@ impl<T> RefCell<T> {
616
646
/// assert_eq!(c, RefCell::new(6));
617
647
/// assert_eq!(d, RefCell::new(5));
618
648
/// ```
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.
624
649
#[ inline]
625
650
#[ unstable( feature = "refcell_replace_swap" , issue="43570" ) ]
626
651
pub fn swap ( & self , other : & Self ) {
0 commit comments