crabmole/
io.rs

1/// Pipe
2#[cfg(feature = "pipe")]
3#[cfg_attr(docsrs, doc(cfg(feature = "pipe")))]
4pub mod pipe;
5
6/// Pipe
7#[cfg(feature = "async-pipe")]
8#[cfg_attr(docsrs, doc(cfg(feature = "async-pipe")))]
9pub mod async_pipe;
10
11/// Error for pipe
12#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
13#[cfg(any(feature = "pipe", feature = "async-pipe"))]
14#[cfg_attr(docsrs, doc(cfg(any(feature = "pipe", feature = "async-pipe"))))]
15pub enum PipeError {
16    /// Read/Write on closed pipe
17    Closed,
18    /// EOF
19    Eof,
20}
21
22impl PipeError {
23    /// Returns true if the error is [`PipeError::Eof`]
24    #[inline]
25    pub const fn is_eof(&self) -> bool {
26        matches!(self, Self::Eof)
27    }
28
29    /// Returns true if the error is [`PipeError::Closed`]
30    #[inline]
31    pub const fn is_closed(&self) -> bool {
32        matches!(self, Self::Closed)
33    }
34}
35
36impl core::fmt::Display for PipeError {
37    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
38        match self {
39            PipeError::Closed => write!(f, "Pipe: read/write on closed pipe"),
40            PipeError::Eof => write!(f, "Pipe: EOF"),
41        }
42    }
43}
44
45impl std::error::Error for PipeError {}
46
47/// The trait that groups the basic [`std::io::Read`] and [`std::io::Write`].
48pub trait ReadWriter: std::io::Write + std::io::Read {}
49
50impl<T: std::io::Read + std::io::Write> ReadWriter for T {}
51
52/// The trait that groups the basic [`std::io::Read`] and [`Closer`].
53pub trait ReadCloser: std::io::Read + Closer {}
54
55impl<T: std::io::Read + Closer> ReadCloser for T {}
56
57/// The trait that groups the basic [`std::io::Write`] and [`Closer`].
58pub trait WriteCloser: std::io::Write + Closer {}
59
60impl<T: std::io::Write + Closer> WriteCloser for T {}
61
62/// The trait that groups the basic [`std::io::Read`], [`std::io::Write`] and [`Closer`].
63pub trait ReadWriteCloser: std::io::Read + Closer {}
64
65/// Closer is the trait that wraps the basic `close` method.
66///
67/// The behavior of `close` after the first call is undefined.
68/// Specific implementations may document their own behavior.
69pub trait Closer {
70    /// Close
71    fn close(&mut self) -> std::io::Result<()>;
72}