Skip to content

Commit bcb1047

Browse files
committed
Make LazyRows a fallible iterator
1 parent 62cac49 commit bcb1047

File tree

2 files changed

+28
-24
lines changed

2 files changed

+28
-24
lines changed

src/rows.rs

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
//! Query result rows.
22
3+
use fallible_iterator::FallibleIterator;
4+
use postgres_protocol::message::frontend;
35
use std::ascii::AsciiExt;
46
use std::borrow::Cow;
57
use std::collections::VecDeque;
68
use std::fmt;
79
use std::ops::Deref;
810
use std::slice;
9-
use postgres_protocol::message::frontend;
1011

1112
use {Result, SessionInfoNew, RowsNew, LazyRowsNew, StatementInternals, WrongTypeNew};
1213
use transaction::Transaction;
@@ -55,9 +56,9 @@ impl<'a> RowsNew<'a> for Rows<'a> {
5556
impl<'a> fmt::Debug for Rows<'a> {
5657
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
5758
fmt.debug_struct("Rows")
58-
.field("columns", &self.columns())
59-
.field("rows", &self.data.len())
60-
.finish()
59+
.field("columns", &self.columns())
60+
.field("rows", &self.data.len())
61+
.finish()
6162
}
6263
}
6364

@@ -152,8 +153,8 @@ pub struct Row<'a> {
152153
impl<'a> fmt::Debug for Row<'a> {
153154
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
154155
fmt.debug_struct("Row")
155-
.field("statement", self.stmt)
156-
.finish()
156+
.field("statement", self.stmt)
157+
.finish()
157158
}
158159
}
159160

@@ -332,11 +333,11 @@ impl<'a, 'b> Drop for LazyRows<'a, 'b> {
332333
impl<'a, 'b> fmt::Debug for LazyRows<'a, 'b> {
333334
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
334335
fmt.debug_struct("LazyRows")
335-
.field("name", &self.name)
336-
.field("row_limit", &self.row_limit)
337-
.field("remaining_rows", &self.data.len())
338-
.field("more_rows", &self.more_rows)
339-
.finish()
336+
.field("name", &self.name)
337+
.field("row_limit", &self.row_limit)
338+
.field("remaining_rows", &self.data.len())
339+
.field("more_rows", &self.more_rows)
340+
.finish()
340341
}
341342
}
342343

@@ -373,22 +374,25 @@ impl<'trans, 'stmt> LazyRows<'trans, 'stmt> {
373374
}
374375
}
375376

376-
impl<'trans, 'stmt> Iterator for LazyRows<'trans, 'stmt> {
377-
type Item = Result<Row<'stmt>>;
377+
impl<'trans, 'stmt> FallibleIterator for LazyRows<'trans, 'stmt> {
378+
type Item = Row<'stmt>;
379+
type Error = Error;
378380

379-
fn next(&mut self) -> Option<Result<Row<'stmt>>> {
381+
fn next(&mut self) -> Result<Option<Row<'stmt>>> {
380382
if self.data.is_empty() && self.more_rows {
381-
if let Err(err) = self.execute() {
382-
return Some(Err(err));
383-
}
383+
try!(self.execute());
384384
}
385385

386-
self.data.pop_front().map(|r| {
387-
Ok(Row {
388-
stmt: self.stmt,
389-
data: Cow::Owned(r),
390-
})
391-
})
386+
let row = self.data
387+
.pop_front()
388+
.map(|r| {
389+
Row {
390+
stmt: self.stmt,
391+
data: Cow::Owned(r),
392+
}
393+
});
394+
395+
Ok(row)
392396
}
393397

394398
fn size_hint(&self) -> (usize, Option<usize>) {

tests/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ fn test_lazy_query() {
406406
}
407407
let stmt = or_panic!(trans.prepare("SELECT id FROM foo ORDER BY id"));
408408
let result = or_panic!(stmt.lazy_query(&trans, &[], 2));
409-
assert_eq!(values, result.map(|row| row.unwrap().get(0)).collect::<Vec<_>>());
409+
assert_eq!(values, result.map(|row| row.get(0)).collect::<Vec<_>>().unwrap());
410410
}
411411

412412
#[test]

0 commit comments

Comments
 (0)