diff options
| author | F001 <changchun.fan@qq.com> | 2018-05-28 07:52:39 +0800 |
|---|---|---|
| committer | F001 <changchun.fan@qq.com> | 2018-07-17 11:34:19 +0800 |
| commit | bdf86300b4170abef1399bb9b7bc63bac52a0092 (patch) | |
| tree | 194f7af7b416eebf0f9e5d156e863ab89ff6db2a | |
| parent | 4d99957ce30b6c5021d14c2812b12e817ec60c59 (diff) | |
| download | rust-bdf86300b4170abef1399bb9b7bc63bac52a0092.tar.gz rust-bdf86300b4170abef1399bb9b7bc63bac52a0092.zip | |
impl `Deref` instead of `Index`
| -rw-r--r-- | src/libcore/cell.rs | 18 | ||||
| -rw-r--r-- | src/test/run-pass/rfc-1789-as-cell/from-mut.rs | 1 |
2 files changed, 9 insertions, 10 deletions
diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index 97fee5d06f4..85ef417ea15 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -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. /// @@ -510,8 +509,9 @@ impl<T: ?Sized> Cell<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 @@ impl<T: Default> Cell<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>]) } } } 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 28b6d8213f7..9989690bc79 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,6 +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 sub_slice : &[Cell<i32>] = &cell_slice[1..]; assert_eq!(sub_slice.len(), 2); } |
