aboutsummaryrefslogtreecommitdiff
path: root/src/io/util/write_all.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/io/util/write_all.rs')
-rw-r--r--src/io/util/write_all.rs34
1 files changed, 16 insertions, 18 deletions
diff --git a/src/io/util/write_all.rs b/src/io/util/write_all.rs
index 898006c..e59d41e 100644
--- a/src/io/util/write_all.rs
+++ b/src/io/util/write_all.rs
@@ -1,17 +1,22 @@
use crate::io::AsyncWrite;
+use pin_project_lite::pin_project;
use std::future::Future;
use std::io;
+use std::marker::PhantomPinned;
use std::mem;
use std::pin::Pin;
use std::task::{Context, Poll};
-cfg_io_util! {
+pin_project! {
#[derive(Debug)]
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct WriteAll<'a, W: ?Sized> {
writer: &'a mut W,
buf: &'a [u8],
+ // Make this future `!Unpin` for compatibility with async trait methods.
+ #[pin]
+ _pin: PhantomPinned,
}
}
@@ -19,7 +24,11 @@ pub(crate) fn write_all<'a, W>(writer: &'a mut W, buf: &'a [u8]) -> WriteAll<'a,
where
W: AsyncWrite + Unpin + ?Sized,
{
- WriteAll { writer, buf }
+ WriteAll {
+ writer,
+ buf,
+ _pin: PhantomPinned,
+ }
}
impl<W> Future for WriteAll<'_, W>
@@ -28,13 +37,13 @@ where
{
type Output = io::Result<()>;
- fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
- let me = &mut *self;
+ fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
+ let me = self.project();
while !me.buf.is_empty() {
- let n = ready!(Pin::new(&mut me.writer).poll_write(cx, me.buf))?;
+ let n = ready!(Pin::new(&mut *me.writer).poll_write(cx, me.buf))?;
{
- let (_, rest) = mem::replace(&mut me.buf, &[]).split_at(n);
- me.buf = rest;
+ let (_, rest) = mem::replace(&mut *me.buf, &[]).split_at(n);
+ *me.buf = rest;
}
if n == 0 {
return Poll::Ready(Err(io::ErrorKind::WriteZero.into()));
@@ -44,14 +53,3 @@ where
Poll::Ready(Ok(()))
}
}
-
-#[cfg(test)]
-mod tests {
- use super::*;
-
- #[test]
- fn assert_unpin() {
- use std::marker::PhantomPinned;
- crate::is_unpin::<WriteAll<'_, PhantomPinned>>();
- }
-}