2019-12-15 23:58:50 +00:00
|
|
|
use crate::Rt;
|
2018-12-29 05:01:10 +00:00
|
|
|
use bytes::{Buf, Bytes};
|
2019-12-04 02:25:29 +00:00
|
|
|
use futures::StreamExt;
|
2018-12-29 05:01:10 +00:00
|
|
|
use std::io::{self, BufRead, Cursor, Read};
|
2019-08-04 01:09:27 +00:00
|
|
|
use std::pin::Pin;
|
2019-11-30 21:17:23 +00:00
|
|
|
use tokio_postgres::{CopyOutStream, Error};
|
2018-12-29 05:01:10 +00:00
|
|
|
|
2019-03-31 03:58:01 +00:00
|
|
|
/// The reader returned by the `copy_out` method.
|
2019-11-30 16:04:59 +00:00
|
|
|
pub struct CopyOutReader<'a> {
|
2019-12-15 23:58:50 +00:00
|
|
|
runtime: Rt<'a>,
|
2019-12-04 02:25:29 +00:00
|
|
|
stream: Pin<Box<CopyOutStream>>,
|
2018-12-29 05:01:10 +00:00
|
|
|
cur: Cursor<Bytes>,
|
|
|
|
}
|
|
|
|
|
2019-11-30 16:04:59 +00:00
|
|
|
impl<'a> CopyOutReader<'a> {
|
2019-12-04 02:25:29 +00:00
|
|
|
pub(crate) fn new(
|
2019-12-15 23:58:50 +00:00
|
|
|
mut runtime: Rt<'a>,
|
2019-12-04 02:25:29 +00:00
|
|
|
stream: CopyOutStream,
|
|
|
|
) -> Result<CopyOutReader<'a>, Error> {
|
|
|
|
let mut stream = Box::pin(stream);
|
|
|
|
let cur = match runtime.block_on(stream.next()) {
|
2018-12-29 05:01:10 +00:00
|
|
|
Some(Ok(cur)) => cur,
|
|
|
|
Some(Err(e)) => return Err(e),
|
|
|
|
None => Bytes::new(),
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(CopyOutReader {
|
2019-12-04 02:25:29 +00:00
|
|
|
runtime,
|
|
|
|
stream,
|
2018-12-29 05:01:10 +00:00
|
|
|
cur: Cursor::new(cur),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-30 16:04:59 +00:00
|
|
|
impl Read for CopyOutReader<'_> {
|
2018-12-29 05:01:10 +00:00
|
|
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
|
|
|
let b = self.fill_buf()?;
|
|
|
|
let len = usize::min(buf.len(), b.len());
|
|
|
|
buf[..len].copy_from_slice(&b[..len]);
|
|
|
|
self.consume(len);
|
|
|
|
Ok(len)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-30 16:04:59 +00:00
|
|
|
impl BufRead for CopyOutReader<'_> {
|
2018-12-29 05:01:10 +00:00
|
|
|
fn fill_buf(&mut self) -> io::Result<&[u8]> {
|
|
|
|
if self.cur.remaining() == 0 {
|
2019-12-04 02:25:29 +00:00
|
|
|
match self.runtime.block_on(self.stream.next()) {
|
2018-12-29 05:01:10 +00:00
|
|
|
Some(Ok(cur)) => self.cur = Cursor::new(cur),
|
|
|
|
Some(Err(e)) => return Err(io::Error::new(io::ErrorKind::Other, e)),
|
|
|
|
None => {}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Buf::bytes(&self.cur))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn consume(&mut self, amt: usize) {
|
|
|
|
self.cur.advance(amt);
|
|
|
|
}
|
|
|
|
}
|