@@ -543,6 +543,59 @@ impl<T> RefCell<T> {
543
543
debug_assert ! ( self . borrow. get( ) == UNUSED ) ;
544
544
unsafe { self . value . into_inner ( ) }
545
545
}
546
+
547
+ /// Replaces the wrapped value with a new one, returning the old value,
548
+ /// without deinitializing either one.
549
+ ///
550
+ /// This function corresponds to [`std::mem::replace`](../mem/fn.replace.html).
551
+ ///
552
+ /// # Example
553
+ ///
554
+ /// ```
555
+ /// #![feature(refcell_replace_swap)]
556
+ /// use std::cell::RefCell;
557
+ /// let c = RefCell::new(5);
558
+ /// let u = c.replace(6);
559
+ /// assert_eq!(u, 5);
560
+ /// assert_eq!(c, RefCell::new(6));
561
+ /// ```
562
+ ///
563
+ /// # Panics
564
+ ///
565
+ /// This function will panic if the `RefCell` has any outstanding borrows,
566
+ /// whether or not they are full mutable borrows.
567
+ #[ inline]
568
+ #[ unstable( feature = "refcell_replace_swap" , issue="43570" ) ]
569
+ pub fn replace ( & self , t : T ) -> T {
570
+ mem:: replace ( & mut * self . borrow_mut ( ) , t)
571
+ }
572
+
573
+ /// Swaps the wrapped value of `self` with the wrapped value of `other`,
574
+ /// without deinitializing either one.
575
+ ///
576
+ /// This function corresponds to [`std::mem::swap`](../mem/fn.swap.html).
577
+ ///
578
+ /// # Example
579
+ ///
580
+ /// ```
581
+ /// #![feature(refcell_replace_swap)]
582
+ /// use std::cell::RefCell;
583
+ /// let c = RefCell::new(5);
584
+ /// let d = RefCell::new(6);
585
+ /// c.swap(&d);
586
+ /// assert_eq!(c, RefCell::new(6));
587
+ /// assert_eq!(d, RefCell::new(5));
588
+ /// ```
589
+ ///
590
+ /// # Panics
591
+ ///
592
+ /// This function will panic if the `RefCell` has any outstanding borrows,
593
+ /// whether or not they are full mutable borrows.
594
+ #[ inline]
595
+ #[ unstable( feature = "refcell_replace_swap" , issue="43570" ) ]
596
+ pub fn swap ( & self , other : & Self ) {
597
+ mem:: swap ( & mut * self . borrow_mut ( ) , & mut * other. borrow_mut ( ) )
598
+ }
546
599
}
547
600
548
601
impl < T : ?Sized > RefCell < T > {
0 commit comments