Skip to content

Commit ba338e3

Browse files
committed
Merge ImapStream.decode() and ImapStream.maybe_decode()
maybe_decode() is an optimized version of decode() and decode() should never be called directly. Merge them into a single decode() function.
1 parent ad62aad commit ba338e3

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/imap_stream.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,16 @@ impl<R: Read + Write + Unpin> ImapStream<R> {
7575
}
7676

7777
impl<R: Read + Write + Unpin> ImapStream<R> {
78-
fn maybe_decode(&mut self) -> io::Result<Option<ResponseData>> {
79-
if self.buffer.used() >= self.decode_needs {
80-
self.decode()
81-
} else {
82-
Ok(None)
78+
/// Attempts to decode a single response from the buffer.
79+
///
80+
/// Returns `None` if the buffer does not contain enough data.
81+
fn decode(&mut self) -> io::Result<Option<ResponseData>> {
82+
if self.buffer.used() < self.decode_needs {
83+
// We know that there is not enough data to decode anything
84+
// from previous attempts.
85+
return Ok(None);
8386
}
84-
}
8587

86-
fn decode(&mut self) -> io::Result<Option<ResponseData>> {
8788
let block: Block<'static> = self.buffer.take_block();
8889
// Be aware, now self.buffer is invalid until block is returned or reset!
8990

@@ -259,7 +260,7 @@ impl<R: Read + Write + Unpin> Stream for ImapStream<R> {
259260

260261
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
261262
let this = &mut *self;
262-
if let Some(response) = this.maybe_decode()? {
263+
if let Some(response) = this.decode()? {
263264
return Poll::Ready(Some(Ok(response)));
264265
}
265266
loop {
@@ -287,7 +288,7 @@ impl<R: Read + Write + Unpin> Stream for ImapStream<R> {
287288
return Poll::Ready(None);
288289
}
289290
this.buffer.extend_used(num_bytes_read);
290-
if let Some(response) = this.maybe_decode()? {
291+
if let Some(response) = this.decode()? {
291292
return Poll::Ready(Some(Ok(response)));
292293
}
293294
}

0 commit comments

Comments
 (0)