Skip to content

Commit 2a0dac6

Browse files
committed
Handle fallout for vector addition
Adding two vectors now results in a Vec<T> instead of a ~[T]. Implement Add on Vec<T>.
1 parent cc42b61 commit 2a0dac6

File tree

4 files changed

+38
-20
lines changed

4 files changed

+38
-20
lines changed

src/libcore/should_not_exist.rs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -150,20 +150,3 @@ impl<A: Clone> Clone for ~[A] {
150150
self.iter().map(|a| a.clone()).collect()
151151
}
152152
}
153-
154-
#[cfg(not(test))]
155-
impl<'a,T:Clone, V: Vector<T>> Add<V, ~[T]> for &'a [T] {
156-
#[inline]
157-
fn add(&self, rhs: &V) -> ~[T] {
158-
let first = self.iter().map(|t| t.clone());
159-
first.chain(rhs.as_slice().iter().map(|t| t.clone())).collect()
160-
}
161-
}
162-
163-
#[cfg(not(test))]
164-
impl<T:Clone, V: Vector<T>> Add<V, ~[T]> for ~[T] {
165-
#[inline]
166-
fn add(&self, rhs: &V) -> ~[T] {
167-
self.as_slice() + rhs.as_slice()
168-
}
169-
}

src/libstd/slice.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,26 @@ impl<T: Clone> Iterator<~[T]> for Permutations<T> {
279279
}
280280
}
281281

282+
#[cfg(not(test))]
283+
impl<'a,T:Clone, V: Vector<T>> Add<V, Vec<T>> for &'a [T] {
284+
#[inline]
285+
fn add(&self, rhs: &V) -> Vec<T> {
286+
let rhs = rhs.as_slice();
287+
let mut res = Vec::with_capacity(self.len() + rhs.len());
288+
res.push_all(*self);
289+
res.push_all(rhs);
290+
res
291+
}
292+
}
293+
294+
#[cfg(not(test))]
295+
impl<T:Clone, V: Vector<T>> Add<V, Vec<T>> for ~[T] {
296+
#[inline]
297+
fn add(&self, rhs: &V) -> Vec<T> {
298+
self.as_slice() + rhs.as_slice()
299+
}
300+
}
301+
282302
/// Extension methods for vector slices with cloneable elements
283303
pub trait CloneableVector<T> {
284304
/// Copy `self` into a new owned vector

src/libstd/unstable/dynamic_lib.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@ A simple wrapper over the platform's dynamic library facilities
1818

1919
use c_str::ToCStr;
2020
use cast;
21+
use iter::Iterator;
2122
use ops::*;
2223
use option::*;
2324
use os;
2425
use path::GenericPath;
2526
use path;
2627
use result::*;
28+
use slice::{Vector,OwnedVector};
2729
use str;
30+
use vec::Vec;
2831

2932
pub struct DynamicLibrary { handle: *u8}
3033

@@ -73,8 +76,10 @@ impl DynamicLibrary {
7376
("LD_LIBRARY_PATH", ':' as u8)
7477
};
7578
let newenv = os::getenv_as_bytes(envvar).unwrap_or(box []);
76-
let newenv = newenv + &[sep] + path.as_vec();
77-
os::setenv(envvar, str::from_utf8(newenv).unwrap());
79+
let mut newenv = newenv.move_iter().collect::<Vec<_>>();
80+
newenv.push_all(&[sep]);
81+
newenv.push_all(path.as_vec());
82+
os::setenv(envvar, str::from_utf8(newenv.as_slice()).unwrap());
7883
}
7984

8085
/// Access the value at the symbol of the dynamic library

src/libstd/vec.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use mem::{size_of, move_val_init};
2222
use mem;
2323
use num;
2424
use num::{CheckedMul, CheckedAdd};
25-
use ops::Drop;
25+
use ops::{Add, Drop};
2626
use option::{None, Option, Some, Expect};
2727
use ptr::RawPtr;
2828
use ptr;
@@ -1370,6 +1370,16 @@ impl<T> Vector<T> for Vec<T> {
13701370
}
13711371
}
13721372

1373+
impl<T: Clone, V: Vector<T>> Add<V, Vec<T>> for Vec<T> {
1374+
#[inline]
1375+
fn add(&self, rhs: &V) -> Vec<T> {
1376+
let mut res = Vec::with_capacity(self.len() + rhs.as_slice().len());
1377+
res.push_all(self.as_slice());
1378+
res.push_all(rhs.as_slice());
1379+
res
1380+
}
1381+
}
1382+
13731383
#[unsafe_destructor]
13741384
impl<T> Drop for Vec<T> {
13751385
fn drop(&mut self) {

0 commit comments

Comments
 (0)