From ddcf7bd2ab4a1cd06ef2fe3da601591437e8e86f Mon Sep 17 00:00:00 2001 From: Daniel Verkamp Date: Wed, 18 Aug 2021 14:19:22 -0700 Subject: [PATCH] cros_async: convert to ThisError and sort BUG=b:197143586 TEST=cargo check Cq-Depend: chromium:3105313 Change-Id: Ic9757b7e1947970910245fe954e47e4a2b7aa28e Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3105074 Tested-by: kokoro Commit-Queue: Daniel Verkamp Reviewed-by: Chirantan Ekbote --- Cargo.lock | 1 + cros_async/Cargo.toml | 1 + cros_async/src/fd_executor.rs | 8 +++++--- cros_async/src/io_ext.rs | 2 ++ cros_async/src/lib.rs | 14 ++++++++------ cros_async/src/mem.rs | 23 +++++------------------ cros_async/src/poll_source.rs | 2 ++ cros_async/src/uring_executor.rs | 8 +++++--- 8 files changed, 29 insertions(+), 30 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2e94e4d280..4df0e963b1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -196,6 +196,7 @@ dependencies = [ "once_cell", "paste", "pin-utils", + "remain", "slab", "sync", "sys_util", diff --git a/cros_async/Cargo.toml b/cros_async/Cargo.toml index cd9417c761..62268bd640 100644 --- a/cros_async/Cargo.toml +++ b/cros_async/Cargo.toml @@ -14,6 +14,7 @@ libc = "*" once_cell = "1.7.2" paste = "1.0" pin-utils = "0.1.0-alpha.4" +remain = "0.2" slab = "0.4" sync = { path = "../sync" } # provided by ebuild sys_util = { path = "../sys_util" } # provided by ebuild diff --git a/cros_async/src/fd_executor.rs b/cros_async/src/fd_executor.rs index 339f9fa228..dda5c40b05 100644 --- a/cros_async/src/fd_executor.rs +++ b/cros_async/src/fd_executor.rs @@ -22,6 +22,7 @@ use std::task::{Context, Poll, Waker}; use async_task::Task; use futures::task::noop_waker; use pin_utils::pin_mut; +use remain::sorted; use slab::Slab; use sync::Mutex; use sys_util::{add_fd_flags, warn, EpollContext, EpollEvents, EventFd, WatchingEvents}; @@ -30,6 +31,7 @@ use thiserror::Error as ThisError; use crate::waker::{new_waker, WakerToken, WeakWake}; use crate::{queue::RunnableQueue, BlockingPool}; +#[sorted] #[derive(Debug, ThisError)] pub enum Error { /// Failed to clone the EventFd for waking the executor. @@ -38,15 +40,15 @@ pub enum Error { /// Failed to create the EventFd for waking the executor. #[error("Failed to create the EventFd for waking the executor: {0}")] CreateEventFd(sys_util::Error), + /// Creating a context to wait on FDs failed. + #[error("An error creating the fd waiting context: {0}")] + CreatingContext(sys_util::Error), /// Failed to copy the FD for the polling context. #[error("Failed to copy the FD for the polling context: {0}")] DuplicatingFd(sys_util::Error), /// The Executor is gone. #[error("The FDExecutor is gone")] ExecutorGone, - /// Creating a context to wait on FDs failed. - #[error("An error creating the fd waiting context: {0}")] - CreatingContext(sys_util::Error), /// PollContext failure. #[error("PollContext failure: {0}")] PollContextError(sys_util::Error), diff --git a/cros_async/src/io_ext.rs b/cros_async/src/io_ext.rs index 81ad987a59..ea79794df9 100644 --- a/cros_async/src/io_ext.rs +++ b/cros_async/src/io_ext.rs @@ -22,11 +22,13 @@ use std::os::unix::io::{AsRawFd, RawFd}; use std::sync::Arc; use async_trait::async_trait; +use remain::sorted; use sys_util::net::UnixSeqpacket; use thiserror::Error as ThisError; use crate::{BackingMemory, MemRegion}; +#[sorted] #[derive(ThisError, Debug)] pub enum Error { /// An error with a polled(FD) source. diff --git a/cros_async/src/lib.rs b/cros_async/src/lib.rs index f9604e5dcc..30ca9b00fe 100644 --- a/cros_async/src/lib.rs +++ b/cros_async/src/lib.rs @@ -96,22 +96,24 @@ use std::marker::PhantomData; use std::pin::Pin; use std::task::{Context, Poll}; +use remain::sorted; use thiserror::Error as ThisError; +#[sorted] #[derive(ThisError, Debug)] pub enum Error { /// Error from the FD executor. #[error("Failure in the FD executor: {0}")] FdExecutor(fd_executor::Error), - /// Error from the uring executor. - #[error("Failure in the uring executor: {0}")] - URingExecutor(uring_executor::Error), - /// Error from TimerFd. - #[error("Failure in TimerFd: {0}")] - TimerFd(sys_util::Error), /// Error from TimerFd. #[error("Failure in TimerAsync: {0}")] TimerAsync(AsyncError), + /// Error from TimerFd. + #[error("Failure in TimerFd: {0}")] + TimerFd(sys_util::Error), + /// Error from the uring executor. + #[error("Failure in the uring executor: {0}")] + URingExecutor(uring_executor::Error), } pub type Result = std::result::Result; diff --git a/cros_async/src/mem.rs b/cros_async/src/mem.rs index bf612b7dee..691e629fec 100644 --- a/cros_async/src/mem.rs +++ b/cros_async/src/mem.rs @@ -2,32 +2,19 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -use std::fmt::{self, Display}; - use data_model::VolatileSlice; +use remain::sorted; +use thiserror::Error as ThisError; -#[derive(Debug)] +#[sorted] +#[derive(ThisError, Debug)] pub enum Error { /// Invalid offset or length given for an iovec in backing memory. + #[error("Invalid offset/len for getting a slice from {0} with len {1}.")] InvalidOffset(u64, usize), } pub type Result = std::result::Result; -impl Display for Error { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - use self::Error::*; - - match self { - InvalidOffset(base, len) => write!( - f, - "Invalid offset/len for getting a slice from {} with len {}.", - base, len - ), - } - } -} -impl std::error::Error for Error {} - /// Used to index subslices of backing memory. Like an iovec, but relative to the start of the /// memory region instead of an absolute pointer. /// The backing memory referenced by the region can be an array, an mmapped file, or guest memory. diff --git a/cros_async/src/poll_source.rs b/cros_async/src/poll_source.rs index 1fd7f060c0..a4aed09944 100644 --- a/cros_async/src/poll_source.rs +++ b/cros_async/src/poll_source.rs @@ -12,6 +12,7 @@ use std::sync::Arc; use async_trait::async_trait; use data_model::VolatileSlice; +use remain::sorted; use thiserror::Error as ThisError; use crate::fd_executor::{self, FdExecutor, RegisteredSource}; @@ -19,6 +20,7 @@ use crate::mem::{BackingMemory, MemRegion}; use crate::{AsyncError, AsyncResult}; use crate::{IoSourceExt, ReadAsync, WriteAsync}; +#[sorted] #[derive(ThisError, Debug)] pub enum Error { /// An error occurred attempting to register a waker with the executor. diff --git a/cros_async/src/uring_executor.rs b/cros_async/src/uring_executor.rs index a38e6c71b7..df7be2b0a0 100644 --- a/cros_async/src/uring_executor.rs +++ b/cros_async/src/uring_executor.rs @@ -70,6 +70,7 @@ use futures::task::noop_waker; use io_uring::URingContext; use once_cell::sync::Lazy; use pin_utils::pin_mut; +use remain::sorted; use slab::Slab; use sync::Mutex; use sys_util::{warn, WatchingEvents}; @@ -82,8 +83,12 @@ use crate::{ BlockingPool, }; +#[sorted] #[derive(Debug, ThisError)] pub enum Error { + /// Creating a context to wait on FDs failed. + #[error("Error creating the fd waiting context: {0}")] + CreatingContext(io_uring::Error), /// Failed to copy the FD for the polling context. #[error("Failed to copy the FD for the polling context: {0}")] DuplicatingFd(sys_util::Error), @@ -99,9 +104,6 @@ pub enum Error { /// Error doing the IO. #[error("Error during IO: {0}")] Io(io::Error), - /// Creating a context to wait on FDs failed. - #[error("Error creating the fd waiting context: {0}")] - CreatingContext(io_uring::Error), /// Failed to remove the waker remove the polling context. #[error("Error removing from the URing context: {0}")] RemovingWaker(io_uring::Error),