use std::vec::Drain;
pub trait DrainWhileable<T> {
fn drain_while<P>(&mut self, pred: P) -> DrainWhile<T> where P: Fn(&T) -> bool;
}
pub struct DrainWhile<'a, T: 'a>(Drain<'a, T>);
impl<'a, T> Iterator for DrainWhile<'a, T> {
type Item = T;
fn next(&mut self) -> Option<T> { self.0.next() }
}
impl<T> DrainWhileable<T> for Vec<T> {
fn drain_while<P>(&mut self, mut pred: P) -> DrainWhile<T> where P: FnMut(&T) -> bool {
let some_match = match self.first() {
Some(x) => pred(x),
_ => false
};
if some_match {
match self.iter().position(|x| !pred(x)) {
None => DrainWhile(self.drain(..)),
Some(i) => DrainWhile(self.drain(..i)),
}
} else {
DrainWhile(self.drain(0..0))
}
}
}