aboutsummaryrefslogtreecommitdiff
path: root/src/io/util/read_int.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/io/util/read_int.rs')
-rw-r--r--src/io/util/read_int.rs48
1 files changed, 34 insertions, 14 deletions
diff --git a/src/io/util/read_int.rs b/src/io/util/read_int.rs
index 9d37dc7..5b9fb7b 100644
--- a/src/io/util/read_int.rs
+++ b/src/io/util/read_int.rs
@@ -1,10 +1,11 @@
-use crate::io::AsyncRead;
+use crate::io::{AsyncRead, ReadBuf};
use bytes::Buf;
use pin_project_lite::pin_project;
use std::future::Future;
use std::io;
use std::io::ErrorKind::UnexpectedEof;
+use std::marker::PhantomPinned;
use std::mem::size_of;
use std::pin::Pin;
use std::task::{Context, Poll};
@@ -16,11 +17,15 @@ macro_rules! reader {
($name:ident, $ty:ty, $reader:ident, $bytes:expr) => {
pin_project! {
#[doc(hidden)]
+ #[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct $name<R> {
#[pin]
src: R,
buf: [u8; $bytes],
read: u8,
+ // Make this future `!Unpin` for compatibility with async trait methods.
+ #[pin]
+ _pin: PhantomPinned,
}
}
@@ -30,6 +35,7 @@ macro_rules! reader {
src,
buf: [0; $bytes],
read: 0,
+ _pin: PhantomPinned,
}
}
}
@@ -48,17 +54,19 @@ macro_rules! reader {
}
while *me.read < $bytes as u8 {
- *me.read += match me
- .src
- .as_mut()
- .poll_read(cx, &mut me.buf[*me.read as usize..])
- {
+ let mut buf = ReadBuf::new(&mut me.buf[*me.read as usize..]);
+
+ *me.read += match me.src.as_mut().poll_read(cx, &mut buf) {
Poll::Pending => return Poll::Pending,
Poll::Ready(Err(e)) => return Poll::Ready(Err(e.into())),
- Poll::Ready(Ok(0)) => {
- return Poll::Ready(Err(UnexpectedEof.into()));
+ Poll::Ready(Ok(())) => {
+ let n = buf.filled().len();
+ if n == 0 {
+ return Poll::Ready(Err(UnexpectedEof.into()));
+ }
+
+ n as u8
}
- Poll::Ready(Ok(n)) => n as u8,
};
}
@@ -75,15 +83,22 @@ macro_rules! reader8 {
pin_project! {
/// Future returned from `read_u8`
#[doc(hidden)]
+ #[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct $name<R> {
#[pin]
reader: R,
+ // Make this future `!Unpin` for compatibility with async trait methods.
+ #[pin]
+ _pin: PhantomPinned,
}
}
impl<R> $name<R> {
pub(crate) fn new(reader: R) -> $name<R> {
- $name { reader }
+ $name {
+ reader,
+ _pin: PhantomPinned,
+ }
}
}
@@ -97,12 +112,17 @@ macro_rules! reader8 {
let me = self.project();
let mut buf = [0; 1];
- match me.reader.poll_read(cx, &mut buf[..]) {
+ let mut buf = ReadBuf::new(&mut buf);
+ match me.reader.poll_read(cx, &mut buf) {
Poll::Pending => Poll::Pending,
Poll::Ready(Err(e)) => Poll::Ready(Err(e.into())),
- Poll::Ready(Ok(0)) => Poll::Ready(Err(UnexpectedEof.into())),
- Poll::Ready(Ok(1)) => Poll::Ready(Ok(buf[0] as $ty)),
- Poll::Ready(Ok(_)) => unreachable!(),
+ Poll::Ready(Ok(())) => {
+ if buf.filled().len() == 0 {
+ return Poll::Ready(Err(UnexpectedEof.into()));
+ }
+
+ Poll::Ready(Ok(buf.filled()[0] as $ty))
+ }
}
}
}