1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use std::pin::Pin;
use std::future::Future;
use crate::io::{self, Write};
use crate::task::{Context, Poll};
#[doc(hidden)]
#[allow(missing_debug_implementations)]
#[must_use]
pub struct WriteFmtFuture<'a, T: Unpin + ?Sized> {
pub(crate) writer: &'a mut T,
pub(crate) res: Option<io::Result<Vec<u8>>>,
pub(crate) buffer: Option<Vec<u8>>,
pub(crate) amt: u64,
}
impl<T: Write + Unpin + ?Sized> Future for WriteFmtFuture<'_, T> {
type Output = io::Result<()>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
if self.buffer.is_none() {
match self.res.take().unwrap() {
Err(err) => return Poll::Ready(Err(err)),
Ok(buffer) => self.buffer = Some(buffer),
};
}
let Self {
writer,
amt,
buffer,
..
} = &mut *self;
let buffer = buffer.as_mut().unwrap();
loop {
if *amt == buffer.len() as u64 {
futures_core::ready!(Pin::new(&mut **writer).poll_flush(cx))?;
return Poll::Ready(Ok(()));
}
let i = futures_core::ready!(Pin::new(&mut **writer).poll_write(cx, buffer))?;
if i == 0 {
return Poll::Ready(Err(io::ErrorKind::WriteZero.into()));
}
*amt += i as u64;
}
}
}