aboutsummaryrefslogtreecommitdiff
path: root/src/io/util/read.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/io/util/read.rs')
-rw-r--r--src/io/util/read.rs34
1 files changed, 17 insertions, 17 deletions
diff --git a/src/io/util/read.rs b/src/io/util/read.rs
index a8ca370..edc9d5a 100644
--- a/src/io/util/read.rs
+++ b/src/io/util/read.rs
@@ -1,7 +1,9 @@
-use crate::io::AsyncRead;
+use crate::io::{AsyncRead, ReadBuf};
+use pin_project_lite::pin_project;
use std::future::Future;
use std::io;
+use std::marker::PhantomPinned;
use std::marker::Unpin;
use std::pin::Pin;
use std::task::{Context, Poll};
@@ -15,10 +17,14 @@ pub(crate) fn read<'a, R>(reader: &'a mut R, buf: &'a mut [u8]) -> Read<'a, R>
where
R: AsyncRead + Unpin + ?Sized,
{
- Read { reader, buf }
+ Read {
+ reader,
+ buf,
+ _pin: PhantomPinned,
+ }
}
-cfg_io_util! {
+pin_project! {
/// A future which can be used to easily read available number of bytes to fill
/// a buffer.
///
@@ -28,6 +34,9 @@ cfg_io_util! {
pub struct Read<'a, R: ?Sized> {
reader: &'a mut R,
buf: &'a mut [u8],
+ // Make this future `!Unpin` for compatibility with async trait methods.
+ #[pin]
+ _pin: PhantomPinned,
}
}
@@ -37,19 +46,10 @@ where
{
type Output = io::Result<usize>;
- fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<usize>> {
- let me = &mut *self;
- Pin::new(&mut *me.reader).poll_read(cx, me.buf)
- }
-}
-
-#[cfg(test)]
-mod tests {
- use super::*;
-
- #[test]
- fn assert_unpin() {
- use std::marker::PhantomPinned;
- crate::is_unpin::<Read<'_, PhantomPinned>>();
+ fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<usize>> {
+ let me = self.project();
+ let mut buf = ReadBuf::new(*me.buf);
+ ready!(Pin::new(me.reader).poll_read(cx, &mut buf))?;
+ Poll::Ready(Ok(buf.filled().len()))
}
}