Skip to content
This repository has been archived by the owner on May 11, 2023. It is now read-only.

Commit

Permalink
zb: Replace Error::Io(..) by Error::InputOutput(Arc<..>)
Browse files Browse the repository at this point in the history
This is so that we can implement `Clone` for `Error` in a following
commit w/o ending up tranforming all I/O errors to generic string
errors during cloning (`std::io::Error` doesn't implement `Clone`[1]).

We still keep `Io` variant around for backwards-compat but we deprecate
it and don't return errors with that variant for any of our API anymore.

[1]: rust-lang/rust#24135
  • Loading branch information
zeenix committed Dec 9, 2022
1 parent 538c312 commit 9d81a9b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 17 deletions.
10 changes: 6 additions & 4 deletions zbus/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ async fn connect_tcp(addr: TcpAddress) -> Result<Async<TcpStream>> {
async fn connect_tcp(addr: TcpAddress) -> Result<TcpStream> {
TcpStream::connect((addr.host(), addr.port()))
.await
.map_err(Error::Io)
.map_err(|e| Error::InputOutput(e.into()))
}

impl Address {
Expand Down Expand Up @@ -254,15 +254,17 @@ impl Address {
#[cfg(windows)]
{
let stream = run_in_thread(move || UnixStream::connect(p)).await?;
Async::new(stream).map(Stream::Unix).map_err(Error::Io)
Async::new(stream)
.map(Stream::Unix)
.map_err(|e| Error::InputOutput(e.into()))
}

#[cfg(not(windows))]
{
Async::<UnixStream>::connect(p)
.await
.map(Stream::Unix)
.map_err(Error::Io)
.map_err(|e| Error::InputOutput(e.into()))
}
}

Expand All @@ -273,7 +275,7 @@ impl Address {
UnixStream::connect(p)
.await
.map(Stream::Unix)
.map_err(Error::Io)
.map_err(|e| Error::InputOutput(e.into()))
}

#[cfg(not(unix))]
Expand Down
7 changes: 3 additions & 4 deletions zbus/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,9 @@ impl Future for PendingMethodCall {
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
self.poll_before(cx, None).map(|ret| {
ret.map(|(_, r)| r).unwrap_or_else(|| {
Err(crate::Error::Io(io::Error::new(
ErrorKind::BrokenPipe,
"socket closed",
)))
Err(crate::Error::InputOutput(
io::Error::new(ErrorKind::BrokenPipe, "socket closed").into(),
))
})
})
}
Expand Down
11 changes: 10 additions & 1 deletion zbus/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ pub enum Error {
/// Invalid D-Bus address.
Address(String),
/// An I/O error.
#[deprecated(note = "Use `Error::InputOutput` instead")]
Io(io::Error),
/// Invalid message field.
InputOutput(Arc<io::Error>),
/// Invalid message field.
InvalidField,
/// Data too large.
ExcessData,
Expand Down Expand Up @@ -83,7 +86,9 @@ impl PartialEq for Error {
(Self::Variant(s), Self::Variant(o)) => s == o,
(Self::Names(s), Self::Names(o)) => s == o,
(Self::NameTaken, Self::NameTaken) => true,
#[allow(deprecated)]
(Error::Io(_), Self::Io(_)) => false,
(Error::InputOutput(_), Self::InputOutput(_)) => false,
#[cfg(feature = "xml")]
(Self::SerdeXml(_), Self::SerdeXml(_)) => false,
#[cfg(feature = "quick-xml")]
Expand All @@ -98,7 +103,9 @@ impl error::Error for Error {
match self {
Error::InterfaceNotFound => None,
Error::Address(_) => None,
#[allow(deprecated)]
Error::Io(e) => Some(e),
Error::InputOutput(e) => Some(e),
Error::ExcessData => None,
Error::Handshake(_) => None,
Error::IncorrectEndian => None,
Expand Down Expand Up @@ -128,7 +135,9 @@ impl fmt::Display for Error {
Error::InterfaceNotFound => write!(f, "Interface not found"),
Error::Address(e) => write!(f, "address error: {}", e),
Error::ExcessData => write!(f, "excess data"),
#[allow(deprecated)]
Error::Io(e) => write!(f, "I/O error: {}", e),
Error::InputOutput(e) => write!(f, "I/O error: {}", e),
Error::Handshake(e) => write!(f, "D-Bus handshake failed: {}", e),
Error::IncorrectEndian => write!(f, "incorrect endian"),
Error::InvalidField => write!(f, "invalid message field"),
Expand Down Expand Up @@ -158,7 +167,7 @@ impl fmt::Display for Error {

impl From<io::Error> for Error {
fn from(val: io::Error) -> Self {
Error::Io(val)
Error::InputOutput(Arc::new(val))
}
}

Expand Down
11 changes: 7 additions & 4 deletions zbus/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1119,10 +1119,13 @@ impl<'a> SignalStream<'a> {
break None;
}
None => {
return Err(Error::Io(std::io::Error::new(
std::io::ErrorKind::BrokenPipe,
"connection closed",
)))
return Err(Error::InputOutput(
std::io::Error::new(
std::io::ErrorKind::BrokenPipe,
"connection closed",
)
.into(),
))
}
}
};
Expand Down
11 changes: 7 additions & 4 deletions zbus/src/raw/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,13 @@ impl<S: Socket> Connection<S> {
};
self.raw_in_pos += len;
if len == 0 {
return Poll::Ready(Err(crate::Error::Io(std::io::Error::new(
std::io::ErrorKind::UnexpectedEof,
"failed to receive message",
))));
return Poll::Ready(Err(crate::Error::InputOutput(
std::io::Error::new(
std::io::ErrorKind::UnexpectedEof,
"failed to receive message",
)
.into(),
)));
}
}

Expand Down

0 comments on commit 9d81a9b

Please sign in to comment.