From 2a0dac6f58fb07d5fb6a4dfa94e10ddaa44315a7 Mon Sep 17 00:00:00 2001 From: Kevin Ballard Date: Sat, 3 May 2014 23:09:45 -0700 Subject: [PATCH] Handle fallout for vector addition Adding two vectors now results in a Vec instead of a ~[T]. Implement Add on Vec. --- src/libcore/should_not_exist.rs | 17 ----------------- src/libstd/slice.rs | 20 ++++++++++++++++++++ src/libstd/unstable/dynamic_lib.rs | 9 +++++++-- src/libstd/vec.rs | 12 +++++++++++- 4 files changed, 38 insertions(+), 20 deletions(-) diff --git a/src/libcore/should_not_exist.rs b/src/libcore/should_not_exist.rs index e7a1286bafc..78d5afec7b4 100644 --- a/src/libcore/should_not_exist.rs +++ b/src/libcore/should_not_exist.rs @@ -150,20 +150,3 @@ impl Clone for ~[A] { self.iter().map(|a| a.clone()).collect() } } - -#[cfg(not(test))] -impl<'a,T:Clone, V: Vector> Add 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> Add for ~[T] { - #[inline] - fn add(&self, rhs: &V) -> ~[T] { - self.as_slice() + rhs.as_slice() - } -} diff --git a/src/libstd/slice.rs b/src/libstd/slice.rs index 42ffce56e87..d260ca46513 100644 --- a/src/libstd/slice.rs +++ b/src/libstd/slice.rs @@ -279,6 +279,26 @@ impl Iterator<~[T]> for Permutations { } } +#[cfg(not(test))] +impl<'a,T:Clone, V: Vector> Add> for &'a [T] { + #[inline] + fn add(&self, rhs: &V) -> Vec { + 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> Add> for ~[T] { + #[inline] + fn add(&self, rhs: &V) -> Vec { + self.as_slice() + rhs.as_slice() + } +} + /// Extension methods for vector slices with cloneable elements pub trait CloneableVector { /// Copy `self` into a new owned vector diff --git a/src/libstd/unstable/dynamic_lib.rs b/src/libstd/unstable/dynamic_lib.rs index 68f0aaab05b..e2a9f6a5c48 100644 --- a/src/libstd/unstable/dynamic_lib.rs +++ b/src/libstd/unstable/dynamic_lib.rs @@ -18,13 +18,16 @@ A simple wrapper over the platform's dynamic library facilities 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 @@ impl DynamicLibrary { ("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::>(); + 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 diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index d220ebd0d1e..fe122c7873a 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -22,7 +22,7 @@ use mem::{size_of, move_val_init}; 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 @@ impl Vector for Vec { } } +impl> Add> for Vec { + #[inline] + fn add(&self, rhs: &V) -> Vec { + 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 Drop for Vec { fn drop(&mut self) {