Skip to content

Commit a5fbaed

Browse files
committed
Auto merge of rust-lang#79673 - ijackson:intoinnerintoinnererror, r=m-ou-se
Provide IntoInnerError::into_parts Hi. This is an updated version of the IntoInnerError bits of my previous portmanteau MR rust-lang#78689. Thanks to `@jyn514` and `@m-ou-se` for helpful comments there. I have made this insta-stable since it seems like it will probably be uncontroversial, but that is definitely something that someone from the libs API team should be aware of and explicitly consider. I included a tangentially-related commit providing documentation of the buffer full behaviiour of `&mut [u8] as Write`; the behaviour I am documenting is relied on by the doctest for `into_parts`.
2 parents 3ff10e7 + b777552 commit a5fbaed

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

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

+45
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,51 @@ 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. Unlike `error`, this can be used to
132+
/// obtain ownership of the underlying error.
133+
///
134+
/// # Example
135+
/// ```
136+
/// #![feature(io_into_inner_error_parts)]
137+
/// use std::io::{BufWriter, ErrorKind, Write};
138+
///
139+
/// let mut not_enough_space = [0u8; 10];
140+
/// let mut stream = BufWriter::new(not_enough_space.as_mut());
141+
/// write!(stream, "this cannot be actually written").unwrap();
142+
/// let into_inner_err = stream.into_inner().expect_err("now we discover it's too small");
143+
/// let err = into_inner_err.into_error();
144+
/// assert_eq!(err.kind(), ErrorKind::WriteZero);
145+
/// ```
146+
#[unstable(feature = "io_into_inner_error_parts", issue = "79704")]
147+
pub fn into_error(self) -> Error {
148+
self.1
149+
}
150+
151+
/// Consumes the [`IntoInnerError`] and returns the error which caused the call to
152+
/// [`BufWriter::into_inner()`] to fail, and the underlying writer.
153+
///
154+
/// This can be used to simply obtain ownership of the underlying error; it can also be used for
155+
/// advanced error recovery.
156+
///
157+
/// # Example
158+
/// ```
159+
/// #![feature(io_into_inner_error_parts)]
160+
/// use std::io::{BufWriter, ErrorKind, Write};
161+
///
162+
/// let mut not_enough_space = [0u8; 10];
163+
/// let mut stream = BufWriter::new(not_enough_space.as_mut());
164+
/// write!(stream, "this cannot be actually written").unwrap();
165+
/// let into_inner_err = stream.into_inner().expect_err("now we discover it's too small");
166+
/// let (err, recovered_writer) = into_inner_err.into_parts();
167+
/// assert_eq!(err.kind(), ErrorKind::WriteZero);
168+
/// assert_eq!(recovered_writer.buffer(), b"t be actually written");
169+
/// ```
170+
#[unstable(feature = "io_into_inner_error_parts", issue = "79704")]
171+
pub fn into_parts(self) -> (Error, W) {
172+
(self.1, self.0)
173+
}
129174
}
130175

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

library/std/src/io/impls.rs

+4
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,10 @@ impl BufRead for &[u8] {
306306
///
307307
/// Note that writing updates the slice to point to the yet unwritten part.
308308
/// The slice will be empty when it has been completely overwritten.
309+
///
310+
/// If the number of bytes to be written exceeds the size of the slice, write operations will
311+
/// return short writes: ultimately, `Ok(0)`; in this situation, `write_all` returns an error of
312+
/// kind `ErrorKind::WriteZero`.
309313
#[stable(feature = "rust1", since = "1.0.0")]
310314
impl Write for &mut [u8] {
311315
#[inline]

0 commit comments

Comments
 (0)