From 4a2194f37c3489360f8206642b4a045c4755cb33 Mon Sep 17 00:00:00 2001 From: Yoshua Wuyts Date: Fri, 6 Sep 2019 13:04:20 +0200 Subject: [PATCH] split io::write into multiple files Signed-off-by: Yoshua Wuyts --- src/io/write/flush.rs | 21 ++++++++ src/io/{write.rs => write/mod.rs} | 89 ++++--------------------------- src/io/write/write.rs | 23 ++++++++ src/io/write/write_all.rs | 35 ++++++++++++ src/io/write/write_vectored.rs | 24 +++++++++ 5 files changed, 113 insertions(+), 79 deletions(-) create mode 100644 src/io/write/flush.rs rename src/io/{write.rs => write/mod.rs} (66%) create mode 100644 src/io/write/write.rs create mode 100644 src/io/write/write_all.rs create mode 100644 src/io/write/write_vectored.rs diff --git a/src/io/write/flush.rs b/src/io/write/flush.rs new file mode 100644 index 0000000..21ada15 --- /dev/null +++ b/src/io/write/flush.rs @@ -0,0 +1,21 @@ +use crate::future::Future; +use crate::task::{Context, Poll}; + +use std::io; +use std::pin::Pin; + +use futures_io::AsyncWrite; + +#[doc(hidden)] +#[allow(missing_debug_implementations)] +pub struct FlushFuture<'a, T: Unpin + ?Sized> { + pub(crate) writer: &'a mut T, +} + +impl Future for FlushFuture<'_, T> { + type Output = io::Result<()>; + + fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + Pin::new(&mut *self.writer).poll_flush(cx) + } +} diff --git a/src/io/write.rs b/src/io/write/mod.rs similarity index 66% rename from src/io/write.rs rename to src/io/write/mod.rs index d332eec..41a00c1 100644 --- a/src/io/write.rs +++ b/src/io/write/mod.rs @@ -1,14 +1,18 @@ +mod flush; +mod write_all; +mod write; +mod write_vectored; + +use flush::FlushFuture; +use write_all::WriteAllFuture; +use write::WriteFuture; +use write_vectored::WriteVectoredFuture; + use std::io::IoSlice; -use std::mem; -use std::pin::Pin; use cfg_if::cfg_if; use futures_io::AsyncWrite; -use crate::future::Future; -use crate::io; -use crate::task::{Context, Poll}; - cfg_if! { if #[cfg(feature = "docs")] { #[doc(hidden)] @@ -140,76 +144,3 @@ impl Write for T { FlushFuture { writer: self } } } - -#[doc(hidden)] -#[allow(missing_debug_implementations)] -pub struct WriteFuture<'a, T: Unpin + ?Sized> { - writer: &'a mut T, - buf: &'a [u8], -} - -impl Future for WriteFuture<'_, T> { - type Output = io::Result; - - fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - let buf = self.buf; - Pin::new(&mut *self.writer).poll_write(cx, buf) - } -} - -#[doc(hidden)] -#[allow(missing_debug_implementations)] -pub struct FlushFuture<'a, T: Unpin + ?Sized> { - writer: &'a mut T, -} - -impl Future for FlushFuture<'_, T> { - type Output = io::Result<()>; - - fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - Pin::new(&mut *self.writer).poll_flush(cx) - } -} - -#[doc(hidden)] -#[allow(missing_debug_implementations)] -pub struct WriteVectoredFuture<'a, T: Unpin + ?Sized> { - writer: &'a mut T, - bufs: &'a [IoSlice<'a>], -} - -impl Future for WriteVectoredFuture<'_, T> { - type Output = io::Result; - - fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - let bufs = self.bufs; - Pin::new(&mut *self.writer).poll_write_vectored(cx, bufs) - } -} - -#[doc(hidden)] -#[allow(missing_debug_implementations)] -pub struct WriteAllFuture<'a, T: Unpin + ?Sized> { - writer: &'a mut T, - buf: &'a [u8], -} - -impl Future for WriteAllFuture<'_, T> { - type Output = io::Result<()>; - - fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - let Self { writer, buf } = &mut *self; - - while !buf.is_empty() { - let n = futures_core::ready!(Pin::new(&mut **writer).poll_write(cx, buf))?; - let (_, rest) = mem::replace(buf, &[]).split_at(n); - *buf = rest; - - if n == 0 { - return Poll::Ready(Err(io::ErrorKind::WriteZero.into())); - } - } - - Poll::Ready(Ok(())) - } -} diff --git a/src/io/write/write.rs b/src/io/write/write.rs new file mode 100644 index 0000000..361e6af --- /dev/null +++ b/src/io/write/write.rs @@ -0,0 +1,23 @@ +use crate::future::Future; +use crate::task::{Context, Poll}; + +use std::io; +use std::pin::Pin; + +use futures_io::AsyncWrite; + +#[doc(hidden)] +#[allow(missing_debug_implementations)] +pub struct WriteFuture<'a, T: Unpin + ?Sized> { + pub(crate) writer: &'a mut T, + pub(crate) buf: &'a [u8], +} + +impl Future for WriteFuture<'_, T> { + type Output = io::Result; + + fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + let buf = self.buf; + Pin::new(&mut *self.writer).poll_write(cx, buf) + } +} diff --git a/src/io/write/write_all.rs b/src/io/write/write_all.rs new file mode 100644 index 0000000..a5c3090 --- /dev/null +++ b/src/io/write/write_all.rs @@ -0,0 +1,35 @@ +use crate::future::Future; +use crate::task::{Context, Poll}; + +use std::io; +use std::pin::Pin; +use std::mem; + +use futures_io::AsyncWrite; + +#[doc(hidden)] +#[allow(missing_debug_implementations)] +pub struct WriteAllFuture<'a, T: Unpin + ?Sized> { + pub(crate) writer: &'a mut T, + pub(crate) buf: &'a [u8], +} + +impl Future for WriteAllFuture<'_, T> { + type Output = io::Result<()>; + + fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + let Self { writer, buf } = &mut *self; + + while !buf.is_empty() { + let n = futures_core::ready!(Pin::new(&mut **writer).poll_write(cx, buf))?; + let (_, rest) = mem::replace(buf, &[]).split_at(n); + *buf = rest; + + if n == 0 { + return Poll::Ready(Err(io::ErrorKind::WriteZero.into())); + } + } + + Poll::Ready(Ok(())) + } +} diff --git a/src/io/write/write_vectored.rs b/src/io/write/write_vectored.rs new file mode 100644 index 0000000..fadf2fc --- /dev/null +++ b/src/io/write/write_vectored.rs @@ -0,0 +1,24 @@ +use crate::future::Future; +use crate::task::{Context, Poll}; + +use std::io; +use std::pin::Pin; +use std::io::IoSlice; + +use futures_io::AsyncWrite; + +#[doc(hidden)] +#[allow(missing_debug_implementations)] +pub struct WriteVectoredFuture<'a, T: Unpin + ?Sized> { + pub(crate) writer: &'a mut T, + pub(crate) bufs: &'a [IoSlice<'a>], +} + +impl Future for WriteVectoredFuture<'_, T> { + type Output = io::Result; + + fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + let bufs = self.bufs; + Pin::new(&mut *self.writer).poll_write_vectored(cx, bufs) + } +}