@@ -3,6 +3,7 @@ use std::pin::Pin;
33
44use futures_core:: ready;
55
6+ use crate :: io:: write:: WriteExt ;
67use crate :: io:: { self , Seek , SeekFrom , Write } ;
78use crate :: task:: { Context , Poll } ;
89
@@ -83,6 +84,9 @@ pub struct BufWriter<W> {
8384 written : usize ,
8485}
8586
87+ #[ derive( Debug ) ]
88+ pub struct IntoInnerError < W > ( W , std:: io:: Error ) ;
89+
8690impl < W : Write > BufWriter < W > {
8791 pin_utils:: unsafe_pinned!( inner: W ) ;
8892 pin_utils:: unsafe_unpinned!( buf: Vec <u8 >) ;
@@ -180,8 +184,28 @@ impl<W: Write> BufWriter<W> {
180184 /// For method that will attempt to write before returning the writer see [`poll_into_inner`]
181185 ///
182186 /// [`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+ }
185209 }
186210
187211 /// Returns a reference to the internally buffered data.
0 commit comments