Skip to content

Commit 169d809

Browse files
committed
try to fix msvc once again
1 parent c66eb1c commit 169d809

File tree

2 files changed

+124
-12
lines changed

2 files changed

+124
-12
lines changed

src/io/buf_writer.rs

Lines changed: 124 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,9 @@ impl<W: AsyncWrite + Unpin> BufWriter<W> {
178178
&mut self.inner
179179
}
180180

181-
pub fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut W> {
182-
self.inner()
183-
}
181+
// pub fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut W> {
182+
// self.inner()
183+
// }
184184

185185
/// Consumes BufWriter, returning the underlying writer
186186
///
@@ -192,9 +192,9 @@ impl<W: AsyncWrite + Unpin> BufWriter<W> {
192192
self.inner
193193
}
194194

195-
pub fn poll_into_inner(self: Pin<&mut Self>, _cx: Context<'_>) -> Poll<io::Result<usize>> {
196-
unimplemented!("poll into inner method")
197-
}
195+
// pub fn poll_into_inner(self: Pin<&mut Self>, _cx: Context<'_>) -> Poll<io::Result<usize>> {
196+
// unimplemented!("poll into inner method")
197+
// }
198198

199199
/// Returns a reference to the internally buffered data.
200200
///
@@ -217,7 +217,11 @@ impl<W: AsyncWrite + Unpin> BufWriter<W> {
217217
&self.buf
218218
}
219219

220-
220+
/// Poll buffer flushing until completion
221+
///
222+
/// This is used in types that wrap around BufWrite, one such example: [`LineWriter`]
223+
///
224+
/// [`LineWriter`]: struct.LineWriter.html
221225
pub fn poll_flush_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
222226
let Self {
223227
inner,
@@ -289,6 +293,73 @@ impl<W: AsyncWrite + fmt::Debug + Unpin> fmt::Debug for BufWriter<W> {
289293
}
290294
}
291295

296+
/// Wraps a writer and buffers output to it, flushing whenever a newline
297+
/// (`0x0a`, `'\n'`) is detected.
298+
///
299+
/// The [`BufWriter`][bufwriter] struct wraps a writer and buffers its output.
300+
/// But it only does this batched write when it goes out of scope, or when the
301+
/// internal buffer is full. Sometimes, you'd prefer to write each line as it's
302+
/// completed, rather than the entire buffer at once. Enter `LineWriter`. It
303+
/// does exactly that.
304+
///
305+
/// Like [`BufWriter`][bufwriter], a `LineWriter`’s buffer will also be flushed when the
306+
/// `LineWriter` goes out of scope or when its internal buffer is full.
307+
///
308+
/// [bufwriter]: struct.BufWriter.html
309+
///
310+
/// If there's still a partial line in the buffer when the `LineWriter` is
311+
/// dropped, it will flush those contents.
312+
///
313+
/// This type is an async version of [`std::io::LineWriter`]
314+
///
315+
/// [`std::io::LineWriter`]: https://doc.rust-lang.org/std/io/struct.LineWriter.html
316+
///
317+
/// # Examples
318+
///
319+
/// We can use `LineWriter` to write one line at a time, significantly
320+
/// reducing the number of actual writes to the file.
321+
///
322+
/// ```no_run
323+
/// # #![feature(async_await)]
324+
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
325+
/// use async_std::io::{LineWriter, Write};
326+
/// use async_std::fs::{File, self};
327+
/// let road_not_taken = b"I shall be telling this with a sigh
328+
/// Somewhere ages and ages hence:
329+
/// Two roads diverged in a wood, and I -
330+
/// I took the one less traveled by,
331+
/// And that has made all the difference.";
332+
///
333+
/// let file = File::create("poem.txt").await?;
334+
/// let mut file = LineWriter::new(file);
335+
///
336+
/// file.write_all(b"I shall be telling this with a sigh").await?;
337+
///
338+
/// // No bytes are written until a newline is encountered (or
339+
/// // the internal buffer is filled).
340+
/// assert_eq!(fs::read_to_string("poem.txt").await?, "");
341+
/// file.write_all(b"\n").await?;
342+
/// assert_eq!(
343+
/// fs::read_to_string("poem.txt").await?,
344+
/// "I shall be telling this with a sigh\n",
345+
/// );
346+
///
347+
/// // Write the rest of the poem.
348+
/// file.write_all(b"Somewhere ages and ages hence:
349+
/// Two roads diverged in a wood, and I -
350+
/// I took the one less traveled by,
351+
/// And that has made all the difference.").await?;
352+
///
353+
/// // The last line of the poem doesn't end in a newline, so
354+
/// // we have to flush or drop the `LineWriter` to finish
355+
/// // writing.
356+
/// file.flush().await?;
357+
///
358+
/// // Confirm the whole poem was written.
359+
/// assert_eq!(fs::read("poem.txt").await?, &road_not_taken[..]);
360+
/// #
361+
/// # Ok(()) }) }
362+
/// ```
292363
pub struct LineWriter<W: AsyncWrite + Unpin> {
293364
inner: BufWriter<W>,
294365
need_flush: bool,
@@ -340,14 +411,57 @@ impl<W: AsyncWrite + Unpin> LineWriter<W> {
340411
}
341412
}
342413

414+
/// Gets a reference to the underlying writer.
415+
///
416+
/// # Examples
417+
///
418+
/// ```no_run
419+
/// # #![feature(async_await)]
420+
/// # #![allow(unused_mut)]
421+
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
422+
/// use async_std::io::LineWriter;
423+
/// use async_std::fs::File;
424+
///
425+
/// let file = File::create("poem.txt").await?;
426+
/// let file = LineWriter::new(file);
427+
///
428+
/// // We can use reference just like buffer
429+
/// let reference = file.get_ref();
430+
/// #
431+
/// # Ok(()) }) }
432+
/// ```
343433
pub fn get_ref(&self) -> &W {
344434
self.inner.get_ref()
345435
}
346436

437+
/// Gets a mutable reference to the underlying writer.
438+
///
439+
/// Caution must be taken when calling methods on the mutable reference
440+
/// returned as extra writes could corrupt the output stream.
441+
///
442+
/// # Examples
443+
///
444+
/// ```no_run
445+
/// # #![feature(async_await)]
446+
/// # #![allow(unused_mut)]
447+
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
448+
/// use async_std::io::LineWriter;
449+
/// use async_std::fs::File;
450+
///
451+
/// let file = File::create("poem.txt").await?;
452+
/// let mut file = LineWriter::new(file);
453+
///
454+
/// // We can use reference just like buffer
455+
/// let reference = file.get_mut();
456+
/// #
457+
/// # Ok(()) }) }
458+
/// ```
347459
pub fn get_mut(&mut self) -> &mut W {
348460
self.inner.get_mut()
349461
}
350462

463+
//TODO: Implement flushing before returning inner
464+
#[allow(missing_docs)]
351465
pub fn into_inner(self) -> W {
352466
self.inner.into_inner()
353467
}
@@ -360,7 +474,7 @@ impl<W: AsyncWrite + Unpin> AsyncWrite for LineWriter<W> {
360474
buf: &[u8],
361475
) -> Poll<io::Result<usize>> {
362476
if self.need_flush {
363-
self.as_mut().poll_flush(cx)?;
477+
let _ = self.as_mut().poll_flush(cx)?;
364478
}
365479

366480
let i = match memchr::memrchr(b'\n', buf) {
@@ -380,13 +494,13 @@ impl<W: AsyncWrite + Unpin> AsyncWrite for LineWriter<W> {
380494
}
381495

382496
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
383-
self.as_mut().inner().poll_flush(cx)?;
497+
let _ = self.as_mut().inner().poll_flush(cx)?;
384498
*self.need_flush() = false;
385499
Poll::Ready(Ok(()))
386500
}
387501

388502
fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
389-
self.as_mut().inner().poll_flush(cx)?;
503+
let _ = self.as_mut().inner().poll_flush(cx)?;
390504
self.inner().poll_close(cx)
391505
}
392506
}

src/io/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
//! # Ok(()) }) }
2222
//! ```
2323
24-
pub(crate) const DEFAULT_CAPACITY: usize = 8 * 1024;
25-
2624
#[doc(inline)]
2725
pub use std::io::{Error, ErrorKind, Result, SeekFrom};
2826

0 commit comments

Comments
 (0)