]> git.lizzy.rs Git - rust.git/commitdiff
revert Deref
authorF001 <changchun.fan@qq.com>
Tue, 17 Jul 2018 03:30:17 +0000 (11:30 +0800)
committerF001 <changchun.fan@qq.com>
Wed, 18 Jul 2018 04:30:20 +0000 (12:30 +0800)
src/libcore/cell.rs
src/test/run-pass/rfc-1789-as-cell/from-mut.rs

index 7e2ece0f02c1c21eb2f908a9c00e4be3cfb74ffe..8a0f499b598a594df503fa1c8a6c59435f203184 100644 (file)
 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 @@ pub fn get_mut(&mut self) -> &mut 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 @@ pub fn take(&self) -> 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)
         }
     }
 }
index 8c7317b0e9a59630b2dd4e65d47a285fc27c85e7..bd00f305cc49a4c7ab0089498e75409b7f761c82 100644 (file)
@@ -15,7 +15,7 @@
 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);