Skip to content

Commit 489101c

Browse files
committed
use inherent method instead
1 parent 8812c6b commit 489101c

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

src/libcore/cell.rs

+20-12
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,8 @@ use cmp::Ordering;
200200
use fmt::{self, Debug, Display};
201201
use marker::Unsize;
202202
use mem;
203-
use ops::{Deref, DerefMut, CoerceUnsized, Index};
203+
use ops::{Deref, DerefMut, CoerceUnsized};
204204
use ptr;
205-
use slice::SliceIndex;
206205

207206
/// A mutable memory location.
208207
///
@@ -511,9 +510,8 @@ impl<T: ?Sized> Cell<T> {
511510
///
512511
/// let slice: &mut [i32] = &mut [1, 2, 3];
513512
/// let cell_slice: &Cell<[i32]> = Cell::from_mut(slice);
514-
/// assert_eq!(cell_slice[..].len(), 3);
513+
/// let slice_cell: &[Cell<i32>] = cell_slice.as_slice_of_cells();
515514
///
516-
/// let slice_cell: &[Cell<i32>] = &cell_slice[..];
517515
/// assert_eq!(slice_cell.len(), 3);
518516
/// ```
519517
#[inline]
@@ -548,15 +546,25 @@ impl<T: Default> Cell<T> {
548546
#[unstable(feature = "coerce_unsized", issue = "27732")]
549547
impl<T: CoerceUnsized<U>, U> CoerceUnsized<Cell<U>> for Cell<T> {}
550548

551-
#[unstable(feature = "as_cell", issue="43038")]
552-
impl<T, I> Index<I> for Cell<[T]>
553-
where I: SliceIndex<[Cell<T>]>
554-
{
555-
type Output = I::Output;
556-
557-
fn index(&self, index: I) -> &Self::Output {
549+
impl<T> Cell<[T]> {
550+
/// Returns a `&[Cell<T>]` from a `&Cell<[T]>`
551+
///
552+
/// # Examples
553+
///
554+
/// ```
555+
/// #![feature(as_cell)]
556+
/// use std::cell::Cell;
557+
///
558+
/// let slice: &mut [i32] = &mut [1, 2, 3];
559+
/// let cell_slice: &Cell<[i32]> = Cell::from_mut(slice);
560+
/// let slice_cell: &[Cell<i32>] = cell_slice.as_slice_of_cells();
561+
///
562+
/// assert_eq!(slice_cell.len(), 3);
563+
/// ```
564+
#[unstable(feature = "as_cell", issue="43038")]
565+
pub fn as_slice_of_cells(&self) -> &[Cell<T>] {
558566
unsafe {
559-
Index::index(&*(self as *const Cell<[T]> as *const [Cell<T>]), index)
567+
&*(self as *const Cell<[T]> as *const [Cell<T>])
560568
}
561569
}
562570
}

src/test/run-pass/rfc-1789-as-cell/from-mut.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ use std::cell::Cell;
1515
fn main() {
1616
let slice: &mut [i32] = &mut [1, 2, 3];
1717
let cell_slice: &Cell<[i32]> = Cell::from_mut(slice);
18-
assert_eq!(cell_slice[..].len(), 3);
18+
let slice_cell: &[Cell<i32>] = cell_slice.as_slice_of_cells();
1919

20-
let sub_slice: &[Cell<i32>] = &cell_slice[1..];
21-
assert_eq!(sub_slice.len(), 2);
20+
assert_eq!(slice_cell.len(), 3);
2221
}

0 commit comments

Comments
 (0)