Skip to content

Commit 2b1cac4

Browse files
committed
Clean up CopyInFuture
1 parent 1b29330 commit 2b1cac4

1 file changed

Lines changed: 31 additions & 25 deletions

File tree

postgres/src/client.rs

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use futures::sync::mpsc;
2-
use futures::{try_ready, Async, AsyncSink, Future, Poll, Sink, Stream};
2+
use futures::{Async, AsyncSink, Future, Poll, Sink, Stream};
33
use std::io::{self, Read};
44
use tokio_postgres::types::{ToSql, Type};
55
use tokio_postgres::{Error, Row};
@@ -73,6 +73,7 @@ impl Client {
7373
sender,
7474
reader,
7575
pending: None,
76+
done: false,
7677
}
7778
.wait()
7879
}
@@ -125,23 +126,7 @@ struct CopyInFuture<R> {
125126
sender: mpsc::Sender<CopyData>,
126127
reader: R,
127128
pending: Option<CopyData>,
128-
}
129-
130-
impl<R> CopyInFuture<R> {
131-
fn poll_send_data(&mut self, data: CopyData) -> Poll<(), Error> {
132-
match self.sender.start_send(data) {
133-
Ok(AsyncSink::Ready) => Ok(Async::Ready(())),
134-
Ok(AsyncSink::NotReady(pending)) => {
135-
self.pending = Some(pending);
136-
Ok(Async::NotReady)
137-
}
138-
// the future's hung up on its end of the channel, so we'll wait for it to report an error
139-
Err(_) => {
140-
self.pending = Some(CopyData::Done);
141-
Ok(Async::NotReady)
142-
}
143-
}
144-
}
129+
done: bool,
145130
}
146131

147132
impl<R> Future for CopyInFuture<R>
@@ -152,24 +137,45 @@ where
152137
type Error = Error;
153138

154139
fn poll(&mut self) -> Poll<u64, Error> {
155-
if let Async::Ready(n) = self.future.poll()? {
156-
return Ok(Async::Ready(n));
157-
}
158-
159140
loop {
141+
if let Async::Ready(n) = self.future.poll()? {
142+
return Ok(Async::Ready(n));
143+
}
144+
160145
let data = match self.pending.take() {
161146
Some(pending) => pending,
162147
None => {
148+
if self.done {
149+
continue;
150+
}
151+
163152
let mut buf = vec![];
164153
match self.reader.by_ref().take(4096).read_to_end(&mut buf) {
165-
Ok(0) => CopyData::Done,
154+
Ok(0) => {
155+
self.done = true;
156+
CopyData::Done
157+
}
166158
Ok(_) => CopyData::Data(buf),
167-
Err(e) => CopyData::Error(e),
159+
Err(e) => {
160+
self.done = true;
161+
CopyData::Error(e)
162+
}
168163
}
169164
}
170165
};
171166

172-
try_ready!(self.poll_send_data(data));
167+
match self.sender.start_send(data) {
168+
Ok(AsyncSink::Ready) => {}
169+
Ok(AsyncSink::NotReady(pending)) => {
170+
self.pending = Some(pending);
171+
return Ok(Async::NotReady);
172+
}
173+
// the future's hung up on its end of the channel, so we'll wait for it to error
174+
Err(_) => {
175+
self.done = true;
176+
return Ok(Async::NotReady);
177+
}
178+
}
173179
}
174180
}
175181
}

0 commit comments

Comments
 (0)