]> git.lizzy.rs Git - rust.git/commitdiff
Handle fallout for vector addition
authorKevin Ballard <kevin@sb.org>
Sun, 4 May 2014 06:09:45 +0000 (23:09 -0700)
committerKevin Ballard <kevin@sb.org>
Thu, 8 May 2014 19:06:22 +0000 (12:06 -0700)
Adding two vectors now results in a Vec<T> instead of a ~[T].

Implement Add on Vec<T>.

src/libcore/should_not_exist.rs
src/libstd/slice.rs
src/libstd/unstable/dynamic_lib.rs
src/libstd/vec.rs

index e7a1286bafc3be1956c5d2fec67f142ad9076bcc..78d5afec7b48f34912dbe4619095a10fc1c1d2fc 100644 (file)
@@ -150,20 +150,3 @@ fn clone(&self) -> ~[A] {
         self.iter().map(|a| a.clone()).collect()
     }
 }
-
-#[cfg(not(test))]
-impl<'a,T:Clone, V: Vector<T>> Add<V, ~[T]> for &'a [T] {
-    #[inline]
-    fn add(&self, rhs: &V) -> ~[T] {
-        let first = self.iter().map(|t| t.clone());
-        first.chain(rhs.as_slice().iter().map(|t| t.clone())).collect()
-    }
-}
-
-#[cfg(not(test))]
-impl<T:Clone, V: Vector<T>> Add<V, ~[T]> for ~[T] {
-    #[inline]
-    fn add(&self, rhs: &V) -> ~[T] {
-        self.as_slice() + rhs.as_slice()
-    }
-}
index 42ffce56e875e7b98d755c18c0b54525be618c30..d260ca4651318190d24625b59ef1fb5a2824cb1a 100644 (file)
@@ -279,6 +279,26 @@ fn size_hint(&self) -> (uint, Option<uint>) {
     }
 }
 
+#[cfg(not(test))]
+impl<'a,T:Clone, V: Vector<T>> Add<V, Vec<T>> for &'a [T] {
+    #[inline]
+    fn add(&self, rhs: &V) -> Vec<T> {
+        let rhs = rhs.as_slice();
+        let mut res = Vec::with_capacity(self.len() + rhs.len());
+        res.push_all(*self);
+        res.push_all(rhs);
+        res
+    }
+}
+
+#[cfg(not(test))]
+impl<T:Clone, V: Vector<T>> Add<V, Vec<T>> for ~[T] {
+    #[inline]
+    fn add(&self, rhs: &V) -> Vec<T> {
+        self.as_slice() + rhs.as_slice()
+    }
+}
+
 /// Extension methods for vector slices with cloneable elements
 pub trait CloneableVector<T> {
     /// Copy `self` into a new owned vector
index 68f0aaab05b1b034075d23b691fb6911632ea318..e2a9f6a5c4821c481ace94816a41577fcdf4c6fa 100644 (file)
 
 use c_str::ToCStr;
 use cast;
+use iter::Iterator;
 use ops::*;
 use option::*;
 use os;
 use path::GenericPath;
 use path;
 use result::*;
+use slice::{Vector,OwnedVector};
 use str;
+use vec::Vec;
 
 pub struct DynamicLibrary { handle: *u8}
 
@@ -73,8 +76,10 @@ pub fn add_search_path(path: &path::Path) {
             ("LD_LIBRARY_PATH", ':' as u8)
         };
         let newenv = os::getenv_as_bytes(envvar).unwrap_or(box []);
-        let newenv = newenv + &[sep] + path.as_vec();
-        os::setenv(envvar, str::from_utf8(newenv).unwrap());
+        let mut newenv = newenv.move_iter().collect::<Vec<_>>();
+        newenv.push_all(&[sep]);
+        newenv.push_all(path.as_vec());
+        os::setenv(envvar, str::from_utf8(newenv.as_slice()).unwrap());
     }
 
     /// Access the value at the symbol of the dynamic library
index d220ebd0d1e525318d31fe396742157edf7b496c..fe122c7873add6d682c43fb9b3915855a3a55335 100644 (file)
@@ -22,7 +22,7 @@
 use mem;
 use num;
 use num::{CheckedMul, CheckedAdd};
-use ops::Drop;
+use ops::{Add, Drop};
 use option::{None, Option, Some, Expect};
 use ptr::RawPtr;
 use ptr;
@@ -1370,6 +1370,16 @@ fn as_slice<'a>(&'a self) -> &'a [T] {
     }
 }
 
+impl<T: Clone, V: Vector<T>> Add<V, Vec<T>> for Vec<T> {
+    #[inline]
+    fn add(&self, rhs: &V) -> Vec<T> {
+        let mut res = Vec::with_capacity(self.len() + rhs.as_slice().len());
+        res.push_all(self.as_slice());
+        res.push_all(rhs.as_slice());
+        res
+    }
+}
+
 #[unsafe_destructor]
 impl<T> Drop for Vec<T> {
     fn drop(&mut self) {