Skip to content

Commit

Permalink
Rollup merge of rust-lang#35392 - malbarbo:cell-from, r=brson
Browse files Browse the repository at this point in the history
Implement From for Cell, RefCell and UnsafeCell

Considering that `From` is implemented for `Box`, `Rc` and `Arc`, it seems [reasonable](https://internals.rust-lang.org/t/implementing-from-t-for-other-std-types/3744) to implement it for `Cell`, `RefCell` and `UnsafeCell`.
  • Loading branch information
Manishearth authored Aug 13, 2016
2 parents ad247ce + 1403df7 commit 7e2fa43
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/libcore/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@

use clone::Clone;
use cmp::{PartialEq, Eq, PartialOrd, Ord, Ordering};
use convert::From;
use default::Default;
use fmt::{self, Debug, Display};
use marker::{Copy, PhantomData, Send, Sync, Sized, Unsize};
Expand Down Expand Up @@ -329,6 +330,13 @@ impl<T:Ord + Copy> Ord for Cell<T> {
}
}

#[stable(feature = "cell_from", since = "1.12.0")]
impl<T: Copy> From<T> for Cell<T> {
fn from(t: T) -> Cell<T> {
Cell::new(t)
}
}

/// A mutable memory location with dynamically checked borrow rules
///
/// See the [module-level documentation](index.html) for more.
Expand Down Expand Up @@ -742,6 +750,13 @@ impl<T: ?Sized + Ord> Ord for RefCell<T> {
}
}

#[stable(feature = "cell_from", since = "1.12.0")]
impl<T> From<T> for RefCell<T> {
fn from(t: T) -> RefCell<T> {
RefCell::new(t)
}
}

struct BorrowRef<'b> {
borrow: &'b Cell<BorrowFlag>,
}
Expand Down Expand Up @@ -1064,3 +1079,10 @@ impl<T: Default> Default for UnsafeCell<T> {
UnsafeCell::new(Default::default())
}
}

#[stable(feature = "cell_from", since = "1.12.0")]
impl<T> From<T> for UnsafeCell<T> {
fn from(t: T) -> UnsafeCell<T> {
UnsafeCell::new(t)
}
}

0 comments on commit 7e2fa43

Please sign in to comment.