Skip to content

Commit

Permalink
use inherent method instead
Browse files Browse the repository at this point in the history
  • Loading branch information
F001 committed Jul 23, 2018
1 parent 8812c6b commit 489101c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
32 changes: 20 additions & 12 deletions src/libcore/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -511,9 +510,8 @@ impl<T: ?Sized> Cell<T> {
///
/// 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<i32>] = cell_slice.as_slice_of_cells();
///
/// let slice_cell: &[Cell<i32>] = &cell_slice[..];
/// assert_eq!(slice_cell.len(), 3);
/// ```
#[inline]
Expand Down Expand Up @@ -548,15 +546,25 @@ impl<T: Default> Cell<T> {
#[unstable(feature = "coerce_unsized", issue = "27732")]
impl<T: CoerceUnsized<U>, U> CoerceUnsized<Cell<U>> for Cell<T> {}

#[unstable(feature = "as_cell", issue="43038")]
impl<T, I> Index<I> for Cell<[T]>
where I: SliceIndex<[Cell<T>]>
{
type Output = I::Output;

fn index(&self, index: I) -> &Self::Output {
impl<T> Cell<[T]> {
/// Returns a `&[Cell<T>]` 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<i32>] = 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<T>] {
unsafe {
Index::index(&*(self as *const Cell<[T]> as *const [Cell<T>]), index)
&*(self as *const Cell<[T]> as *const [Cell<T>])
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/test/run-pass/rfc-1789-as-cell/from-mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<i32>] = cell_slice.as_slice_of_cells();

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

0 comments on commit 489101c

Please sign in to comment.