diff options
| author | F001 <changchun.fan@qq.com> | 2018-07-17 11:30:17 +0800 |
|---|---|---|
| committer | F001 <changchun.fan@qq.com> | 2018-07-18 12:30:20 +0800 |
| commit | 8812c6bae92d1a8e3a255d3eacd40cd9b65bb7f6 (patch) | |
| tree | 1fc8a4603a8f02236275dbdf8c9ed925f1496ec7 | |
| parent | 7e1b983579ec582dfbe409342756252f6b080918 (diff) | |
| download | rust-8812c6bae92d1a8e3a255d3eacd40cd9b65bb7f6.tar.gz rust-8812c6bae92d1a8e3a255d3eacd40cd9b65bb7f6.zip | |
revert Deref
| -rw-r--r-- | src/libcore/cell.rs | 26 | ||||
| -rw-r--r-- | src/test/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 7e2ece0f02c..8a0f499b598 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<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); + /// assert_eq!(cell_slice[..].len(), 3); /// /// let slice_cell: &[Cell<i32>] = &cell_slice[..]; /// assert_eq!(slice_cell.len(), 3); @@ -548,23 +549,14 @@ impl<T: Default> Cell<T> { impl<T: CoerceUnsized<U>, U> CoerceUnsized<Cell<U>> for Cell<T> {} #[unstable(feature = "as_cell", issue="43038")] -impl<T> Deref for Cell<[T]> { - type Target = [Cell<T>]; +impl<T, I> Index<I> for Cell<[T]> + where I: SliceIndex<[Cell<T>]> +{ + type Output = I::Output; - #[inline] - fn deref(&self) -> &[Cell<T>] { - unsafe { - &*(self as *const Cell<[T]> as *const [Cell<T>]) - } - } -} - -#[unstable(feature = "as_cell", issue="43038")] -impl<T> DerefMut for Cell<[T]> { - #[inline] - fn deref_mut(&mut self) -> &mut [Cell<T>] { + fn index(&self, index: I) -> &Self::Output { unsafe { - &mut *(self as *mut Cell<[T]> as *mut [Cell<T>]) + Index::index(&*(self as *const Cell<[T]> as *const [Cell<T>]), 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 8c7317b0e9a..bd00f305cc4 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<i32>] = &cell_slice[1..]; assert_eq!(sub_slice.len(), 2); |
