]> git.lizzy.rs Git - rust.git/commitdiff
impl `Deref` instead of `Index`
authorF001 <changchun.fan@qq.com>
Sun, 27 May 2018 23:52:39 +0000 (07:52 +0800)
committerF001 <changchun.fan@qq.com>
Tue, 17 Jul 2018 03:34:19 +0000 (11:34 +0800)
src/libcore/cell.rs
src/test/run-pass/rfc-1789-as-cell/from-mut.rs

index 97fee5d06f45db4ef398b462da41fe9f883b87fe..85ef417ea15c22646f4d01ee7901d5c029ff8a78 100644 (file)
 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.
 ///
@@ -510,8 +509,9 @@ pub fn get_mut(&mut self) -> &mut T {
     /// use std::cell::Cell;
     /// 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[..];
-    /// assert_eq!(slice_cell.len(), 3)
+    /// assert_eq!(slice_cell.len(), 3);
     /// ```
     #[inline]
     #[unstable(feature = "as_cell", issue="43038")]
@@ -546,15 +546,13 @@ pub fn take(&self) -> T {
 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;
+impl<T> Deref for Cell<[T]> {
+    type Target = [Cell<T>];
 
-    fn index(&self, index: I) -> &Self::Output {
+    #[inline]
+    fn deref(&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>])
         }
     }
 }
index 28b6d8213f714fdbcbed47a94c15db502b0dea8c..9989690bc790718ee886cc8aa3ea2c4a996264e8 100644 (file)
@@ -15,6 +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);
     let sub_slice : &[Cell<i32>] = &cell_slice[1..];
     assert_eq!(sub_slice.len(), 2);
 }