提交 2a0dac6f 编写于 作者: K Kevin Ballard

Handle fallout for vector addition

Adding two vectors now results in a Vec<T> instead of a ~[T].

Implement Add on Vec<T>.
上级 cc42b619
......@@ -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()
}
}
......@@ -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
......
......@@ -18,13 +18,16 @@
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
......
......@@ -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) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册