1 //! Buffering wrappers for I/O traits
15 #[stable(feature = "rust1", since = "1.0.0")]
16 pub use self::{bufreader::BufReader, bufwriter::BufWriter, linewriter::LineWriter};
17 use linewritershim::LineWriterShim;
19 #[stable(feature = "bufwriter_into_parts", since = "1.56.0")]
20 pub use bufwriter::WriterPanicked;
22 /// An error returned by [`BufWriter::into_inner`] which combines an error that
23 /// happened while writing out the buffer, and the buffered writer object
24 /// which may be used to recover from the condition.
29 /// use std::io::BufWriter;
30 /// use std::net::TcpStream;
32 /// let mut stream = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
34 /// // do stuff with the stream
36 /// // we want to get our `TcpStream` back, so let's try:
38 /// let stream = match stream.into_inner() {
41 /// // Here, e is an IntoInnerError
42 /// panic!("An error occurred");
47 #[stable(feature = "rust1", since = "1.0.0")]
48 pub struct IntoInnerError<W>(W, Error);
50 impl<W> IntoInnerError<W> {
51 /// Construct a new IntoInnerError
52 fn new(writer: W, error: Error) -> Self {
56 /// Helper to construct a new IntoInnerError; intended to help with
57 /// adapters that wrap other adapters
58 fn new_wrapped<W2>(self, f: impl FnOnce(W) -> W2) -> IntoInnerError<W2> {
59 let Self(writer, error) = self;
60 IntoInnerError::new(f(writer), error)
63 /// Returns the error which caused the call to [`BufWriter::into_inner()`]
66 /// This error was returned when attempting to write the internal buffer.
71 /// use std::io::BufWriter;
72 /// use std::net::TcpStream;
74 /// let mut stream = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
76 /// // do stuff with the stream
78 /// // we want to get our `TcpStream` back, so let's try:
80 /// let stream = match stream.into_inner() {
83 /// // Here, e is an IntoInnerError, let's log the inner error.
85 /// // We'll just 'log' to stdout for this example.
86 /// println!("{}", e.error());
88 /// panic!("An unexpected error occurred.");
92 #[stable(feature = "rust1", since = "1.0.0")]
93 pub fn error(&self) -> &Error {
97 /// Returns the buffered writer instance which generated the error.
99 /// The returned object can be used for error recovery, such as
100 /// re-inspecting the buffer.
105 /// use std::io::BufWriter;
106 /// use std::net::TcpStream;
108 /// let mut stream = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
110 /// // do stuff with the stream
112 /// // we want to get our `TcpStream` back, so let's try:
114 /// let stream = match stream.into_inner() {
117 /// // Here, e is an IntoInnerError, let's re-examine the buffer:
118 /// let buffer = e.into_inner();
120 /// // do stuff to try to recover
122 /// // afterwards, let's just return the stream
123 /// buffer.into_inner().unwrap()
127 #[stable(feature = "rust1", since = "1.0.0")]
128 pub fn into_inner(self) -> W {
132 /// Consumes the [`IntoInnerError`] and returns the error which caused the call to
133 /// [`BufWriter::into_inner()`] to fail. Unlike `error`, this can be used to
134 /// obtain ownership of the underlying error.
138 /// use std::io::{BufWriter, ErrorKind, Write};
140 /// let mut not_enough_space = [0u8; 10];
141 /// let mut stream = BufWriter::new(not_enough_space.as_mut());
142 /// write!(stream, "this cannot be actually written").unwrap();
143 /// let into_inner_err = stream.into_inner().expect_err("now we discover it's too small");
144 /// let err = into_inner_err.into_error();
145 /// assert_eq!(err.kind(), ErrorKind::WriteZero);
147 #[stable(feature = "io_into_inner_error_parts", since = "1.55.0")]
148 pub fn into_error(self) -> Error {
152 /// Consumes the [`IntoInnerError`] and returns the error which caused the call to
153 /// [`BufWriter::into_inner()`] to fail, and the underlying writer.
155 /// This can be used to simply obtain ownership of the underlying error; it can also be used for
156 /// advanced error recovery.
160 /// use std::io::{BufWriter, ErrorKind, Write};
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");
170 #[stable(feature = "io_into_inner_error_parts", since = "1.55.0")]
171 pub fn into_parts(self) -> (Error, W) {
176 #[stable(feature = "rust1", since = "1.0.0")]
177 impl<W> From<IntoInnerError<W>> for Error {
178 fn from(iie: IntoInnerError<W>) -> Error {
183 #[stable(feature = "rust1", since = "1.0.0")]
184 impl<W: Send + fmt::Debug> error::Error for IntoInnerError<W> {
185 #[allow(deprecated, deprecated_in_future)]
186 fn description(&self) -> &str {
187 error::Error::description(self.error())
191 #[stable(feature = "rust1", since = "1.0.0")]
192 impl<W> fmt::Display for IntoInnerError<W> {
193 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {