Skip to content

Commit efb4cc4

Browse files
author
Jorge Aparicio
committed
admit Pod data in Cell
1 parent 5924d7e commit efb4cc4

File tree

1 file changed

+116
-1
lines changed

1 file changed

+116
-1
lines changed

src/libcore/cell.rs

Lines changed: 116 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,18 @@
141141
142142
#![stable(feature = "rust1", since = "1.0.0")]
143143

144+
// TODO(japaric) update docs
145+
144146
use clone::Clone;
145147
use cmp::PartialEq;
146148
use default::Default;
147-
use marker::{Copy, Send, Sync};
149+
#[cfg(not(stage0))]
150+
use mem;
151+
#[cfg(stage0)]
152+
use marker::Copy;
153+
use marker::{Send, Sync};
154+
#[cfg(not(stage0))]
155+
use marker::Pod;
148156
use ops::{Deref, DerefMut, Drop};
149157
use option::Option;
150158
use option::Option::{None, Some};
@@ -157,6 +165,7 @@ pub struct Cell<T> {
157165
value: UnsafeCell<T>,
158166
}
159167

168+
#[cfg(stage0)]
160169
impl<T:Copy> Cell<T> {
161170
/// Creates a new `Cell` containing the given value.
162171
///
@@ -232,16 +241,104 @@ impl<T:Copy> Cell<T> {
232241
}
233242
}
234243

244+
#[cfg(not(stage0))]
245+
impl<T:Pod> Cell<T> {
246+
/// Creates a new `Cell` containing the given value.
247+
///
248+
/// # Examples
249+
///
250+
/// ```
251+
/// use std::cell::Cell;
252+
///
253+
/// let c = Cell::new(5);
254+
/// ```
255+
#[stable(feature = "rust1", since = "1.0.0")]
256+
pub fn new(value: T) -> Cell<T> {
257+
Cell {
258+
value: UnsafeCell::new(value),
259+
}
260+
}
261+
262+
/// Returns a copy of the contained value.
263+
///
264+
/// # Examples
265+
///
266+
/// ```
267+
/// use std::cell::Cell;
268+
///
269+
/// let c = Cell::new(5);
270+
///
271+
/// let five = c.get();
272+
/// ```
273+
#[inline]
274+
#[stable(feature = "rust1", since = "1.0.0")]
275+
pub fn get(&self) -> T {
276+
unsafe {
277+
mem::transmute_copy(mem::transmute::<_, &T>(self.value.get()))
278+
}
279+
}
280+
281+
/// Sets the contained value.
282+
///
283+
/// # Examples
284+
///
285+
/// ```
286+
/// use std::cell::Cell;
287+
///
288+
/// let c = Cell::new(5);
289+
///
290+
/// c.set(10);
291+
/// ```
292+
#[inline]
293+
#[stable(feature = "rust1", since = "1.0.0")]
294+
pub fn set(&self, value: T) {
295+
unsafe {
296+
*self.value.get() = value;
297+
}
298+
}
299+
300+
/// Get a reference to the underlying `UnsafeCell`.
301+
///
302+
/// # Unsafety
303+
///
304+
/// This function is `unsafe` because `UnsafeCell`'s field is public.
305+
///
306+
/// # Examples
307+
///
308+
/// ```
309+
/// use std::cell::Cell;
310+
///
311+
/// let c = Cell::new(5);
312+
///
313+
/// let uc = unsafe { c.as_unsafe_cell() };
314+
/// ```
315+
#[inline]
316+
#[unstable(feature = "core")]
317+
pub unsafe fn as_unsafe_cell<'a>(&'a self) -> &'a UnsafeCell<T> {
318+
&self.value
319+
}
320+
}
321+
235322
#[stable(feature = "rust1", since = "1.0.0")]
236323
unsafe impl<T> Send for Cell<T> where T: Send {}
237324

325+
#[cfg(stage0)]
238326
#[stable(feature = "rust1", since = "1.0.0")]
239327
impl<T:Copy> Clone for Cell<T> {
240328
fn clone(&self) -> Cell<T> {
241329
Cell::new(self.get())
242330
}
243331
}
244332

333+
#[cfg(not(stage0))]
334+
#[stable(feature = "rust1", since = "1.0.0")]
335+
impl<T:Pod> Clone for Cell<T> {
336+
fn clone(&self) -> Cell<T> {
337+
Cell::new(self.get())
338+
}
339+
}
340+
341+
#[cfg(stage0)]
245342
#[stable(feature = "rust1", since = "1.0.0")]
246343
impl<T:Default + Copy> Default for Cell<T> {
247344
#[stable(feature = "rust1", since = "1.0.0")]
@@ -250,13 +347,31 @@ impl<T:Default + Copy> Default for Cell<T> {
250347
}
251348
}
252349

350+
#[cfg(not(stage0))]
351+
#[stable(feature = "rust1", since = "1.0.0")]
352+
impl<T:Default + Pod> Default for Cell<T> {
353+
#[stable(feature = "rust1", since = "1.0.0")]
354+
fn default() -> Cell<T> {
355+
Cell::new(Default::default())
356+
}
357+
}
358+
359+
#[cfg(stage0)]
253360
#[stable(feature = "rust1", since = "1.0.0")]
254361
impl<T:PartialEq + Copy> PartialEq for Cell<T> {
255362
fn eq(&self, other: &Cell<T>) -> bool {
256363
self.get() == other.get()
257364
}
258365
}
259366

367+
#[cfg(not(stage0))]
368+
#[stable(feature = "rust1", since = "1.0.0")]
369+
impl<T:PartialEq + Pod> PartialEq for Cell<T> {
370+
fn eq(&self, other: &Cell<T>) -> bool {
371+
self.get() == other.get()
372+
}
373+
}
374+
260375
/// A mutable memory location with dynamically checked borrow rules
261376
///
262377
/// See the [module-level documentation](index.html) for more.

0 commit comments

Comments
 (0)