diff options
| author | Charlie Fan <changchu@synopsys.com> | 2017-02-10 16:38:59 +0800 |
|---|---|---|
| committer | f001 <changchun.fan@qq.com> | 2017-02-11 13:08:13 +0800 |
| commit | a5e8bbf32b32ed7d3b06149fdfa0977c0ec0a22e (patch) | |
| tree | 5098de32e56806d6d7407a59bf4bb5c8810a5eb3 /src/libcore | |
| parent | 24a70eb598a76edb0941f628a87946b40f2a1c83 (diff) | |
| download | rust-a5e8bbf32b32ed7d3b06149fdfa0977c0ec0a22e.tar.gz rust-a5e8bbf32b32ed7d3b06149fdfa0977c0ec0a22e.zip | |
Add `swap` method for `Cell`
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/cell.rs | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index ab44342ebf0..d130b0279a2 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -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. /// @@ -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 |
