Skip to content

Commit 9928baa

Browse files
committed
implement rfc 1789
1 parent 55c04ba commit 9928baa

File tree

2 files changed

+125
-44
lines changed

2 files changed

+125
-44
lines changed

src/libcore/cell.rs

Lines changed: 103 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,9 @@ use cmp::Ordering;
200200
use fmt::{self, Debug, Display};
201201
use marker::Unsize;
202202
use mem;
203-
use ops::{Deref, DerefMut, CoerceUnsized};
203+
use ops::{Deref, DerefMut, CoerceUnsized, Index};
204204
use ptr;
205+
use slice::SliceIndex;
205206

206207
/// A mutable memory location.
207208
///
@@ -236,7 +237,7 @@ use ptr;
236237
/// See the [module-level documentation](index.html) for more.
237238
#[stable(feature = "rust1", since = "1.0.0")]
238239
#[repr(transparent)]
239-
pub struct Cell<T> {
240+
pub struct Cell<T: ?Sized> {
240241
value: UnsafeCell<T>,
241242
}
242243

@@ -287,10 +288,10 @@ impl<T:Copy> Cell<T> {
287288
}
288289

289290
#[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 {}
291292

292293
#[stable(feature = "rust1", since = "1.0.0")]
293-
impl<T> !Sync for Cell<T> {}
294+
impl<T: ?Sized> !Sync for Cell<T> {}
294295

295296
#[stable(feature = "rust1", since = "1.0.0")]
296297
impl<T:Copy> Clone for Cell<T> {
@@ -381,46 +382,6 @@ impl<T> Cell<T> {
381382
}
382383
}
383384

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-
424385
/// Sets the contained value.
425386
///
426387
/// # Examples
@@ -499,6 +460,90 @@ impl<T> Cell<T> {
499460
}
500461
}
501462

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+
502547
impl<T: Default> Cell<T> {
503548
/// Takes the value of the cell, leaving `Default::default()` in its place.
504549
///
@@ -522,6 +567,20 @@ impl<T: Default> Cell<T> {
522567
#[unstable(feature = "coerce_unsized", issue = "27732")]
523568
impl<T: CoerceUnsized<U>, U> CoerceUnsized<Cell<U>> for Cell<T> {}
524569

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+
525584
/// A mutable memory location with dynamically checked borrow rules
526585
///
527586
/// See the [module-level documentation](index.html) for more.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(as_cell)]
12+
13+
use std::cell::Cell;
14+
15+
fn main() {
16+
let slice: &mut [i32] = &mut [1,2,3];
17+
let cell_slice: &Cell<[i32]> = Cell::from_mut(slice);
18+
assert_eq!(cell_slice.get_with(|v|v.len()), 3);
19+
20+
let sub_slice : &[Cell<i32>] = &cell_slice[1..];
21+
assert_eq!(sub_slice.len(), 2);
22+
}

0 commit comments

Comments
 (0)