diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index 8a0f499b598a5..b6087628ea6a4 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -200,9 +200,8 @@ use cmp::Ordering; use fmt::{self, Debug, Display}; use marker::Unsize; use mem; -use ops::{Deref, DerefMut, CoerceUnsized, Index}; +use ops::{Deref, DerefMut, CoerceUnsized}; use ptr; -use slice::SliceIndex; /// A mutable memory location. /// @@ -511,9 +510,8 @@ impl Cell { /// /// let slice: &mut [i32] = &mut [1, 2, 3]; /// let cell_slice: &Cell<[i32]> = Cell::from_mut(slice); - /// assert_eq!(cell_slice[..].len(), 3); + /// let slice_cell: &[Cell] = cell_slice.as_slice_of_cells(); /// - /// let slice_cell: &[Cell] = &cell_slice[..]; /// assert_eq!(slice_cell.len(), 3); /// ``` #[inline] @@ -548,15 +546,25 @@ impl Cell { #[unstable(feature = "coerce_unsized", issue = "27732")] impl, U> CoerceUnsized> for Cell {} -#[unstable(feature = "as_cell", issue="43038")] -impl Index for Cell<[T]> - where I: SliceIndex<[Cell]> -{ - type Output = I::Output; - - fn index(&self, index: I) -> &Self::Output { +impl Cell<[T]> { + /// Returns a `&[Cell]` from a `&Cell<[T]>` + /// + /// # Examples + /// + /// ``` + /// #![feature(as_cell)] + /// use std::cell::Cell; + /// + /// let slice: &mut [i32] = &mut [1, 2, 3]; + /// let cell_slice: &Cell<[i32]> = Cell::from_mut(slice); + /// let slice_cell: &[Cell] = cell_slice.as_slice_of_cells(); + /// + /// assert_eq!(slice_cell.len(), 3); + /// ``` + #[unstable(feature = "as_cell", issue="43038")] + pub fn as_slice_of_cells(&self) -> &[Cell] { unsafe { - Index::index(&*(self as *const Cell<[T]> as *const [Cell]), index) + &*(self as *const Cell<[T]> as *const [Cell]) } } } diff --git a/src/test/run-pass/rfc-1789-as-cell/from-mut.rs b/src/test/run-pass/rfc-1789-as-cell/from-mut.rs index bd00f305cc49a..88f7fbc86df6e 100644 --- a/src/test/run-pass/rfc-1789-as-cell/from-mut.rs +++ b/src/test/run-pass/rfc-1789-as-cell/from-mut.rs @@ -15,8 +15,7 @@ use std::cell::Cell; fn main() { let slice: &mut [i32] = &mut [1, 2, 3]; let cell_slice: &Cell<[i32]> = Cell::from_mut(slice); - assert_eq!(cell_slice[..].len(), 3); + let slice_cell: &[Cell] = cell_slice.as_slice_of_cells(); - let sub_slice: &[Cell] = &cell_slice[1..]; - assert_eq!(sub_slice.len(), 2); + assert_eq!(slice_cell.len(), 3); }