]> git.lizzy.rs Git - rust.git/commitdiff
Make the Vec data structure layout match raw::Slice.
authorClark Gaebel <cgaebel@mozilla.com>
Sat, 25 Oct 2014 02:31:17 +0000 (19:31 -0700)
committerClark Gaebel <cgaebel@mozilla.com>
Sat, 25 Oct 2014 03:12:53 +0000 (20:12 -0700)
Fixes #18302

r? @thestinger

src/libcollections/slice.rs
src/libcollections/string.rs
src/libcollections/vec.rs
src/libsyntax/owned_slice.rs

index d061e60a42265d95f97e298a2c34f377225a8897..d4115df7da4787e13e9c43e219d90d4606ea1c02 100644 (file)
@@ -292,7 +292,7 @@ impl<T> BoxedSlice<T> for Box<[T]> {
     #[experimental]
     fn into_vec(mut self) -> Vec<T> {
         unsafe {
-            let xs = Vec::from_raw_parts(self.len(), self.len(), self.as_mut_ptr());
+            let xs = Vec::from_raw_parts(self.as_mut_ptr(), self.len(), self.len());
             mem::forget(self);
             xs
         }
index fa45dee7cdea7c2756b692add3cf3ef9d5d59ec9..c44a03b05cd35d7f3f6664dc68f0d871e1757cb6 100644 (file)
@@ -780,7 +780,7 @@ pub mod raw {
     #[inline]
     pub unsafe fn from_parts(buf: *mut u8, length: uint, capacity: uint) -> String {
         String {
-            vec: Vec::from_raw_parts(length, capacity, buf),
+            vec: Vec::from_raw_parts(buf, length, capacity),
         }
     }
 
index e608a7d22dcf5e8a8fe33bf3fbf969974157ae93..c57a465df3780e085b7fd07cc01be1fad37eafb2 100644 (file)
 #[unsafe_no_drop_flag]
 #[stable]
 pub struct Vec<T> {
+    ptr: *mut T,
     len: uint,
     cap: uint,
-    ptr: *mut T
 }
 
 impl<T> Vec<T> {
@@ -125,7 +125,7 @@ pub fn new() -> Vec<T> {
         // non-null value which is fine since we never call deallocate on the ptr
         // if cap is 0. The reason for this is because the pointer of a slice
         // being NULL would break the null pointer optimization for enums.
-        Vec { len: 0, cap: 0, ptr: EMPTY as *mut T }
+        Vec { ptr: EMPTY as *mut T, len: 0, cap: 0 }
     }
 
     /// Constructs a new, empty `Vec` with the specified capacity.
@@ -159,14 +159,14 @@ pub fn new() -> Vec<T> {
     #[stable]
     pub fn with_capacity(capacity: uint) -> Vec<T> {
         if mem::size_of::<T>() == 0 {
-            Vec { len: 0, cap: uint::MAX, ptr: EMPTY as *mut T }
+            Vec { ptr: EMPTY as *mut T, len: 0, cap: uint::MAX }
         } else if capacity == 0 {
             Vec::new()
         } else {
             let size = capacity.checked_mul(&mem::size_of::<T>())
                                .expect("capacity overflow");
             let ptr = unsafe { allocate(size, mem::min_align_of::<T>()) };
-            Vec { len: 0, cap: capacity, ptr: ptr as *mut T }
+            Vec { ptr: ptr as *mut T, len: 0, cap: capacity }
         }
     }
 
@@ -237,9 +237,9 @@ pub fn from_fn(length: uint, op: |uint| -> T) -> Vec<T> {
     /// }
     /// ```
     #[experimental]
-    pub unsafe fn from_raw_parts(length: uint, capacity: uint,
-                                 ptr: *mut T) -> Vec<T> {
-        Vec { len: length, cap: capacity, ptr: ptr }
+    pub unsafe fn from_raw_parts(ptr: *mut T, length: uint,
+                                 capacity: uint) -> Vec<T> {
+        Vec { ptr: ptr, len: length, cap: capacity }
     }
 
     /// Consumes the `Vec`, partitioning it based on a predicate.
@@ -1680,7 +1680,7 @@ fn drop(&mut self) {
 pub fn as_vec<'a, T>(x: &'a [T]) -> DerefVec<'a, T> {
     unsafe {
         DerefVec {
-            x: Vec::from_raw_parts(x.len(), x.len(), x.as_ptr() as *mut T),
+            x: Vec::from_raw_parts(x.as_ptr() as *mut T, x.len(), x.len()),
             l: ContravariantLifetime::<'a>
         }
     }
@@ -1929,7 +1929,7 @@ pub fn map_in_place<U>(self, f: |T| -> U) -> Vec<U> {
                 let vec_cap = pv.vec.capacity();
                 let vec_ptr = pv.vec.as_mut_ptr() as *mut U;
                 mem::forget(pv);
-                Vec::from_raw_parts(vec_len, vec_cap, vec_ptr)
+                Vec::from_raw_parts(vec_ptr, vec_len, vec_cap)
             }
         } else {
             // Put the `Vec` into the `PartialVecZeroSized` structure and
index e5c37e5041abf43a984c2716fdc56d8c5bcdfe25..4f09b34557c74e93ab2938474406992381a77e14 100644 (file)
@@ -74,7 +74,7 @@ pub fn from_vec(mut v: Vec<T>) -> OwnedSlice<T> {
     pub fn into_vec(self) -> Vec<T> {
         // null is ok, because len == 0 in that case, as required by Vec.
         unsafe {
-            let ret = Vec::from_raw_parts(self.len, self.len, self.data);
+            let ret = Vec::from_raw_parts(self.data, self.len, self.len);
             // the vector owns the allocation now
             mem::forget(self);
             ret