|
1 | 1 | mod flush;
|
2 | 2 | mod write;
|
3 | 3 | mod write_all;
|
| 4 | +mod write_fmt; |
4 | 5 | mod write_vectored;
|
5 | 6 |
|
6 | 7 | use flush::FlushFuture;
|
7 | 8 | use write::WriteFuture;
|
8 | 9 | use write_all::WriteAllFuture;
|
| 10 | +use write_fmt::WriteFmtFuture; |
9 | 11 | use write_vectored::WriteVectoredFuture;
|
10 | 12 |
|
11 | 13 | use cfg_if::cfg_if;
|
12 | 14 |
|
13 | 15 | use crate::io::IoSlice;
|
14 | 16 | use crate::utils::extension_trait;
|
15 | 17 |
|
| 18 | +use crate::io; |
| 19 | + |
16 | 20 | cfg_if! {
|
17 | 21 | if #[cfg(feature = "docs")] {
|
18 | 22 | use std::pin::Pin;
|
19 | 23 | use std::ops::{Deref, DerefMut};
|
20 |
| - |
21 |
| - use crate::io; |
22 | 24 | use crate::task::{Context, Poll};
|
23 | 25 | }
|
24 | 26 | }
|
@@ -197,6 +199,50 @@ extension_trait! {
|
197 | 199 | {
|
198 | 200 | WriteAllFuture { writer: self, buf }
|
199 | 201 | }
|
| 202 | + |
| 203 | + #[doc = r#" |
| 204 | + Writes a formatted string into this writer, returning any error encountered. |
| 205 | +
|
| 206 | + This method will continuously call [`write`] until there is no more data to be |
| 207 | + written or an error is returned. This future will not resolve until the entire |
| 208 | + buffer has been successfully written or such an error occurs. |
| 209 | +
|
| 210 | + [`write`]: #tymethod.write |
| 211 | +
|
| 212 | + # Examples |
| 213 | +
|
| 214 | + ```no_run |
| 215 | + # fn main() -> std::io::Result<()> { async_std::task::block_on(async { |
| 216 | + # |
| 217 | + use async_std::io::prelude::*; |
| 218 | + use async_std::fs::File; |
| 219 | +
|
| 220 | + let mut buffer = File::create("foo.txt").await?; |
| 221 | +
|
| 222 | + // this call |
| 223 | + write!(buffer, "{:.*}", 2, 1.234567).await?; |
| 224 | + // turns into this: |
| 225 | + buffer.write_fmt(format_args!("{:.*}", 2, 1.234567)).await?; |
| 226 | + # |
| 227 | + # Ok(()) }) } |
| 228 | + ``` |
| 229 | + "#] |
| 230 | + fn write_fmt<'a>( |
| 231 | + &'a mut self, |
| 232 | + fmt: std::fmt::Arguments<'_>, |
| 233 | + ) -> impl Future<Output = io::Result<()>> + 'a [WriteFmtFuture<'a, Self>] |
| 234 | + where |
| 235 | + Self: Unpin, |
| 236 | + { |
| 237 | + // In order to not have to implement an async version of `fmt` including private types |
| 238 | + // and all, we convert `Arguments` to a `Result<Vec<u8>>` and pass that to the Future. |
| 239 | + // Doing an owned conversion saves us from juggling references. |
| 240 | + let mut string = String::new(); |
| 241 | + let res = std::fmt::write(&mut string, fmt) |
| 242 | + .map(|_| string.into_bytes()) |
| 243 | + .map_err(|_| io::Error::new(io::ErrorKind::Other, "formatter error")); |
| 244 | + WriteFmtFuture { writer: self, res: Some(res), buffer: None, amt: 0 } |
| 245 | + } |
200 | 246 | }
|
201 | 247 |
|
202 | 248 | impl<T: Write + Unpin + ?Sized> Write for Box<T> {
|
|
0 commit comments