Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,14 @@ impl std::error::Error for Error {}
impl Error {
/// Convert the error into a [`std::io::Error`].
///
/// If the error is [`Error::Io`], we unpack the error. In othe cases we make
/// an `std::io::ErrorKind::Other`.
/// If the error is [`Error::Io`], we unpack the error. If the error is
/// [`Error::Timeout`] we translate to `io::ErrorKind::TimedOut`.
/// In other cases we make it an `std::io::ErrorKind::Other`.
pub fn into_io(self) -> io::Error {
if let Self::Io(e) = self {
e
} else if let Self::Timeout(_) = self {
io::Error::new(io::ErrorKind::TimedOut, self)
} else {
io::Error::new(io::ErrorKind::Other, self)
}
Expand Down
42 changes: 42 additions & 0 deletions src/unversioned/transport/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,45 @@ impl<T: Transport> io::Write for TransportAdapter<T> {
Ok(())
}
}

#[cfg(test)]
mod tests {
use std::io::{self, Read};

use super::super::*;
use super::*;

#[test]
fn timeout_errors_translate_transparently() {
#[derive(Debug)]
struct FailingTransport(LazyBuffers);
impl Transport for FailingTransport {
fn buffers(&mut self) -> &mut dyn crate::unversioned::transport::Buffers {
&mut self.0
}

fn transmit_output(
&mut self,
_amount: usize,
_timeout: NextTimeout,
) -> Result<(), crate::Error> {
unimplemented!()
}

fn await_input(&mut self, _timeout: NextTimeout) -> Result<bool, crate::Error> {
Err(crate::Error::Timeout(Timeout::Global))
}

fn is_open(&mut self) -> bool {
unimplemented!()
}
}

let mut adapter = TransportAdapter::new(FailingTransport(LazyBuffers::new(1024, 1024)));
let Err(expected_error) = adapter.read(&mut [0u8; 10]) else {
panic!("Expected error, but got success");
};

assert_eq!(expected_error.kind(), io::ErrorKind::TimedOut);
}
}