Add Rows::map method

This commit is contained in:
gwenn
2019-03-10 12:58:20 +01:00
parent 59a44cfae5
commit 71a2004894
7 changed files with 49 additions and 25 deletions

View File

@@ -1,6 +1,8 @@
use fallible_iterator::FallibleIterator;
use fallible_streaming_iterator::FallibleStreamingIterator;
use std::{convert, result};
use super::{Error, FallibleStreamingIterator, Result, Statement};
use super::{Error, Result, Statement};
use crate::types::{FromSql, FromSqlError, ValueRef};
/// An handle for the resulting rows of a query.
@@ -32,6 +34,13 @@ impl<'stmt> Rows<'stmt> {
self.advance()?;
Ok((*self).get())
}
pub fn map<F, B>(self, f: F) -> Map<'stmt, F>
where
F: FnMut(&Row<'_>) -> Result<B>,
{
Map { rows: self, f: f }
}
}
impl<'stmt> Rows<'stmt> {
@@ -56,6 +65,26 @@ impl Drop for Rows<'_> {
}
}
pub struct Map<'stmt, F> {
rows: Rows<'stmt>,
f: F,
}
impl<F, B> FallibleIterator for Map<'_, F>
where
F: FnMut(&Row<'_>) -> Result<B>,
{
type Error = Error;
type Item = B;
fn next(&mut self) -> Result<Option<B>> {
match self.rows.next()? {
Some(v) => Ok(Some((self.f)(v)?)),
None => Ok(None),
}
}
}
/// An iterator over the mapped resulting rows of a query.
pub struct MappedRows<'stmt, F> {
rows: Rows<'stmt>,