use std::pin::Pin;
use std::task::{Context, Poll};
use futures_core::Stream;
use tokio_postgres::RowStream;
use crate::error::{BsqlError, BsqlResult};
use crate::pool::PoolConnection;
pub struct QueryStream {
_conn: PoolConnection,
stream: Pin<Box<RowStream>>,
}
impl QueryStream {
pub(crate) fn new(conn: PoolConnection, stream: RowStream) -> Self {
Self {
_conn: conn,
stream: Box::pin(stream),
}
}
}
impl Stream for QueryStream {
type Item = BsqlResult<tokio_postgres::Row>;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
self.stream
.as_mut()
.poll_next(cx)
.map(|opt| opt.map(|r| r.map_err(BsqlError::from)))
}
}