From c6cfa3c4899e46addfe6d04d6aaa2d247056748f Mon Sep 17 00:00:00 2001 From: Wesley Wiser Date: Tue, 24 Jan 2017 22:44:33 -0500 Subject: Extend Cell to work with non-Copy types Part of #39264 --- src/libcore/cell.rs | 130 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 94 insertions(+), 36 deletions(-) (limited to 'src/libcore') diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index c3f862e7c54..cafa19afcb6 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -176,6 +176,7 @@ use cmp::Ordering; use fmt::{self, Debug, Display}; use marker::Unsize; +use mem; use ops::{Deref, DerefMut, CoerceUnsized}; /// A mutable memory location that admits only `Copy` data. @@ -187,23 +188,6 @@ pub struct Cell { } impl Cell { - /// Creates a new `Cell` containing the given value. - /// - /// # Examples - /// - /// ``` - /// use std::cell::Cell; - /// - /// let c = Cell::new(5); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - #[inline] - pub const fn new(value: T) -> Cell { - Cell { - value: UnsafeCell::new(value), - } - } - /// Returns a copy of the contained value. /// /// # Examples @@ -221,25 +205,6 @@ impl Cell { unsafe{ *self.value.get() } } - /// Sets the contained value. - /// - /// # Examples - /// - /// ``` - /// use std::cell::Cell; - /// - /// let c = Cell::new(5); - /// - /// c.set(10); - /// ``` - #[inline] - #[stable(feature = "rust1", since = "1.0.0")] - pub fn set(&self, value: T) { - unsafe { - *self.value.get() = value; - } - } - /// Returns a reference to the underlying `UnsafeCell`. /// /// # Examples @@ -378,6 +343,99 @@ impl From for Cell { } } +#[unstable(feature = "move_cell", issue = "39264")] +impl Cell { + /// Creates a new `Cell` containing the given value. + /// + /// # Examples + /// + /// ``` + /// use std::cell::Cell; + /// + /// let c = Cell::new(5); + /// ``` + #[stable(feature = "rust1", since = "1.0.0")] + #[inline] + pub const fn new(value: T) -> Cell { + Cell { + value: UnsafeCell::new(value), + } + } + + /// Sets the contained value. + /// + /// # Examples + /// + /// ``` + /// use std::cell::Cell; + /// + /// let c = Cell::new(5); + /// + /// c.set(10); + /// ``` + #[inline] + #[stable(feature = "rust1", since = "1.0.0")] + pub fn set(&self, val: T) { + let old = self.replace(val); + drop(old); + } + + /// Replaces the contained value. + /// + /// # Examples + /// + /// ``` + /// #![feature(move_cell)] + /// use std::cell::Cell; + /// + /// let c = Cell::new(5); + /// let old = c.replace(10); + /// + /// assert_eq!(5, old); + /// ``` + pub fn replace(&self, val: T) -> T { + mem::replace(unsafe { &mut *self.value.get() }, val) + } + + /// Unwraps the value. + /// + /// # Examples + /// + /// ``` + /// #![feature(move_cell)] + /// use std::cell::Cell; + /// + /// let c = Cell::new(5); + /// let five = c.into_inner(); + /// + /// assert_eq!(five, 5); + /// ``` + pub fn into_inner(self) -> T { + unsafe { self.value.into_inner() } + } +} + +#[unstable(feature = "move_cell", issue = "39264")] +impl Cell { + /// Takes the value of the cell, leaving `Default::default()` in its place. + /// + /// # Examples + /// + /// ``` + /// #![feature(move_cell)] + /// use std::cell::Cell; + /// + /// let c = Cell::new(5); + /// let five = c.take(); + /// + /// assert_eq!(five, 5); + /// assert_eq!(c.into_inner(), 0); + /// ``` + pub fn take(&self) -> T { + self.replace(Default::default()) + } +} + #[unstable(feature = "coerce_unsized", issue = "27732")] impl, U> CoerceUnsized> for Cell {} -- cgit 1.4.1-3-g733a5 From afac3ecacc92ccf7a3f693702c9582f930fb91f3 Mon Sep 17 00:00:00 2001 From: Wesley Wiser Date: Thu, 26 Jan 2017 21:28:22 -0500 Subject: Move stability attributes per feedback --- src/libcore/cell.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/libcore') diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index cafa19afcb6..f889ff9a6ae 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -343,7 +343,6 @@ impl From for Cell { } } -#[unstable(feature = "move_cell", issue = "39264")] impl Cell { /// Creates a new `Cell` containing the given value. /// @@ -393,6 +392,7 @@ impl Cell { /// /// assert_eq!(5, old); /// ``` + #[unstable(feature = "move_cell", issue = "39264")] pub fn replace(&self, val: T) -> T { mem::replace(unsafe { &mut *self.value.get() }, val) } @@ -410,12 +410,12 @@ impl Cell { /// /// assert_eq!(five, 5); /// ``` + #[unstable(feature = "move_cell", issue = "39264")] pub fn into_inner(self) -> T { unsafe { self.value.into_inner() } } } -#[unstable(feature = "move_cell", issue = "39264")] impl Cell { /// Takes the value of the cell, leaving `Default::default()` in its place. /// @@ -431,6 +431,7 @@ impl Cell { /// assert_eq!(five, 5); /// assert_eq!(c.into_inner(), 0); /// ``` + #[unstable(feature = "move_cell", issue = "39264")] pub fn take(&self) -> T { self.replace(Default::default()) } -- cgit 1.4.1-3-g733a5 From daa509109fb140cdbb6bb391ed361ab9ee502e68 Mon Sep 17 00:00:00 2001 From: Wesley Wiser Date: Thu, 26 Jan 2017 22:23:32 -0500 Subject: Update cell docs --- src/libcore/cell.rs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'src/libcore') diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index f889ff9a6ae..ab44342ebf0 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -15,10 +15,18 @@ //! references. We say that `Cell` and `RefCell` provide 'interior mutability', in contrast //! with typical Rust types that exhibit 'inherited mutability'. //! -//! Cell types come in two flavors: `Cell` and `RefCell`. `Cell` provides `get` and `set` -//! methods that change the interior value with a single method call. `Cell` though is only -//! compatible with types that implement `Copy`. For other types, one must use the `RefCell` -//! type, acquiring a write lock before mutating. +//! Cell types come in two flavors: `Cell` and `RefCell`. `Cell` implements interior +//! mutability by moving values in and out of the `Cell`. To use references instead of values, +//! one must use the `RefCell` type, acquiring a write lock before mutating. `Cell` provides +//! methods to retrieve and change the current interior value: +//! +//! - For types that implement `Copy`, the `get` method retrieves the current interior value. +//! - For types that implement `Default`, the `take` method replaces the current interior value +//! with `Default::default()` and returns the replaced value. +//! - For all types, the `replace` method replaces the current interior value and returns the +//! replaced value and the `into_inner` method consumes the `Cell` and returns the interior +//! value. Additionally, the `set` method replaces the interior value, dropping the replaced +//! value. //! //! `RefCell` uses Rust's lifetimes to implement 'dynamic borrowing', a process whereby one can //! claim temporary, exclusive, mutable access to the inner value. Borrows for `RefCell`s are @@ -179,7 +187,7 @@ use marker::Unsize; use mem; use ops::{Deref, DerefMut, CoerceUnsized}; -/// A mutable memory location that admits only `Copy` data. +/// A mutable memory location. /// /// See the [module-level documentation](index.html) for more. #[stable(feature = "rust1", since = "1.0.0")] -- cgit 1.4.1-3-g733a5