Skip to content

Commit b7d45ee

Browse files
committed
split io::write into multiple files
Signed-off-by: Yoshua Wuyts <yoshuawuyts@gmail.com>
1 parent b25b1d4 commit b7d45ee

File tree

5 files changed

+113
-79
lines changed

5 files changed

+113
-79
lines changed

src/io/write/flush.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use crate::future::Future;
2+
use crate::task::{Context, Poll};
3+
4+
use std::io;
5+
use std::pin::Pin;
6+
7+
use futures_io::AsyncWrite;
8+
9+
#[doc(hidden)]
10+
#[allow(missing_debug_implementations)]
11+
pub struct FlushFuture<'a, T: Unpin + ?Sized> {
12+
pub(crate) writer: &'a mut T,
13+
}
14+
15+
impl<T: AsyncWrite + Unpin + ?Sized> Future for FlushFuture<'_, T> {
16+
type Output = io::Result<()>;
17+
18+
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
19+
Pin::new(&mut *self.writer).poll_flush(cx)
20+
}
21+
}

src/io/write.rs renamed to src/io/write/mod.rs

Lines changed: 10 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1+
mod flush;
2+
mod write_all;
3+
mod write;
4+
mod write_vectored;
5+
6+
use flush::FlushFuture;
7+
use write_all::WriteAllFuture;
8+
use write::WriteFuture;
9+
use write_vectored::WriteVectoredFuture;
10+
111
use std::io::IoSlice;
2-
use std::mem;
3-
use std::pin::Pin;
412

513
use cfg_if::cfg_if;
614
use futures_io::AsyncWrite;
715

8-
use crate::future::Future;
9-
use crate::io;
10-
use crate::task::{Context, Poll};
11-
1216
cfg_if! {
1317
if #[cfg(feature = "docs")] {
1418
#[doc(hidden)]
@@ -140,76 +144,3 @@ impl<T: AsyncWrite + Unpin + ?Sized> Write for T {
140144
FlushFuture { writer: self }
141145
}
142146
}
143-
144-
#[doc(hidden)]
145-
#[allow(missing_debug_implementations)]
146-
pub struct WriteFuture<'a, T: Unpin + ?Sized> {
147-
writer: &'a mut T,
148-
buf: &'a [u8],
149-
}
150-
151-
impl<T: AsyncWrite + Unpin + ?Sized> Future for WriteFuture<'_, T> {
152-
type Output = io::Result<usize>;
153-
154-
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
155-
let buf = self.buf;
156-
Pin::new(&mut *self.writer).poll_write(cx, buf)
157-
}
158-
}
159-
160-
#[doc(hidden)]
161-
#[allow(missing_debug_implementations)]
162-
pub struct FlushFuture<'a, T: Unpin + ?Sized> {
163-
writer: &'a mut T,
164-
}
165-
166-
impl<T: AsyncWrite + Unpin + ?Sized> Future for FlushFuture<'_, T> {
167-
type Output = io::Result<()>;
168-
169-
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
170-
Pin::new(&mut *self.writer).poll_flush(cx)
171-
}
172-
}
173-
174-
#[doc(hidden)]
175-
#[allow(missing_debug_implementations)]
176-
pub struct WriteVectoredFuture<'a, T: Unpin + ?Sized> {
177-
writer: &'a mut T,
178-
bufs: &'a [IoSlice<'a>],
179-
}
180-
181-
impl<T: AsyncWrite + Unpin + ?Sized> Future for WriteVectoredFuture<'_, T> {
182-
type Output = io::Result<usize>;
183-
184-
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
185-
let bufs = self.bufs;
186-
Pin::new(&mut *self.writer).poll_write_vectored(cx, bufs)
187-
}
188-
}
189-
190-
#[doc(hidden)]
191-
#[allow(missing_debug_implementations)]
192-
pub struct WriteAllFuture<'a, T: Unpin + ?Sized> {
193-
writer: &'a mut T,
194-
buf: &'a [u8],
195-
}
196-
197-
impl<T: AsyncWrite + Unpin + ?Sized> Future for WriteAllFuture<'_, T> {
198-
type Output = io::Result<()>;
199-
200-
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
201-
let Self { writer, buf } = &mut *self;
202-
203-
while !buf.is_empty() {
204-
let n = futures_core::ready!(Pin::new(&mut **writer).poll_write(cx, buf))?;
205-
let (_, rest) = mem::replace(buf, &[]).split_at(n);
206-
*buf = rest;
207-
208-
if n == 0 {
209-
return Poll::Ready(Err(io::ErrorKind::WriteZero.into()));
210-
}
211-
}
212-
213-
Poll::Ready(Ok(()))
214-
}
215-
}

src/io/write/write.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use crate::future::Future;
2+
use crate::task::{Context, Poll};
3+
4+
use std::io;
5+
use std::pin::Pin;
6+
7+
use futures_io::AsyncWrite;
8+
9+
#[doc(hidden)]
10+
#[allow(missing_debug_implementations)]
11+
pub struct WriteFuture<'a, T: Unpin + ?Sized> {
12+
pub(crate) writer: &'a mut T,
13+
pub(crate) buf: &'a [u8],
14+
}
15+
16+
impl<T: AsyncWrite + Unpin + ?Sized> Future for WriteFuture<'_, T> {
17+
type Output = io::Result<usize>;
18+
19+
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
20+
let buf = self.buf;
21+
Pin::new(&mut *self.writer).poll_write(cx, buf)
22+
}
23+
}

src/io/write/write_all.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use crate::future::Future;
2+
use crate::task::{Context, Poll};
3+
4+
use std::io;
5+
use std::pin::Pin;
6+
use std::mem;
7+
8+
use futures_io::AsyncWrite;
9+
10+
#[doc(hidden)]
11+
#[allow(missing_debug_implementations)]
12+
pub struct WriteAllFuture<'a, T: Unpin + ?Sized> {
13+
pub(crate) writer: &'a mut T,
14+
pub(crate) buf: &'a [u8],
15+
}
16+
17+
impl<T: AsyncWrite + Unpin + ?Sized> Future for WriteAllFuture<'_, T> {
18+
type Output = io::Result<()>;
19+
20+
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
21+
let Self { writer, buf } = &mut *self;
22+
23+
while !buf.is_empty() {
24+
let n = futures_core::ready!(Pin::new(&mut **writer).poll_write(cx, buf))?;
25+
let (_, rest) = mem::replace(buf, &[]).split_at(n);
26+
*buf = rest;
27+
28+
if n == 0 {
29+
return Poll::Ready(Err(io::ErrorKind::WriteZero.into()));
30+
}
31+
}
32+
33+
Poll::Ready(Ok(()))
34+
}
35+
}

src/io/write/write_vectored.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use crate::future::Future;
2+
use crate::task::{Context, Poll};
3+
4+
use std::io;
5+
use std::pin::Pin;
6+
use std::io::IoSlice;
7+
8+
use futures_io::AsyncWrite;
9+
10+
#[doc(hidden)]
11+
#[allow(missing_debug_implementations)]
12+
pub struct WriteVectoredFuture<'a, T: Unpin + ?Sized> {
13+
pub(crate) writer: &'a mut T,
14+
pub(crate) bufs: &'a [IoSlice<'a>],
15+
}
16+
17+
impl<T: AsyncWrite + Unpin + ?Sized> Future for WriteVectoredFuture<'_, T> {
18+
type Output = io::Result<usize>;
19+
20+
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
21+
let bufs = self.bufs;
22+
Pin::new(&mut *self.writer).poll_write_vectored(cx, bufs)
23+
}
24+
}

0 commit comments

Comments
 (0)