Skip to content

Commit 8777544

Browse files
committed
---
yaml --- r: 30172 b: refs/heads/incoming c: 3e4b558 h: refs/heads/master v: v3
1 parent 897efa1 commit 8777544

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
@@ -6,7 +6,7 @@ refs/heads/try: d324a424d8f84b1eb049b12cf34182bda91b0024
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: d0c6ce338884ee21843f4b40bf6bf18d222ce5df
9-
refs/heads/incoming: 5eef15df126e8dfedc9e72ee3d9456ead0c9d06d
9+
refs/heads/incoming: 3e4b55807d9778c396ae449fe9a5680508d7d62d
1010
refs/heads/dist-snap: 2f32a1581f522e524009138b33b1c7049ced668d
1111
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1212
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/incoming/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)