Skip to content

Commit ff3ea1c

Browse files
committed
Expose RowIter
1 parent 1390cc5 commit ff3ea1c

File tree

5 files changed

+44
-66
lines changed

5 files changed

+44
-66
lines changed

postgres/src/client.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
use crate::iter::Iter;
21
#[cfg(feature = "runtime")]
32
use crate::Config;
4-
use crate::{CopyInWriter, CopyOutReader, Statement, ToStatement, Transaction};
5-
use fallible_iterator::FallibleIterator;
3+
use crate::{CopyInWriter, CopyOutReader, RowIter, Statement, ToStatement, Transaction};
64
use futures::executor;
75
use tokio_postgres::tls::{MakeTlsConnect, TlsConnect};
86
use tokio_postgres::types::{ToSql, Type};
@@ -183,18 +181,14 @@ impl Client {
183181
/// # Ok(())
184182
/// # }
185183
/// ```
186-
pub fn query_raw<'a, T, I>(
187-
&mut self,
188-
query: &T,
189-
params: I,
190-
) -> Result<impl FallibleIterator<Item = Row, Error = Error>, Error>
184+
pub fn query_raw<'a, T, I>(&mut self, query: &T, params: I) -> Result<RowIter<'_>, Error>
191185
where
192186
T: ?Sized + ToStatement,
193187
I: IntoIterator<Item = &'a dyn ToSql>,
194188
I::IntoIter: ExactSizeIterator,
195189
{
196190
let stream = executor::block_on(self.0.query_raw(query, params))?;
197-
Ok(Iter::new(stream))
191+
Ok(RowIter::new(stream))
198192
}
199193

200194
/// Creates a new prepared statement.

postgres/src/iter.rs

Lines changed: 0 additions & 45 deletions
This file was deleted.

postgres/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ pub use crate::copy_out_reader::CopyOutReader;
7575
pub use crate::error::Error;
7676
#[doc(no_inline)]
7777
pub use crate::row::{Row, SimpleQueryRow};
78+
pub use crate::row_iter::RowIter;
7879
#[doc(no_inline)]
7980
pub use crate::tls::NoTls;
8081
pub use crate::transaction::*;
@@ -84,7 +85,7 @@ mod client;
8485
pub mod config;
8586
mod copy_in_writer;
8687
mod copy_out_reader;
87-
mod iter;
88+
mod row_iter;
8889
mod transaction;
8990

9091
#[cfg(feature = "runtime")]

postgres/src/row_iter.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use fallible_iterator::FallibleIterator;
2+
use futures::executor::{self, BlockingStream};
3+
use std::marker::PhantomData;
4+
use std::pin::Pin;
5+
use tokio_postgres::{Error, Row, RowStream};
6+
7+
/// The iterator returned by `query_raw`.
8+
pub struct RowIter<'a> {
9+
it: BlockingStream<Pin<Box<RowStream>>>,
10+
_p: PhantomData<&'a mut ()>,
11+
}
12+
13+
// no-op impl to extend the borrow until drop
14+
impl Drop for RowIter<'_> {
15+
fn drop(&mut self) {}
16+
}
17+
18+
impl<'a> RowIter<'a> {
19+
pub(crate) fn new(stream: RowStream) -> RowIter<'a> {
20+
RowIter {
21+
it: executor::block_on_stream(Box::pin(stream)),
22+
_p: PhantomData,
23+
}
24+
}
25+
}
26+
27+
impl FallibleIterator for RowIter<'_> {
28+
type Item = Row;
29+
type Error = Error;
30+
31+
fn next(&mut self) -> Result<Option<Row>, Error> {
32+
self.it.next().transpose()
33+
}
34+
}

postgres/src/transaction.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
use crate::iter::Iter;
2-
use crate::{CopyInWriter, CopyOutReader, Portal, Statement, ToStatement};
3-
use fallible_iterator::FallibleIterator;
1+
use crate::{CopyInWriter, CopyOutReader, Portal, RowIter, Statement, ToStatement};
42
use futures::executor;
53
use tokio_postgres::types::{ToSql, Type};
64
use tokio_postgres::{Error, Row, SimpleQueryMessage};
@@ -63,18 +61,14 @@ impl<'a> Transaction<'a> {
6361
}
6462

6563
/// Like `Client::query_raw`.
66-
pub fn query_raw<'b, T, I>(
67-
&mut self,
68-
query: &T,
69-
params: I,
70-
) -> Result<impl FallibleIterator<Item = Row, Error = Error>, Error>
64+
pub fn query_raw<'b, T, I>(&mut self, query: &T, params: I) -> Result<RowIter<'_>, Error>
7165
where
7266
T: ?Sized + ToStatement,
7367
I: IntoIterator<Item = &'b dyn ToSql>,
7468
I::IntoIter: ExactSizeIterator,
7569
{
7670
let stream = executor::block_on(self.0.query_raw(query, params))?;
77-
Ok(Iter::new(stream))
71+
Ok(RowIter::new(stream))
7872
}
7973

8074
/// Binds parameters to a statement, creating a "portal".
@@ -107,9 +101,9 @@ impl<'a> Transaction<'a> {
107101
&mut self,
108102
portal: &Portal,
109103
max_rows: i32,
110-
) -> Result<impl FallibleIterator<Item = Row, Error = Error>, Error> {
104+
) -> Result<RowIter<'_>, Error> {
111105
let stream = executor::block_on(self.0.query_portal_raw(portal, max_rows))?;
112-
Ok(Iter::new(stream))
106+
Ok(RowIter::new(stream))
113107
}
114108

115109
/// Like `Client::copy_in`.

0 commit comments

Comments
 (0)