use super::QueryResult;
use connection_like::ConnectionLike;
use errors::*;
use lib_futures::Async::Ready;
use lib_futures::{Future, Poll};
use queryable::Protocol;
use BoxFuture;
use Row;
pub struct ForEach<T, P, F> {
fut: BoxFuture<(QueryResult<T, P>, Option<Row>)>,
fun: F,
}
impl<T, P, F> ForEach<T, P, F>
where
F: FnMut(Row),
P: Send + Protocol + 'static,
T: ConnectionLike + Sized + 'static,
{
pub fn new(query_result: QueryResult<T, P>, fun: F) -> ForEach<T, P, F> {
ForEach {
fut: Box::new(query_result.get_row()),
fun,
}
}
}
impl<T, P, F> Future for ForEach<T, P, F>
where
F: FnMut(Row),
P: Protocol,
P: Send + 'static,
T: ConnectionLike + Sized + 'static,
{
type Item = (QueryResult<T, P>);
type Error = Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
loop {
let (query_result, row_opt) = try_ready!(self.fut.poll());
match row_opt {
Some(row) => {
(self.fun)(row);
}
None => {
return Ok(Ready(query_result));
}
}
self.fut = Box::new(query_result.get_row());
}
}
}