Skip to content

Commit ed62f6d

Browse files
committed
core: add consume_reverse
1 parent 90b3658 commit ed62f6d

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/libcore/vec.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,28 @@ pub fn consume<T>(mut v: ~[T], f: &fn(uint, v: T)) {
558558
}
559559
}
560560
561+
pub fn consume_reverse<T>(mut v: ~[T], f: &fn(uint, v: T)) {
562+
unsafe {
563+
do as_mut_buf(v) |p, ln| {
564+
let mut i = ln;
565+
while i > 0 {
566+
i -= 1;
567+
568+
// NB: This unsafe operation counts on init writing 0s to the
569+
// holes we create in the vector. That ensures that, if the
570+
// iterator fails then we won't try to clean up the consumed
571+
// elements during unwinding
572+
let mut x = intrinsics::init();
573+
let p = ptr::mut_offset(p, i);
574+
x <-> *p;
575+
f(i, x);
576+
}
577+
}
578+
579+
raw::set_len(&mut v, 0);
580+
}
581+
}
582+
561583
/// Remove the last element from a vector and return it
562584
pub fn pop<T>(v: &mut ~[T]) -> T {
563585
let ln = v.len();
@@ -1983,6 +2005,7 @@ pub trait OwnedVector<T> {
19832005
fn truncate(&mut self, newlen: uint);
19842006
fn retain(&mut self, f: &fn(t: &T) -> bool);
19852007
fn consume(self, f: &fn(uint, v: T));
2008+
fn consume_reverse(self, f: &fn(uint, v: T));
19862009
fn filter(self, f: &fn(t: &T) -> bool) -> ~[T];
19872010
fn partition(self, f: &fn(&T) -> bool) -> (~[T], ~[T]);
19882011
fn grow_fn(&mut self, n: uint, op: iter::InitOp<T>);
@@ -2044,6 +2067,11 @@ impl<T> OwnedVector<T> for ~[T] {
20442067
consume(self, f)
20452068
}
20462069

2070+
#[inline]
2071+
fn consume_reverse(self, f: &fn(uint, v: T)) {
2072+
consume_reverse(self, f)
2073+
}
2074+
20472075
#[inline]
20482076
fn filter(self, f: &fn(&T) -> bool) -> ~[T] {
20492077
filter(self, f)

0 commit comments

Comments
 (0)