]> git.lizzy.rs Git - rust.git/blobdiff - src/libcollections/vec.rs
make `IndexMut` a super trait over `Index`
[rust.git] / src / libcollections / vec.rs
index 071c08728cb0321166cb8ce2071c6bbe689b5d1e..4a082c3616cb91bbfe2d5d1e2332f201f5f4ecbb 100644 (file)
@@ -234,7 +234,7 @@ pub fn with_capacity(capacity: usize) -> Vec<T> {
     ///         mem::forget(v);
     ///
     ///         // Overwrite memory with 4, 5, 6
-    ///         for i in 0..len as int {
+    ///         for i in 0..len as isize {
     ///             ptr::write(p.offset(i), 4 + i);
     ///         }
     ///
@@ -457,7 +457,7 @@ pub fn into_iter(self) -> IntoIter<T> {
             let end = if mem::size_of::<T>() == 0 {
                 (ptr as usize + self.len()) as *const T
             } else {
-                ptr.offset(self.len() as int) as *const T
+                ptr.offset(self.len() as isize) as *const T
             };
             mem::forget(self);
             IntoIter { allocation: ptr, cap: cap, ptr: begin, end: end }
@@ -473,7 +473,7 @@ pub fn into_iter(self) -> IntoIter<T> {
     /// # Examples
     ///
     /// ```
-    /// let mut v = vec![1u, 2, 3, 4];
+    /// let mut v = vec![1, 2, 3, 4];
     /// unsafe {
     ///     v.set_len(1);
     /// }
@@ -539,7 +539,7 @@ pub fn insert(&mut self, index: usize, element: T) {
         unsafe { // infallible
             // The spot to put the new value
             {
-                let p = self.as_mut_ptr().offset(index as int);
+                let p = self.as_mut_ptr().offset(index as isize);
                 // Shift everything over to make space. (Duplicating the
                 // `index`th element into two consecutive places.)
                 ptr::copy_memory(p.offset(1), &*p, len - index);
@@ -573,7 +573,7 @@ pub fn remove(&mut self, index: usize) -> T {
             let ret;
             {
                 // the place we are taking from.
-                let ptr = self.as_mut_ptr().offset(index as int);
+                let ptr = self.as_mut_ptr().offset(index as isize);
                 // copy it out, unsafely having a copy of the value on
                 // the stack and in the vector at the same time.
                 ret = ptr::read(ptr);
@@ -655,7 +655,7 @@ pub fn push(&mut self, value: T) {
         }
 
         unsafe {
-            let end = (*self.ptr).offset(self.len as int);
+            let end = (*self.ptr).offset(self.len as isize);
             ptr::write(&mut *end, value);
             self.len += 1;
         }
@@ -743,7 +743,7 @@ pub fn drain(&mut self) -> Drain<T> {
             let end = if mem::size_of::<T>() == 0 {
                 (*self.ptr as usize + self.len()) as *const T
             } else {
-                (*self.ptr).offset(self.len() as int) as *const T
+                (*self.ptr).offset(self.len() as isize) as *const T
             };
             self.set_len(0);
             Drain {
@@ -835,7 +835,7 @@ pub fn map_in_place<U, F>(self, mut f: F) -> Vec<U> where F: FnMut(T) -> U {
             // types are passed to the allocator by `Vec`.
             assert!(mem::min_align_of::<T>() == mem::min_align_of::<U>());
 
-            // This `as int` cast is safe, because the size of the elements of the
+            // This `as isize` cast is safe, because the size of the elements of the
             // vector is not 0, and:
             //
             // 1) If the size of the elements in the vector is 1, the `int` may
@@ -852,7 +852,7 @@ pub fn map_in_place<U, F>(self, mut f: F) -> Vec<U> where F: FnMut(T) -> U {
             //
             // 2) If the size of the elements in the vector is >1, the `usize` ->
             //    `int` conversion can't overflow.
-            let offset = vec.len() as int;
+            let offset = vec.len() as isize;
             let start = vec.as_mut_ptr();
 
             let mut pv = PartialVecNonZeroSized {
@@ -1179,8 +1179,8 @@ pub fn dedup(&mut self) {
             let mut w = 1;
 
             while r < ln {
-                let p_r = p.offset(r as int);
-                let p_wm1 = p.offset((w - 1) as int);
+                let p_r = p.offset(r as isize);
+                let p_wm1 = p.offset((w - 1) as isize);
                 if *p_r != *p_wm1 {
                     if r != w {
                         let p_w = p_wm1.offset(1);
@@ -1286,8 +1286,6 @@ fn index(&self, index: &usize) -> &T {
 
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<T> IndexMut<usize> for Vec<T> {
-    type Output = T;
-
     #[inline]
     fn index_mut(&mut self, index: &usize) -> &mut T {
         // NB built-in indexing via `&mut [T]`
@@ -1331,7 +1329,6 @@ fn index(&self, _index: &ops::RangeFull) -> &[T] {
 
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<T> ops::IndexMut<ops::Range<usize>> for Vec<T> {
-    type Output = [T];
     #[inline]
     fn index_mut(&mut self, index: &ops::Range<usize>) -> &mut [T] {
         IndexMut::index_mut(&mut **self, index)
@@ -1339,7 +1336,6 @@ fn index_mut(&mut self, index: &ops::Range<usize>) -> &mut [T] {
 }
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<T> ops::IndexMut<ops::RangeTo<usize>> for Vec<T> {
-    type Output = [T];
     #[inline]
     fn index_mut(&mut self, index: &ops::RangeTo<usize>) -> &mut [T] {
         IndexMut::index_mut(&mut **self, index)
@@ -1347,7 +1343,6 @@ fn index_mut(&mut self, index: &ops::RangeTo<usize>) -> &mut [T] {
 }
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<T> ops::IndexMut<ops::RangeFrom<usize>> for Vec<T> {
-    type Output = [T];
     #[inline]
     fn index_mut(&mut self, index: &ops::RangeFrom<usize>) -> &mut [T] {
         IndexMut::index_mut(&mut **self, index)
@@ -1355,7 +1350,6 @@ fn index_mut(&mut self, index: &ops::RangeFrom<usize>) -> &mut [T] {
 }
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<T> ops::IndexMut<ops::RangeFull> for Vec<T> {
-    type Output = [T];
     #[inline]
     fn index_mut(&mut self, _index: &ops::RangeFull) -> &mut [T] {
         self.as_mut_slice()
@@ -1648,7 +1642,7 @@ fn next(&mut self) -> Option<T> {
                     self.ptr = mem::transmute(self.ptr as usize + 1);
 
                     // Use a non-null pointer value
-                    Some(ptr::read(mem::transmute(1u)))
+                    Some(ptr::read(EMPTY as *mut T))
                 } else {
                     let old = self.ptr;
                     self.ptr = self.ptr.offset(1);
@@ -1681,7 +1675,7 @@ fn next_back(&mut self) -> Option<T> {
                     self.end = mem::transmute(self.end as usize - 1);
 
                     // Use a non-null pointer value
-                    Some(ptr::read(mem::transmute(1u)))
+                    Some(ptr::read(EMPTY as *mut T))
                 } else {
                     self.end = self.end.offset(-1);
 
@@ -1736,7 +1730,7 @@ fn next(&mut self) -> Option<T> {
                     self.ptr = mem::transmute(self.ptr as usize + 1);
 
                     // Use a non-null pointer value
-                    Some(ptr::read(mem::transmute(1u)))
+                    Some(ptr::read(EMPTY as *mut T))
                 } else {
                     let old = self.ptr;
                     self.ptr = self.ptr.offset(1);
@@ -1769,7 +1763,7 @@ fn next_back(&mut self) -> Option<T> {
                     self.end = mem::transmute(self.end as usize - 1);
 
                     // Use a non-null pointer value
-                    Some(ptr::read(mem::transmute(1u)))
+                    Some(ptr::read(EMPTY as *mut T))
                 } else {
                     self.end = self.end.offset(-1);
 
@@ -2146,10 +2140,10 @@ fn zero_sized_values() {
 
     #[test]
     fn test_partition() {
-        assert_eq!(vec![].into_iter().partition(|x: &int| *x < 3), (vec![], vec![]));
-        assert_eq!(vec![1, 2, 3].into_iter().partition(|x: &int| *x < 4), (vec![1, 2, 3], vec![]));
-        assert_eq!(vec![1, 2, 3].into_iter().partition(|x: &int| *x < 2), (vec![1], vec![2, 3]));
-        assert_eq!(vec![1, 2, 3].into_iter().partition(|x: &int| *x < 0), (vec![], vec![1, 2, 3]));
+        assert_eq!(vec![].into_iter().partition(|x: &i32| *x < 3), (vec![], vec![]));
+        assert_eq!(vec![1, 2, 3].into_iter().partition(|x| *x < 4), (vec![1, 2, 3], vec![]));
+        assert_eq!(vec![1, 2, 3].into_iter().partition(|x| *x < 2), (vec![1], vec![2, 3]));
+        assert_eq!(vec![1, 2, 3].into_iter().partition(|x| *x < 0), (vec![], vec![1, 2, 3]));
     }
 
     #[test]
@@ -2183,7 +2177,7 @@ fn test_unsafe_ptrs() {
     #[test]
     fn test_vec_truncate_drop() {
         static mut drops: u32 = 0;
-        struct Elem(int);
+        struct Elem(i32);
         impl Drop for Elem {
             fn drop(&mut self) {
                 unsafe { drops += 1; }