Skip to content

Commit fb77953

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 ea04b2f commit fb77953

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

src/libstd/slice.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -579,19 +579,19 @@ pub mod traits {
579579
fn gt(&self, other: &~[T]) -> bool { self.as_slice() > other.as_slice() }
580580
}
581581

582-
impl<'a,T:Clone, V: Vector<T>> Add<V, ~[T]> for &'a [T] {
582+
impl<'a,T:Clone, V: Vector<T>> Add<V, Vec<T>> for &'a [T] {
583583
#[inline]
584-
fn add(&self, rhs: &V) -> ~[T] {
584+
fn add(&self, rhs: &V) -> Vec<T> {
585585
let mut res = Vec::with_capacity(self.len() + rhs.as_slice().len());
586586
res.push_all(*self);
587587
res.push_all(rhs.as_slice());
588-
res.move_iter().collect()
588+
res
589589
}
590590
}
591591

592-
impl<T:Clone, V: Vector<T>> Add<V, ~[T]> for ~[T] {
592+
impl<T:Clone, V: Vector<T>> Add<V, Vec<T>> for ~[T] {
593593
#[inline]
594-
fn add(&self, rhs: &V) -> ~[T] {
594+
fn add(&self, rhs: &V) -> Vec<T> {
595595
self.as_slice() + rhs.as_slice()
596596
}
597597
}

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};
2727
use ptr::RawPtr;
2828
use ptr;
@@ -1369,6 +1369,16 @@ impl<T> Vector<T> for Vec<T> {
13691369
}
13701370
}
13711371

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

0 commit comments

Comments
 (0)