]> git.lizzy.rs Git - rust.git/commitdiff
vec: avoid `ptrtoint`/`inttoptr` in the iterators
authorDaniel Micay <danielmicay@gmail.com>
Tue, 6 Aug 2013 21:15:43 +0000 (17:15 -0400)
committerDaniel Micay <danielmicay@gmail.com>
Wed, 7 Aug 2013 03:41:20 +0000 (23:41 -0400)
This results in throwing away alias analysis information, because LLVM
does *not* implement reasoning about these conversions yet.

We specialize zero-size types since a `getelementptr` offset will
return us the same pointer, making it broken as a simple counter.

src/libstd/vec.rs

index 0259b547ab3f06040f812deaa55144fbb5bded3e..8dbfb3ec543e26488468302169cb192040c9cbac 100644 (file)
@@ -849,10 +849,15 @@ fn slice_to(&self, end: uint) -> &'self [T] {
     fn iter(self) -> VecIterator<'self, T> {
         unsafe {
             let p = vec::raw::to_ptr(self);
-            VecIterator{ptr: p,
-                        end: (p as uint + self.len() *
-                              sys::nonzero_size_of::<T>()) as *T,
-                        lifetime: cast::transmute(p)}
+            if sys::size_of::<T>() == 0 {
+                VecIterator{ptr: p,
+                            end: (p as uint + self.len()) as *T,
+                            lifetime: cast::transmute(p)}
+            } else {
+                VecIterator{ptr: p,
+                            end: p.offset(self.len() as int),
+                            lifetime: cast::transmute(p)}
+            }
         }
     }
 
@@ -1826,10 +1831,15 @@ fn mut_split(self, mid: uint) -> (&'self mut [T], &'self mut [T]) {
     fn mut_iter(self) -> VecMutIterator<'self, T> {
         unsafe {
             let p = vec::raw::to_mut_ptr(self);
-            VecMutIterator{ptr: p,
-                           end: (p as uint + self.len() *
-                                 sys::nonzero_size_of::<T>()) as *mut T,
-                           lifetime: cast::transmute(p)}
+            if sys::size_of::<T>() == 0 {
+                VecMutIterator{ptr: p,
+                               end: (p as uint + self.len()) as *mut T,
+                               lifetime: cast::transmute(p)}
+            } else {
+                VecMutIterator{ptr: p,
+                               end: p.offset(self.len() as int),
+                               lifetime: cast::transmute(p)}
+            }
         }
     }