use crate::client::{InnerClient, Responses}; use crate::codec::FrontendMessage; use crate::connection::RequestMessages; use crate::types::ToSql; use crate::{query, Error, Statement}; use bytes::Bytes; use futures::{ready, Stream}; use postgres_protocol::message::backend::Message; use std::pin::Pin; use std::task::{Context, Poll}; pub async fn copy_out<'a, I>( client: &InnerClient, statement: Statement, params: I, ) -> Result where I: IntoIterator, I::IntoIter: ExactSizeIterator, { let buf = query::encode(&statement, params)?; let responses = start(client, buf).await?; Ok(CopyStream { responses }) } async fn start(client: &InnerClient, buf: Vec) -> Result { let mut responses = client.send(RequestMessages::Single(FrontendMessage::Raw(buf)))?; match responses.next().await? { Message::BindComplete => {} _ => return Err(Error::unexpected_message()), } match responses.next().await? { Message::CopyOutResponse(_) => {} _ => return Err(Error::unexpected_message()), } Ok(responses) } pub struct CopyStream { responses: Responses, } impl Stream for CopyStream { type Item = Result; fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { match ready!(self.responses.poll_next(cx)?) { Message::CopyData(body) => Poll::Ready(Some(Ok(body.into_bytes()))), Message::CopyDone => Poll::Ready(None), _ => Poll::Ready(Some(Err(Error::unexpected_message()))), } } }