Skip to content

Commit 0f2c14c

Browse files
committed
add missing documentation
1 parent 48d4c9b commit 0f2c14c

File tree

1 file changed

+56
-12
lines changed

1 file changed

+56
-12
lines changed

src/io/buf_writer.rs

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,20 @@ const DEFAULT_CAPACITY: usize = 8 * 1024;
4949
/// overhead of a system call per byte written. We can fix this with a
5050
/// `BufWriter`:
5151
///
52-
/*/ ```no_run
53-
/ use std::io::prelude::*;
54-
/ use std::io::BufWriter;
55-
/ use std::net::TcpStream;
56-
/
57-
/ let mut stream = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
58-
/
59-
/ for i in 0..10 {
60-
/ stream.write(&[i+1]).unwrap();
61-
/ }
62-
/ ```*/
52+
/// ```no_run
53+
/// use async_std::io::prelude::*;
54+
/// use async_std::io::BufWriter;
55+
/// use async_std::net::TcpStream;
56+
/// use futures::AsyncWrite;
57+
/// use async_std::io::Write;
58+
///
59+
/// async_std::task::block_on(async {
60+
/// let mut stream = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
61+
/// for i in 0..10 {
62+
/// stream.write(&[i+1]).await;
63+
/// }
64+
/// });
65+
/// ```
6366
///
6467
/// By wrapping the stream with a `BufWriter`, these ten writes are all grouped
6568
/// together by the buffer, and will all be written out in one system call when
@@ -115,10 +118,38 @@ impl<W: AsyncWrite + Unpin> BufWriter<W> {
115118
}
116119
}
117120

121+
/// Gets a reference to the underlying writer.
122+
///
123+
/// # Examples
124+
///
125+
/// ```no_run
126+
/// use async_std::io::BufWriter;
127+
/// use async_std::net::TcpStream;
128+
///
129+
/// let mut buffer = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
130+
///
131+
/// // we can use reference just like buffer
132+
/// let reference = buffer.get_ref();
133+
/// ```
118134
pub fn get_ref(&self) -> &W {
119135
&self.inner
120136
}
121137

138+
/// Gets a mutable reference to the underlying writer.
139+
///
140+
/// It is inadvisable to directly write to the underlying writer.
141+
///
142+
/// # Examples
143+
///
144+
/// ```no_run
145+
/// use async_std::io::BufWriter;
146+
/// use async_std::net::TcpStream;
147+
///
148+
/// let mut buffer = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
149+
///
150+
/// // we can use reference just like buffer
151+
/// let reference = buffer.get_mut();
152+
/// ```
122153
pub fn get_mut(&mut self) -> &mut W {
123154
&mut self.inner
124155
}
@@ -141,6 +172,19 @@ impl<W: AsyncWrite + Unpin> BufWriter<W> {
141172
unimplemented!("poll into inner method")
142173
}
143174

175+
/// Returns a reference to the internally buffered data.
176+
///
177+
/// # Examples
178+
///
179+
/// ```no_run
180+
/// use async_std::io::BufWriter;
181+
/// use async_std::net::TcpStream;
182+
///
183+
/// let buf_writer = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
184+
///
185+
/// // See how many bytes are currently buffered
186+
/// let bytes_buffered = buf_writer.buffer().len();
187+
/// ```
144188
pub fn buffer(&self) -> &[u8] {
145189
&self.buf
146190
}
@@ -192,7 +236,7 @@ impl<W: AsyncWrite + Unpin> AsyncWrite for BufWriter<W> {
192236
if buf.len() >= self.buf.capacity() {
193237
self.inner().poll_write(cx, buf)
194238
} else {
195-
self.buf().write(buf).poll()
239+
self.buf().write(buf).poll(cx)
196240
}
197241
}
198242

0 commit comments

Comments
 (0)