From 8812c6bae92d1a8e3a255d3eacd40cd9b65bb7f6 Mon Sep 17 00:00:00 2001 From: F001 Date: Tue, 17 Jul 2018 11:30:17 +0800 Subject: [PATCH] revert Deref --- src/libcore/cell.rs | 26 +++++++------------ .../run-pass/rfc-1789-as-cell/from-mut.rs | 2 +- 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index 7e2ece0f02c1..8a0f499b598a 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -200,8 +200,9 @@ use cmp::Ordering; use fmt::{self, Debug, Display}; use marker::Unsize; use mem; -use ops::{Deref, DerefMut, CoerceUnsized}; +use ops::{Deref, DerefMut, CoerceUnsized, Index}; use ptr; +use slice::SliceIndex; /// A mutable memory location. /// @@ -510,7 +511,7 @@ 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); + /// assert_eq!(cell_slice[..].len(), 3); /// /// let slice_cell: &[Cell] = &cell_slice[..]; /// assert_eq!(slice_cell.len(), 3); @@ -548,23 +549,14 @@ impl Cell { impl, U> CoerceUnsized> for Cell {} #[unstable(feature = "as_cell", issue="43038")] -impl Deref for Cell<[T]> { - type Target = [Cell]; +impl Index for Cell<[T]> + where I: SliceIndex<[Cell]> +{ + type Output = I::Output; - #[inline] - fn deref(&self) -> &[Cell] { + fn index(&self, index: I) -> &Self::Output { unsafe { - &*(self as *const Cell<[T]> as *const [Cell]) - } - } -} - -#[unstable(feature = "as_cell", issue="43038")] -impl DerefMut for Cell<[T]> { - #[inline] - fn deref_mut(&mut self) -> &mut [Cell] { - unsafe { - &mut *(self as *mut Cell<[T]> as *mut [Cell]) + Index::index(&*(self as *const Cell<[T]> as *const [Cell]), index) } } } 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 8c7317b0e9a5..bd00f305cc49 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,7 +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); + assert_eq!(cell_slice[..].len(), 3); let sub_slice: &[Cell] = &cell_slice[1..]; assert_eq!(sub_slice.len(), 2);