diff options
| author | Mazdak Farrokhzad <twingoow@gmail.com> | 2019-10-08 05:02:36 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-10-08 05:02:36 +0200 |
| commit | a9777b3061ad3a2b935f2fce313aa838a40035ec (patch) | |
| tree | f1143be481f9b6ea55117f165fe21c9d7bc30141 | |
| parent | 473709541610bff6642b7abe67b5fbeb2554bdd1 (diff) | |
| parent | 375469149c123d72d188bed3fbe468304a1b6004 (diff) | |
| download | rust-a9777b3061ad3a2b935f2fce313aa838a40035ec.tar.gz rust-a9777b3061ad3a2b935f2fce313aa838a40035ec.zip | |
Rollup merge of #65046 - sinkuu:cell_reorder, r=shepmaster
Make `Cell::new` method come first in documentation Methods to create a thing usually comes first in `std` documentation, and `Cell` has been an exception. Also, `T: Copy` specialized methods should not be on top of the page. (This had led me to miss that most of its methods are not bounded by `Copy`...)
| -rw-r--r-- | src/libcore/cell.rs | 92 |
1 files changed, 46 insertions, 46 deletions
diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index 8579dbf353e..fda103a52d8 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -229,52 +229,6 @@ pub struct Cell<T: ?Sized> { value: UnsafeCell<T>, } -impl<T:Copy> Cell<T> { - /// Returns a copy of the contained value. - /// - /// # Examples - /// - /// ``` - /// use std::cell::Cell; - /// - /// let c = Cell::new(5); - /// - /// let five = c.get(); - /// ``` - #[inline] - #[stable(feature = "rust1", since = "1.0.0")] - pub fn get(&self) -> T { - unsafe{ *self.value.get() } - } - - /// Updates the contained value using a function and returns the new value. - /// - /// # Examples - /// - /// ``` - /// #![feature(cell_update)] - /// - /// use std::cell::Cell; - /// - /// let c = Cell::new(5); - /// let new = c.update(|x| x + 1); - /// - /// assert_eq!(new, 6); - /// assert_eq!(c.get(), 6); - /// ``` - #[inline] - #[unstable(feature = "cell_update", issue = "50186")] - pub fn update<F>(&self, f: F) -> T - where - F: FnOnce(T) -> T, - { - let old = self.get(); - let new = f(old); - self.set(new); - new - } -} - #[stable(feature = "rust1", since = "1.0.0")] unsafe impl<T: ?Sized> Send for Cell<T> where T: Send {} @@ -448,6 +402,52 @@ impl<T> Cell<T> { } } +impl<T:Copy> Cell<T> { + /// Returns a copy of the contained value. + /// + /// # Examples + /// + /// ``` + /// use std::cell::Cell; + /// + /// let c = Cell::new(5); + /// + /// let five = c.get(); + /// ``` + #[inline] + #[stable(feature = "rust1", since = "1.0.0")] + pub fn get(&self) -> T { + unsafe{ *self.value.get() } + } + + /// Updates the contained value using a function and returns the new value. + /// + /// # Examples + /// + /// ``` + /// #![feature(cell_update)] + /// + /// use std::cell::Cell; + /// + /// let c = Cell::new(5); + /// let new = c.update(|x| x + 1); + /// + /// assert_eq!(new, 6); + /// assert_eq!(c.get(), 6); + /// ``` + #[inline] + #[unstable(feature = "cell_update", issue = "50186")] + pub fn update<F>(&self, f: F) -> T + where + F: FnOnce(T) -> T, + { + let old = self.get(); + let new = f(old); + self.set(new); + new + } +} + impl<T: ?Sized> Cell<T> { /// Returns a raw pointer to the underlying data in this cell. /// |
