ax_io/buffered/mod.rs
1mod bufreader;
2mod bufwriter;
3mod linewriter;
4
5use core::fmt;
6
7pub use self::{
8 bufreader::BufReader,
9 bufwriter::{BufWriter, WriterPanicked},
10 linewriter::LineWriter,
11};
12use crate::Error;
13
14/// An error returned by [`BufWriter::into_inner`] which combines an error that
15/// happened while writing out the buffer, and the buffered writer object
16/// which may be used to recover from the condition.
17#[derive(Debug)]
18pub struct IntoInnerError<W>(W, Error);
19
20impl<W> IntoInnerError<W> {
21 /// Constructs a new IntoInnerError
22 fn new(writer: W, error: Error) -> Self {
23 Self(writer, error)
24 }
25
26 /// Helper to construct a new IntoInnerError; intended to help with
27 /// adapters that wrap other adapters
28 fn new_wrapped<W2>(self, f: impl FnOnce(W) -> W2) -> IntoInnerError<W2> {
29 let Self(writer, error) = self;
30 IntoInnerError::new(f(writer), error)
31 }
32
33 /// Returns the error which caused the call to [`BufWriter::into_inner()`]
34 /// to fail.
35 ///
36 /// This error was returned when attempting to write the internal buffer.
37 pub fn error(&self) -> &Error {
38 &self.1
39 }
40
41 /// Returns the buffered writer instance which generated the error.
42 ///
43 /// The returned object can be used for error recovery, such as
44 /// re-inspecting the buffer.
45 pub fn into_inner(self) -> W {
46 self.0
47 }
48
49 /// Consumes the [`IntoInnerError`] and returns the error which caused the call to
50 /// [`BufWriter::into_inner()`] to fail. Unlike `error`, this can be used to
51 /// obtain ownership of the underlying error.
52 pub fn into_error(self) -> Error {
53 self.1
54 }
55
56 /// Consumes the [`IntoInnerError`] and returns the error which caused the call to
57 /// [`BufWriter::into_inner()`] to fail, and the underlying writer.
58 ///
59 /// This can be used to simply obtain ownership of the underlying error; it can also be used for
60 /// advanced error recovery.
61 pub fn into_parts(self) -> (Error, W) {
62 (self.1, self.0)
63 }
64}
65
66impl<W> From<IntoInnerError<W>> for Error {
67 fn from(iie: IntoInnerError<W>) -> Error {
68 iie.1
69 }
70}
71
72impl<W> fmt::Display for IntoInnerError<W> {
73 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
74 self.error().fmt(f)
75 }
76}