Skip to content

Commit

Permalink
Rollup merge of rust-lang#39716 - F001:swapCell, r=alexcrichton
Browse files Browse the repository at this point in the history
Add `swap` method for `Cell`

Addition to rust-lang#39264

r? @alexcrichton
  • Loading branch information
frewsxcv authored Feb 13, 2017
2 parents 717ac96 + a5e8bbf commit 5b79f33
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/libcore/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ use fmt::{self, Debug, Display};
use marker::Unsize;
use mem;
use ops::{Deref, DerefMut, CoerceUnsized};
use ptr;

/// A mutable memory location.
///
Expand Down Expand Up @@ -387,6 +388,32 @@ impl<T> Cell<T> {
drop(old);
}

/// Swaps the values of two Cells.
/// Difference with `std::mem::swap` is that this function doesn't require `&mut` reference.
///
/// # Examples
///
/// ```
/// #![feature(move_cell)]
/// use std::cell::Cell;
///
/// let c1 = Cell::new(5i32);
/// let c2 = Cell::new(10i32);
/// c1.swap(&c2);
/// assert_eq!(10, c1.get());
/// assert_eq!(5, c2.get());
/// ```
#[inline]
#[unstable(feature = "move_cell", issue = "39264")]
pub fn swap(&self, other: &Self) {
if ptr::eq(self, other) {
return;
}
unsafe {
ptr::swap(self.value.get(), other.value.get());
}
}

/// Replaces the contained value.
///
/// # Examples
Expand Down

0 comments on commit 5b79f33

Please sign in to comment.