Skip to content

Commit 0e2fbe7

Browse files
huonwalexcrichton
authored andcommitted
---
yaml --- r: 110422 b: refs/heads/try c: f5a4837 h: refs/heads/master v: v3
1 parent f7513b1 commit 0e2fbe7

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
refs/heads/master: e415c25bcd81dc1f9a5a3d25d9b48ed2d545336b
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: c7fac4471201977fdb1c0c0a26c87287e12dc644
5-
refs/heads/try: 0bd6f2ce0b650ebcb1d2b05dc501a9ed4907b6d2
5+
refs/heads/try: f5a4837df0885368352d118e1e71f4853bf55bf8
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: 147ecfdd8221e4a4d4e090486829a06da1e0ca3c

branches/try/src/libstd/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,4 +232,5 @@ mod std {
232232
pub use to_str;
233233
pub use ty;
234234
pub use unstable;
235+
pub use vec;
235236
}

branches/try/src/libstd/vec.rs

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -310,11 +310,24 @@ impl<T: Clone> Vec<T> {
310310

311311
impl<T:Clone> Clone for Vec<T> {
312312
fn clone(&self) -> Vec<T> {
313-
let mut vector = Vec::with_capacity(self.len());
314-
for element in self.iter() {
315-
vector.push((*element).clone())
313+
self.iter().map(|x| x.clone()).collect()
314+
}
315+
316+
fn clone_from(&mut self, other: &Vec<T>) {
317+
// drop anything in self that will not be overwritten
318+
if self.len() > other.len() {
319+
self.truncate(other.len())
316320
}
317-
vector
321+
322+
// reuse the contained values' allocations/resources.
323+
for (place, thing) in self.mut_iter().zip(other.iter()) {
324+
place.clone_from(thing)
325+
}
326+
327+
// self.len <= other.len due to the truncate above, so the
328+
// slice here is always in-bounds.
329+
let len = self.len();
330+
self.extend(other.slice_from(len).iter().map(|x| x.clone()));
318331
}
319332
}
320333

@@ -1475,4 +1488,39 @@ mod tests {
14751488

14761489
assert!(values == Vec::from_slice([2u8, 3, 5, 6, 7]));
14771490
}
1491+
1492+
#[test]
1493+
fn test_clone() {
1494+
let v: Vec<int> = vec!();
1495+
let w = vec!(1, 2, 3);
1496+
1497+
assert_eq!(v, v.clone());
1498+
1499+
let z = w.clone();
1500+
assert_eq!(w, z);
1501+
// they should be disjoint in memory.
1502+
assert!(w.as_ptr() != z.as_ptr())
1503+
}
1504+
1505+
#[test]
1506+
fn test_clone_from() {
1507+
let mut v = vec!();
1508+
let three = vec!(~1, ~2, ~3);
1509+
let two = vec!(~4, ~5);
1510+
// zero, long
1511+
v.clone_from(&three);
1512+
assert_eq!(v, three);
1513+
1514+
// equal
1515+
v.clone_from(&three);
1516+
assert_eq!(v, three);
1517+
1518+
// long, short
1519+
v.clone_from(&two);
1520+
assert_eq!(v, two);
1521+
1522+
// short, long
1523+
v.clone_from(&three);
1524+
assert_eq!(v, three)
1525+
}
14781526
}

0 commit comments

Comments
 (0)