From 06875127b2217808350946004e756301f8161d5a Mon Sep 17 00:00:00 2001 From: Daniel Verkamp Date: Wed, 18 Aug 2021 14:25:11 -0700 Subject: [PATCH] vhost: convert to ThisError and sort BUG=b:197143586 TEST=cargo check Change-Id: Ib3ecfa6e6c9fc88edcf592d511cacfd97b2dd195 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3105429 Tested-by: kokoro Commit-Queue: Daniel Verkamp Reviewed-by: Keiichi Watanabe --- Cargo.lock | 2 ++ vhost/Cargo.toml | 2 ++ vhost/src/lib.rs | 50 +++++++++++++++++++++--------------------------- 3 files changed, 26 insertions(+), 28 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 23de9082c2..84bf53127f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1243,6 +1243,8 @@ dependencies = [ "base", "libc", "net_util", + "remain", + "thiserror", "virtio_sys", "vm_memory", ] diff --git a/vhost/Cargo.toml b/vhost/Cargo.toml index 1e2253f8dd..63282b22fa 100644 --- a/vhost/Cargo.toml +++ b/vhost/Cargo.toml @@ -9,5 +9,7 @@ assertions = { path = "../assertions" } libc = "*" net_util = { path = "../net_util" } base = { path = "../base" } +remain = "*" +thiserror = "*" virtio_sys = { path = "../virtio_sys" } vm_memory = { path = "../vm_memory" } diff --git a/vhost/src/lib.rs b/vhost/src/lib.rs index 4a70599f31..c4287e3ec4 100644 --- a/vhost/src/lib.rs +++ b/vhost/src/lib.rs @@ -10,50 +10,44 @@ pub use crate::net::NetT; pub use crate::vsock::Vsock; use std::alloc::Layout; -use std::fmt::{self, Display}; use std::io::Error as IoError; use std::ptr::null; use assertions::const_assert; use base::{ioctl, ioctl_with_mut_ref, ioctl_with_ptr, ioctl_with_ref}; use base::{AsRawDescriptor, Event, LayoutAllocation}; +use remain::sorted; +use thiserror::Error; use vm_memory::{GuestAddress, GuestMemory, GuestMemoryError}; -#[derive(Debug)] +#[sorted] +#[derive(Error, Debug)] pub enum Error { - /// Error opening vhost device. - VhostOpen(IoError), - /// Error while running ioctl. - IoctlError(IoError), - /// Invalid queue. - InvalidQueue, - /// Invalid descriptor table address. - DescriptorTableAddress(GuestMemoryError), - /// Invalid used address. - UsedAddress(GuestMemoryError), /// Invalid available address. + #[error("invalid available address: {0}")] AvailAddress(GuestMemoryError), + /// Invalid descriptor table address. + #[error("invalid descriptor table address: {0}")] + DescriptorTableAddress(GuestMemoryError), + /// Invalid queue. + #[error("invalid queue")] + InvalidQueue, + /// Error while running ioctl. + #[error("failed to run ioctl: {0}")] + IoctlError(IoError), /// Invalid log address. + #[error("invalid log address: {0}")] LogAddress(GuestMemoryError), + /// Invalid used address. + #[error("invalid used address: {0}")] + UsedAddress(GuestMemoryError), + /// Error opening vhost device. + #[error("failed to open vhost device: {0}")] + VhostOpen(IoError), } + pub type Result = std::result::Result; -impl Display for Error { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - use self::Error::*; - - match self { - VhostOpen(e) => write!(f, "failed to open vhost device: {}", e), - IoctlError(e) => write!(f, "failed to run ioctl: {}", e), - InvalidQueue => write!(f, "invalid queue"), - DescriptorTableAddress(e) => write!(f, "invalid descriptor table address: {}", e), - UsedAddress(e) => write!(f, "invalid used address: {}", e), - AvailAddress(e) => write!(f, "invalid available address: {}", e), - LogAddress(e) => write!(f, "invalid log address: {}", e), - } - } -} - fn ioctl_result() -> Result { Err(Error::IoctlError(IoError::last_os_error())) }