141
141
142
142
#![ stable( feature = "rust1" , since = "1.0.0" ) ]
143
143
144
+ // TODO(japaric) update docs
145
+
144
146
use clone:: Clone ;
145
147
use cmp:: PartialEq ;
146
148
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 ;
148
156
use ops:: { Deref , DerefMut , Drop } ;
149
157
use option:: Option ;
150
158
use option:: Option :: { None , Some } ;
@@ -157,6 +165,7 @@ pub struct Cell<T> {
157
165
value : UnsafeCell < T > ,
158
166
}
159
167
168
+ #[ cfg( stage0) ]
160
169
impl < T : Copy > Cell < T > {
161
170
/// Creates a new `Cell` containing the given value.
162
171
///
@@ -232,16 +241,104 @@ impl<T:Copy> Cell<T> {
232
241
}
233
242
}
234
243
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
+
235
322
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
236
323
unsafe impl < T > Send for Cell < T > where T : Send { }
237
324
325
+ #[ cfg( stage0) ]
238
326
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
239
327
impl < T : Copy > Clone for Cell < T > {
240
328
fn clone ( & self ) -> Cell < T > {
241
329
Cell :: new ( self . get ( ) )
242
330
}
243
331
}
244
332
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) ]
245
342
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
246
343
impl < T : Default + Copy > Default for Cell < T > {
247
344
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -250,13 +347,31 @@ impl<T:Default + Copy> Default for Cell<T> {
250
347
}
251
348
}
252
349
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) ]
253
360
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
254
361
impl < T : PartialEq + Copy > PartialEq for Cell < T > {
255
362
fn eq ( & self , other : & Cell < T > ) -> bool {
256
363
self . get ( ) == other. get ( )
257
364
}
258
365
}
259
366
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
+
260
375
/// A mutable memory location with dynamically checked borrow rules
261
376
///
262
377
/// See the [module-level documentation](index.html) for more.
0 commit comments