@@ -200,8 +200,9 @@ use cmp::Ordering;
200
200
use fmt:: { self , Debug , Display } ;
201
201
use marker:: Unsize ;
202
202
use mem;
203
- use ops:: { Deref , DerefMut , CoerceUnsized } ;
203
+ use ops:: { Deref , DerefMut , CoerceUnsized , Index } ;
204
204
use ptr;
205
+ use slice:: SliceIndex ;
205
206
206
207
/// A mutable memory location.
207
208
///
@@ -236,7 +237,7 @@ use ptr;
236
237
/// See the [module-level documentation](index.html) for more.
237
238
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
238
239
#[ repr( transparent) ]
239
- pub struct Cell < T > {
240
+ pub struct Cell < T : ? Sized > {
240
241
value : UnsafeCell < T > ,
241
242
}
242
243
@@ -287,10 +288,10 @@ impl<T:Copy> Cell<T> {
287
288
}
288
289
289
290
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
290
- unsafe impl < T > Send for Cell < T > where T : Send { }
291
+ unsafe impl < T : ? Sized > Send for Cell < T > where T : Send { }
291
292
292
293
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
293
- impl < T > !Sync for Cell < T > { }
294
+ impl < T : ? Sized > !Sync for Cell < T > { }
294
295
295
296
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
296
297
impl < T : Copy > Clone for Cell < T > {
@@ -381,46 +382,6 @@ impl<T> Cell<T> {
381
382
}
382
383
}
383
384
384
- /// Returns a raw pointer to the underlying data in this cell.
385
- ///
386
- /// # Examples
387
- ///
388
- /// ```
389
- /// use std::cell::Cell;
390
- ///
391
- /// let c = Cell::new(5);
392
- ///
393
- /// let ptr = c.as_ptr();
394
- /// ```
395
- #[ inline]
396
- #[ stable( feature = "cell_as_ptr" , since = "1.12.0" ) ]
397
- pub fn as_ptr ( & self ) -> * mut T {
398
- self . value . get ( )
399
- }
400
-
401
- /// Returns a mutable reference to the underlying data.
402
- ///
403
- /// This call borrows `Cell` mutably (at compile-time) which guarantees
404
- /// that we possess the only reference.
405
- ///
406
- /// # Examples
407
- ///
408
- /// ```
409
- /// use std::cell::Cell;
410
- ///
411
- /// let mut c = Cell::new(5);
412
- /// *c.get_mut() += 1;
413
- ///
414
- /// assert_eq!(c.get(), 6);
415
- /// ```
416
- #[ inline]
417
- #[ stable( feature = "cell_get_mut" , since = "1.11.0" ) ]
418
- pub fn get_mut ( & mut self ) -> & mut T {
419
- unsafe {
420
- & mut * self . value . get ( )
421
- }
422
- }
423
-
424
385
/// Sets the contained value.
425
386
///
426
387
/// # Examples
@@ -499,6 +460,90 @@ impl<T> Cell<T> {
499
460
}
500
461
}
501
462
463
+ impl < T : ?Sized > Cell < T > {
464
+ /// Returns a raw pointer to the underlying data in this cell.
465
+ ///
466
+ /// # Examples
467
+ ///
468
+ /// ```
469
+ /// use std::cell::Cell;
470
+ ///
471
+ /// let c = Cell::new(5);
472
+ ///
473
+ /// let ptr = c.as_ptr();
474
+ /// ```
475
+ #[ inline]
476
+ #[ stable( feature = "cell_as_ptr" , since = "1.12.0" ) ]
477
+ pub fn as_ptr ( & self ) -> * mut T {
478
+ self . value . get ( )
479
+ }
480
+
481
+ /// Returns a mutable reference to the underlying data.
482
+ ///
483
+ /// This call borrows `Cell` mutably (at compile-time) which guarantees
484
+ /// that we possess the only reference.
485
+ ///
486
+ /// # Examples
487
+ ///
488
+ /// ```
489
+ /// use std::cell::Cell;
490
+ ///
491
+ /// let mut c = Cell::new(5);
492
+ /// *c.get_mut() += 1;
493
+ ///
494
+ /// assert_eq!(c.get(), 6);
495
+ /// ```
496
+ #[ inline]
497
+ #[ stable( feature = "cell_get_mut" , since = "1.11.0" ) ]
498
+ pub fn get_mut ( & mut self ) -> & mut T {
499
+ unsafe {
500
+ & mut * self . value . get ( )
501
+ }
502
+ }
503
+
504
+ /// Returns a `&Cell<T>` from a `&mut T`
505
+ ///
506
+ /// # Examples
507
+ ///
508
+ /// ```
509
+ /// #![feature(as_cell)]
510
+ /// use std::cell::Cell;
511
+ /// let slice: &mut [i32] = &mut [1,2,3];
512
+ /// let cell_slice: &Cell<[i32]> = Cell::from_mut(slice);
513
+ ///
514
+ /// assert_eq!(cell_slice.get_with(|v|v.len()), 3)
515
+ /// ```
516
+ #[ inline]
517
+ #[ unstable( feature = "as_cell" , issue="43038" ) ]
518
+ pub fn from_mut < ' a > ( t : & ' a mut T ) -> & ' a Cell < T > {
519
+ unsafe {
520
+ & * ( t as * mut T as * const Cell < T > )
521
+ }
522
+ }
523
+
524
+ /// Returns a value by applying a function on contained value
525
+ ///
526
+ /// # Examples
527
+ ///
528
+ /// ```
529
+ /// #![feature(as_cell)]
530
+ /// use std::cell::Cell;
531
+ /// let c : Cell<Vec<i32>> = Cell::new(vec![1,2,3]);
532
+ /// let sum : i32 = c.get_with(|v|v.iter().sum());
533
+ /// assert_eq!(sum, 6_i32);
534
+ /// ```
535
+ #[ inline]
536
+ #[ unstable( feature = "as_cell" , issue="43038" ) ]
537
+ pub fn get_with < U , F > ( & self , f : F ) -> U
538
+ where
539
+ F : Fn ( & T ) -> U , U : ' static
540
+ {
541
+ unsafe {
542
+ f ( & * self . value . get ( ) )
543
+ }
544
+ }
545
+ }
546
+
502
547
impl < T : Default > Cell < T > {
503
548
/// Takes the value of the cell, leaving `Default::default()` in its place.
504
549
///
@@ -522,6 +567,20 @@ impl<T: Default> Cell<T> {
522
567
#[ unstable( feature = "coerce_unsized" , issue = "27732" ) ]
523
568
impl < T : CoerceUnsized < U > , U > CoerceUnsized < Cell < U > > for Cell < T > { }
524
569
570
+ #[ unstable( feature = "as_cell" , issue="43038" ) ]
571
+ impl < T , I > Index < I > for Cell < [ T ] >
572
+ where
573
+ I : SliceIndex < [ Cell < T > ] >
574
+ {
575
+ type Output = I :: Output ;
576
+
577
+ fn index ( & self , index : I ) -> & Self :: Output {
578
+ unsafe {
579
+ Index :: index ( & * ( self as * const Cell < [ T ] > as * const [ Cell < T > ] ) , index)
580
+ }
581
+ }
582
+ }
583
+
525
584
/// A mutable memory location with dynamically checked borrow rules
526
585
///
527
586
/// See the [module-level documentation](index.html) for more.
0 commit comments