@@ -571,6 +571,59 @@ impl<T> RefCell<T> {
571571 debug_assert ! ( self . borrow. get( ) == UNUSED ) ;
572572 unsafe { self . value . into_inner ( ) }
573573 }
574+
575+ /// Replaces the wrapped value with a new one, returning the old value,
576+ /// without deinitializing either one.
577+ ///
578+ /// This function corresponds to [`std::mem::replace`](../mem/fn.replace.html).
579+ ///
580+ /// # Examples
581+ ///
582+ /// ```
583+ /// #![feature(refcell_replace_swap)]
584+ /// use std::cell::RefCell;
585+ /// let c = RefCell::new(5);
586+ /// let u = c.replace(6);
587+ /// assert_eq!(u, 5);
588+ /// assert_eq!(c, RefCell::new(6));
589+ /// ```
590+ ///
591+ /// # Panics
592+ ///
593+ /// This function will panic if the `RefCell` has any outstanding borrows,
594+ /// whether or not they are full mutable borrows.
595+ #[ inline]
596+ #[ unstable( feature = "refcell_replace_swap" , issue="43570" ) ]
597+ pub fn replace ( & self , t : T ) -> T {
598+ mem:: replace ( & mut * self . borrow_mut ( ) , t)
599+ }
600+
601+ /// Swaps the wrapped value of `self` with the wrapped value of `other`,
602+ /// without deinitializing either one.
603+ ///
604+ /// This function corresponds to [`std::mem::swap`](../mem/fn.swap.html).
605+ ///
606+ /// # Examples
607+ ///
608+ /// ```
609+ /// #![feature(refcell_replace_swap)]
610+ /// use std::cell::RefCell;
611+ /// let c = RefCell::new(5);
612+ /// let d = RefCell::new(6);
613+ /// c.swap(&d);
614+ /// assert_eq!(c, RefCell::new(6));
615+ /// assert_eq!(d, RefCell::new(5));
616+ /// ```
617+ ///
618+ /// # Panics
619+ ///
620+ /// This function will panic if either `RefCell` has any outstanding borrows,
621+ /// whether or not they are full mutable borrows.
622+ #[ inline]
623+ #[ unstable( feature = "refcell_replace_swap" , issue="43570" ) ]
624+ pub fn swap ( & self , other : & Self ) {
625+ mem:: swap ( & mut * self . borrow_mut ( ) , & mut * other. borrow_mut ( ) )
626+ }
574627}
575628
576629impl < T : ?Sized > RefCell < T > {
0 commit comments