@@ -4,6 +4,7 @@ use std::pin::Pin;
4
4
use futures_core:: ready;
5
5
6
6
use crate :: io:: { self , Seek , SeekFrom , Write } ;
7
+ use crate :: io:: write:: WriteExt ;
7
8
use crate :: task:: { Context , Poll } ;
8
9
9
10
const DEFAULT_CAPACITY : usize = 8 * 1024 ;
@@ -83,6 +84,9 @@ pub struct BufWriter<W> {
83
84
written : usize ,
84
85
}
85
86
87
+ #[ derive( Debug ) ]
88
+ pub struct IntoInnerError < W > ( W , std:: io:: Error ) ;
89
+
86
90
impl < W : Write > BufWriter < W > {
87
91
pin_utils:: unsafe_pinned!( inner: W ) ;
88
92
pin_utils:: unsafe_unpinned!( buf: Vec <u8 >) ;
@@ -180,8 +184,28 @@ impl<W: Write> BufWriter<W> {
180
184
/// For method that will attempt to write before returning the writer see [`poll_into_inner`]
181
185
///
182
186
/// [`poll_into_inner`]: #method.poll_into_inner
183
- pub fn into_inner ( self ) -> W {
184
- self . inner
187
+ /// # Examples
188
+ ///
189
+ /// ```no_run
190
+ /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
191
+ /// use async_std::io::BufWriter;
192
+ /// use async_std::net::TcpStream;
193
+ ///
194
+ /// let buf_writer = BufWriter::new(TcpStream::connect("127.0.0.1:34251").await?);
195
+ ///
196
+ /// // unwrap the TcpStream and flush the buffer
197
+ /// let stream = buf_writer.into_inner().await.unwrap();
198
+ /// #
199
+ /// # Ok(()) }) }
200
+ /// ```
201
+ pub async fn into_inner ( mut self ) -> Result < W , IntoInnerError < BufWriter < W > > >
202
+ where
203
+ Self : Unpin
204
+ {
205
+ match self . flush ( ) . await {
206
+ Err ( e) => Err ( IntoInnerError ( self , e) ) ,
207
+ Ok ( ( ) ) => Ok ( self . inner ) ,
208
+ }
185
209
}
186
210
187
211
/// Returns a reference to the internally buffered data.
0 commit comments