mirror of
https://github.com/isar/rusqlite.git
synced 2025-09-16 04:32:19 +08:00
Add Rows::map method
This commit is contained in:
31
src/row.rs
31
src/row.rs
@@ -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>,
|
||||
|
Reference in New Issue
Block a user