Rollup merge of #39793 - RalfJung:cell, r=alexcrichton
Allow more Cell methods for non-Copy types Clearly, `get_mut` is safe for any `T`. The other two only provide unsafe pointers anyway. The only remaining inherent method with `Copy` bound is `get`, which sounds about right to me. I found the order if `impl` blocks in the file a little weird (first inherent impl, then some trait impls, then another inherent impl), but didn't change it to keep the diff small. Contributes to #39264
This commit is contained in:
commit
3f95303db6
|
@ -213,6 +213,102 @@ impl<T:Copy> Cell<T> {
|
||||||
pub fn get(&self) -> T {
|
pub fn get(&self) -> T {
|
||||||
unsafe{ *self.value.get() }
|
unsafe{ *self.value.get() }
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
|
unsafe impl<T> Send for Cell<T> where T: Send {}
|
||||||
|
|
||||||
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
|
impl<T> !Sync for Cell<T> {}
|
||||||
|
|
||||||
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
|
impl<T:Copy> Clone for Cell<T> {
|
||||||
|
#[inline]
|
||||||
|
fn clone(&self) -> Cell<T> {
|
||||||
|
Cell::new(self.get())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
|
impl<T:Default> Default for Cell<T> {
|
||||||
|
/// Creates a `Cell<T>`, with the `Default` value for T.
|
||||||
|
#[inline]
|
||||||
|
fn default() -> Cell<T> {
|
||||||
|
Cell::new(Default::default())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
|
impl<T:PartialEq + Copy> PartialEq for Cell<T> {
|
||||||
|
#[inline]
|
||||||
|
fn eq(&self, other: &Cell<T>) -> bool {
|
||||||
|
self.get() == other.get()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[stable(feature = "cell_eq", since = "1.2.0")]
|
||||||
|
impl<T:Eq + Copy> Eq for Cell<T> {}
|
||||||
|
|
||||||
|
#[stable(feature = "cell_ord", since = "1.10.0")]
|
||||||
|
impl<T:PartialOrd + Copy> PartialOrd for Cell<T> {
|
||||||
|
#[inline]
|
||||||
|
fn partial_cmp(&self, other: &Cell<T>) -> Option<Ordering> {
|
||||||
|
self.get().partial_cmp(&other.get())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn lt(&self, other: &Cell<T>) -> bool {
|
||||||
|
self.get() < other.get()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn le(&self, other: &Cell<T>) -> bool {
|
||||||
|
self.get() <= other.get()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn gt(&self, other: &Cell<T>) -> bool {
|
||||||
|
self.get() > other.get()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn ge(&self, other: &Cell<T>) -> bool {
|
||||||
|
self.get() >= other.get()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[stable(feature = "cell_ord", since = "1.10.0")]
|
||||||
|
impl<T:Ord + Copy> Ord for Cell<T> {
|
||||||
|
#[inline]
|
||||||
|
fn cmp(&self, other: &Cell<T>) -> Ordering {
|
||||||
|
self.get().cmp(&other.get())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[stable(feature = "cell_from", since = "1.12.0")]
|
||||||
|
impl<T> From<T> for Cell<T> {
|
||||||
|
fn from(t: T) -> Cell<T> {
|
||||||
|
Cell::new(t)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Cell<T> {
|
||||||
|
/// 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<T> {
|
||||||
|
Cell {
|
||||||
|
value: UnsafeCell::new(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns a reference to the underlying `UnsafeCell`.
|
/// Returns a reference to the underlying `UnsafeCell`.
|
||||||
///
|
///
|
||||||
|
@ -273,102 +369,6 @@ impl<T:Copy> Cell<T> {
|
||||||
&mut *self.value.get()
|
&mut *self.value.get()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
|
||||||
unsafe impl<T> Send for Cell<T> where T: Send {}
|
|
||||||
|
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
|
||||||
impl<T> !Sync for Cell<T> {}
|
|
||||||
|
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
|
||||||
impl<T:Copy> Clone for Cell<T> {
|
|
||||||
#[inline]
|
|
||||||
fn clone(&self) -> Cell<T> {
|
|
||||||
Cell::new(self.get())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
|
||||||
impl<T:Default + Copy> Default for Cell<T> {
|
|
||||||
/// Creates a `Cell<T>`, with the `Default` value for T.
|
|
||||||
#[inline]
|
|
||||||
fn default() -> Cell<T> {
|
|
||||||
Cell::new(Default::default())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
|
||||||
impl<T:PartialEq + Copy> PartialEq for Cell<T> {
|
|
||||||
#[inline]
|
|
||||||
fn eq(&self, other: &Cell<T>) -> bool {
|
|
||||||
self.get() == other.get()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[stable(feature = "cell_eq", since = "1.2.0")]
|
|
||||||
impl<T:Eq + Copy> Eq for Cell<T> {}
|
|
||||||
|
|
||||||
#[stable(feature = "cell_ord", since = "1.10.0")]
|
|
||||||
impl<T:PartialOrd + Copy> PartialOrd for Cell<T> {
|
|
||||||
#[inline]
|
|
||||||
fn partial_cmp(&self, other: &Cell<T>) -> Option<Ordering> {
|
|
||||||
self.get().partial_cmp(&other.get())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn lt(&self, other: &Cell<T>) -> bool {
|
|
||||||
self.get() < other.get()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn le(&self, other: &Cell<T>) -> bool {
|
|
||||||
self.get() <= other.get()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn gt(&self, other: &Cell<T>) -> bool {
|
|
||||||
self.get() > other.get()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn ge(&self, other: &Cell<T>) -> bool {
|
|
||||||
self.get() >= other.get()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[stable(feature = "cell_ord", since = "1.10.0")]
|
|
||||||
impl<T:Ord + Copy> Ord for Cell<T> {
|
|
||||||
#[inline]
|
|
||||||
fn cmp(&self, other: &Cell<T>) -> Ordering {
|
|
||||||
self.get().cmp(&other.get())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> Cell<T> {
|
|
||||||
/// 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<T> {
|
|
||||||
Cell {
|
|
||||||
value: UnsafeCell::new(value),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Sets the contained value.
|
/// Sets the contained value.
|
||||||
///
|
///
|
||||||
|
|
Loading…
Reference in New Issue