This repository has been archived by the owner on Oct 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
clippy found out that our implementation is recursive: ``` error: using `self` as `Display` in `impl Display` will cause infinite recursion --> netlink-proto/src/errors.rs:63:32 | 63 | SocketIo(ref e) => write!(f, "{}: {}", self, e), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: `#[deny(clippy::recursive_format_impl)]` on by default = help: for further information visit rust-lang.github.io/rust-clippy/master/index.html#recursive_format_impl = note: this error originates in the macro `write` (in Nightly builds, run with -Z macro-backtrace for more info) ``` It turns out that this Error implementation is clumsy and that we don't need the inner `ErrorKind` type. This commit removes the `ErrorKind` (which is also exposed in `audit` and `rtnetlink`) and uses `thiserror` to derive most of the boilerplate we need for `Error`.
- Loading branch information
1 parent
bb8d0b3
commit 3d799df
Showing
5 changed files
with
12 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,20 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
use std::{ | ||
error::Error as StdError, | ||
fmt::{self, Debug}, | ||
io, | ||
}; | ||
use std::io; | ||
|
||
use netlink_packet_core::NetlinkMessage; | ||
|
||
#[derive(Debug)] | ||
pub struct Error<T> | ||
where | ||
T: Debug, | ||
{ | ||
kind: ErrorKind<T>, | ||
} | ||
|
||
impl<T> Error<T> | ||
where | ||
T: Debug, | ||
{ | ||
pub fn kind(&self) -> &ErrorKind<T> { | ||
&self.kind | ||
} | ||
|
||
pub fn into_inner(self) -> ErrorKind<T> { | ||
self.kind | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub enum ErrorKind<T> | ||
where | ||
T: Debug, | ||
{ | ||
#[derive(thiserror::Error, Debug)] | ||
pub enum Error<T> { | ||
/// The netlink connection is closed | ||
#[error("the netlink connection is closed")] | ||
ConnectionClosed, | ||
|
||
/// Received an error message as a response | ||
#[error("received an error message as a response: {0:?}")] | ||
NetlinkError(NetlinkMessage<T>), | ||
|
||
/// Error while reading from or writing to the netlink socket | ||
SocketIo(io::Error), | ||
} | ||
|
||
impl<T> From<ErrorKind<T>> for Error<T> | ||
where | ||
T: Debug, | ||
{ | ||
fn from(kind: ErrorKind<T>) -> Error<T> { | ||
Error { kind } | ||
} | ||
} | ||
|
||
impl<T> fmt::Display for Error<T> | ||
where | ||
T: Debug, | ||
{ | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
use crate::ErrorKind::*; | ||
match self.kind() { | ||
SocketIo(ref e) => write!(f, "{}: {}", self, e), | ||
ConnectionClosed => write!(f, "{}", self), | ||
NetlinkError(ref message) => write!(f, "{}: {:?}", self, message), | ||
} | ||
} | ||
} | ||
|
||
impl<T> StdError for Error<T> | ||
where | ||
T: Debug, | ||
{ | ||
fn description(&self) -> &str { | ||
use crate::ErrorKind::*; | ||
match self.kind() { | ||
SocketIo(_) => "Error while reading from or writing to the netlink socket", | ||
ConnectionClosed => "The netlink connection is closed", | ||
NetlinkError(_) => "Received an error message as a response", | ||
} | ||
} | ||
|
||
fn source(&self) -> Option<&(dyn StdError + 'static)> { | ||
if let ErrorKind::SocketIo(ref e) = self.kind() { | ||
Some(e) | ||
} else { | ||
None | ||
} | ||
} | ||
#[error("error while reading from or writing to the netlink socket: {0}")] | ||
SocketIo(#[from] io::Error), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,7 +44,6 @@ pub mod proto { | |
Connection, | ||
ConnectionHandle, | ||
Error, | ||
ErrorKind, | ||
}; | ||
} | ||
pub use netlink_proto::sys; | ||
|