Skip to content

Commit ad62aad

Browse files
committed
Remove ImapStream::closed variable
There is no need to track the state of the inner stream. Besides, it may be wrong as the stream may become open again, e.g. if it is a file and it was rewinded.
1 parent 7c0e244 commit ad62aad

File tree

2 files changed

+10
-24
lines changed

2 files changed

+10
-24
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- Do not generate artificial "broken pipe" errors when attempting to send a request
1313
after reaching EOF on the response stream. #73
14+
- Do not attempt to track if the stream is closed or not.
15+
`ImapStream` can wrap any kinds of streams, including streams which may become open again later,
16+
like files which can be rewinded after reaching end of file or appended to.
1417

1518
## [0.7.0] - 2023-04-03
1619

src/imap_stream.rs

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ pub struct ImapStream<R: Read + Write> {
3131
decode_needs: usize,
3232
/// The buffer.
3333
buffer: Buffer,
34-
/// Whether there is any more items to return from the stream. This is set to true once
35-
/// all decodable data in the buffer is returned and the underlying stream is closed.
36-
closed: bool,
3734
}
3835

3936
impl<R: Read + Write + Unpin> ImapStream<R> {
@@ -43,7 +40,6 @@ impl<R: Read + Write + Unpin> ImapStream<R> {
4340
inner,
4441
buffer: Buffer::new(),
4542
decode_needs: 0,
46-
closed: false,
4743
}
4844
}
4945

@@ -76,21 +72,6 @@ impl<R: Read + Write + Unpin> ImapStream<R> {
7672
pub fn as_mut(&mut self) -> &mut R {
7773
&mut self.inner
7874
}
79-
80-
/// End-Of-File return value.
81-
///
82-
/// Return the appropriate EOF value for the stream depending on whether there is still
83-
/// data in the buffer. It is assumed that any remaining data in the buffer can not be
84-
/// decoded.
85-
fn stream_eof_value(&self) -> Option<io::Result<ResponseData>> {
86-
match self.buffer.used() {
87-
0 => None,
88-
_ => Some(Err(io::Error::new(
89-
io::ErrorKind::UnexpectedEof,
90-
"bytes remaining in stream",
91-
))),
92-
}
93-
}
9475
}
9576

9677
impl<R: Read + Write + Unpin> ImapStream<R> {
@@ -281,9 +262,6 @@ impl<R: Read + Write + Unpin> Stream for ImapStream<R> {
281262
if let Some(response) = this.maybe_decode()? {
282263
return Poll::Ready(Some(Ok(response)));
283264
}
284-
if this.closed {
285-
return Poll::Ready(this.stream_eof_value());
286-
}
287265
loop {
288266
this.buffer.ensure_capacity(this.decode_needs)?;
289267
let buf = this.buffer.free_as_mut_slice();
@@ -300,8 +278,13 @@ impl<R: Read + Write + Unpin> Stream for ImapStream<R> {
300278
};
301279

302280
if num_bytes_read == 0 {
303-
this.closed = true;
304-
return Poll::Ready(this.stream_eof_value());
281+
if this.buffer.used() > 0 {
282+
return Poll::Ready(Some(Err(io::Error::new(
283+
io::ErrorKind::UnexpectedEof,
284+
"bytes remaining in stream",
285+
))));
286+
}
287+
return Poll::Ready(None);
305288
}
306289
this.buffer.extend_used(num_bytes_read);
307290
if let Some(response) = this.maybe_decode()? {

0 commit comments

Comments
 (0)