Skip to content

Commit 65d53e7

Browse files
committed
---
yaml --- r: 28150 b: refs/heads/try c: 3e4b558 h: refs/heads/master v: v3
1 parent 7d0fdd1 commit 65d53e7

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
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: cd6f24f9d14ac90d167386a56e7a6ac1f0318195
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: cd6f24f9d14ac90d167386a56e7a6ac1f0318195
5-
refs/heads/try: 5eef15df126e8dfedc9e72ee3d9456ead0c9d06d
5+
refs/heads/try: 3e4b55807d9778c396ae449fe9a5680508d7d62d
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: d0c6ce338884ee21843f4b40bf6bf18d222ce5df

branches/try/src/libcore/vec.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export push, push_all, push_all_move;
4242
export grow;
4343
export grow_fn;
4444
export grow_set;
45+
export truncate;
4546
export map;
4647
export mapi;
4748
export map2;
@@ -611,6 +612,20 @@ fn push_all_move<T>(&v: ~[const T], -rhs: ~[const T]) {
611612
}
612613
}
613614

615+
/// Shorten a vector, dropping excess elements.
616+
fn truncate<T>(&v: ~[const T], newlen: uint) {
617+
do as_buf(v) |p, oldlen| {
618+
assert(newlen <= oldlen);
619+
unsafe {
620+
// This loop is optimized out for non-drop types.
621+
for uint::range(newlen, oldlen) |i| {
622+
let _dropped <- *ptr::offset(p, i);
623+
}
624+
unsafe::set_len(v, newlen);
625+
}
626+
}
627+
}
628+
614629
// Appending
615630
#[inline(always)]
616631
pure fn append<T: copy>(+lhs: ~[T], rhs: &[const T]) -> ~[T] {
@@ -2166,6 +2181,15 @@ mod tests {
21662181
assert (v[4] == 5);
21672182
}
21682183

2184+
#[test]
2185+
fn test_truncate() {
2186+
let mut v = ~[@6,@5,@4];
2187+
truncate(v, 1);
2188+
assert(v.len() == 1);
2189+
assert(*(v[0]) == 6);
2190+
// If the unsafe block didn't drop things properly, we blow up here.
2191+
}
2192+
21692193
#[test]
21702194
fn test_map() {
21712195
// Test on-stack map.

0 commit comments

Comments
 (0)