Skip to content

Commit 19c7619

Browse files
committed
IntoInnerError: Provide into_parts
In particular, IntoIneerError only currently provides .error() which returns a reference, not an owned value. This is not helpful and means that a caller of BufWriter::into_inner cannot acquire an owned io::Error which seems quite wrong. Signed-off-by: Ian Jackson <[email protected]>
1 parent db5d697 commit 19c7619

File tree

1 file changed

+24
-0
lines changed
  • library/std/src/io/buffered

1 file changed

+24
-0
lines changed

library/std/src/io/buffered/mod.rs

+24
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,30 @@ impl<W> IntoInnerError<W> {
126126
pub fn into_inner(self) -> W {
127127
self.0
128128
}
129+
130+
/// Consumes the [`IntoInnerError`] and returns the error which caused the call to
131+
/// [`BufWriter::into_inner()`] to fail, and the underlying writer.
132+
///
133+
/// This can be used to simply obtain ownership of the underlying error; it can also be used for
134+
/// advanced error recovery.
135+
///
136+
/// # Example
137+
/// ```
138+
/// #![feature(io_into_inner_error_parts)]
139+
/// use std::io::{BufWriter, ErrorKind, Write};
140+
///
141+
/// let mut not_enough_space = [0u8; 10];
142+
/// let mut stream = BufWriter::new(not_enough_space.as_mut());
143+
/// write!(stream, "this cannot be actually written").unwrap();
144+
/// let into_inner_err = stream.into_inner().expect_err("now we discover it's too small");
145+
/// let (err, recovered_writer) = into_inner_err.into_parts();
146+
/// assert_eq!(err.kind(), ErrorKind::WriteZero);
147+
/// assert_eq!(recovered_writer.buffer(), b"t be actually written");
148+
/// ```
149+
#[unstable(feature = "io_into_inner_error_parts", issue = "79704")]
150+
pub fn into_parts(self) -> (Error, W) {
151+
(self.1, self.0)
152+
}
129153
}
130154

131155
#[stable(feature = "rust1", since = "1.0.0")]

0 commit comments

Comments
 (0)