From b09646303e63737de0fd0795170f4af891d2f41a Mon Sep 17 00:00:00 2001 From: Daniel Verkamp Date: Wed, 18 Aug 2021 14:18:09 -0700 Subject: [PATCH] audio_streams: convert to ThisError and sort BUG=b:197143586 TEST=cargo check Cq-Depend: chromium:3105308 Change-Id: Id48674c3fb0536a72a14a945a9f76bc58649fb46 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3105072 Tested-by: kokoro Commit-Queue: Daniel Verkamp Reviewed-by: Chih-Yang Hsia --- Cargo.lock | 2 ++ audio_streams/Cargo.toml | 2 ++ audio_streams/src/audio_streams.rs | 44 ++++++++---------------------- audio_streams/src/capture.rs | 18 ++++-------- audio_streams/src/shm_streams.rs | 23 ++++------------ 5 files changed, 26 insertions(+), 63 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 11b1b9de76..efce94c63b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -90,8 +90,10 @@ version = "0.1.0" dependencies = [ "async-trait", "cros_async", + "remain", "sync", "sys_util", + "thiserror", ] [[package]] diff --git a/audio_streams/Cargo.toml b/audio_streams/Cargo.toml index 49160a8197..a9958dcb00 100644 --- a/audio_streams/Cargo.toml +++ b/audio_streams/Cargo.toml @@ -10,5 +10,7 @@ path = "src/audio_streams.rs" [dependencies] async-trait = "0.1.36" cros_async = { path = "../cros_async" } # provided by ebuild +remain = "0.2" sync = { path = "../sync" } # provided by ebuild sys_util = { path = "../sys_util" } # provided by ebuild +thiserror = "1.0.20" diff --git a/audio_streams/src/audio_streams.rs b/audio_streams/src/audio_streams.rs index b1251963fd..3f8d96af2c 100644 --- a/audio_streams/src/audio_streams.rs +++ b/audio_streams/src/audio_streams.rs @@ -51,6 +51,8 @@ use std::str::FromStr; use std::time::{Duration, Instant}; use cros_async::{Executor, TimerAsync}; +use remain::sorted; +use thiserror::Error; #[derive(Copy, Clone, Debug, PartialEq)] pub enum SampleFormat { @@ -111,21 +113,13 @@ impl Default for StreamEffect { pub type BoxError = Box; /// Errors that are possible from a `StreamEffect`. -#[derive(Debug)] +#[sorted] +#[derive(Error, Debug)] pub enum StreamEffectError { + #[error("Must be in [EchoCancellation, aec]")] InvalidEffect, } -impl error::Error for StreamEffectError {} - -impl Display for StreamEffectError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self { - StreamEffectError::InvalidEffect => write!(f, "Must be in [EchoCancellation, aec]"), - } - } -} - impl FromStr for StreamEffect { type Err = StreamEffectError; fn from_str(s: &str) -> std::result::Result { @@ -136,21 +130,13 @@ impl FromStr for StreamEffect { } } -#[derive(Debug)] +#[sorted] +#[derive(Error, Debug)] pub enum Error { + #[error("Unimplemented")] Unimplemented, } -impl error::Error for Error {} - -impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self { - Error::Unimplemented => write!(f, "Unimplemented"), - } - } -} - /// `StreamSource` creates streams for playback or capture of audio. pub trait StreamSource: Send { /// Returns a stream control and buffer generator object. These are separate as the buffer @@ -329,21 +315,13 @@ pub trait AsyncBufferCommit { } /// Errors that are possible from a `PlaybackBuffer`. -#[derive(Debug)] +#[sorted] +#[derive(Error, Debug)] pub enum PlaybackBufferError { + #[error("Invalid buffer length")] InvalidLength, } -impl error::Error for PlaybackBufferError {} - -impl Display for PlaybackBufferError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self { - PlaybackBufferError::InvalidLength => write!(f, "Invalid buffer length"), - } - } -} - /// `AudioBuffer` is one buffer that holds buffer_size audio frames. /// It is the inner data of `PlaybackBuffer` and `CaptureBuffer`. struct AudioBuffer<'a> { diff --git a/audio_streams/src/capture.rs b/audio_streams/src/capture.rs index 70d983207c..1aa1de2794 100644 --- a/audio_streams/src/capture.rs +++ b/audio_streams/src/capture.rs @@ -32,8 +32,6 @@ use async_trait::async_trait; use std::{ - error, - fmt::{self, Display}, io::{self, Read}, time::{Duration, Instant}, }; @@ -42,6 +40,8 @@ use super::{ AsyncBufferCommit, AudioBuffer, BoxError, BufferCommit, NoopBufferCommit, SampleFormat, }; use cros_async::{Executor, TimerAsync}; +use remain::sorted; +use thiserror::Error; /// `CaptureBufferStream` provides `CaptureBuffer`s to read with audio samples from capture. pub trait CaptureBufferStream: Send { @@ -100,21 +100,13 @@ pub struct AsyncCaptureBuffer<'a> { } /// Errors that are possible from a `CaptureBuffer`. -#[derive(Debug)] +#[sorted] +#[derive(Error, Debug)] pub enum CaptureBufferError { + #[error("Invalid buffer length")] InvalidLength, } -impl error::Error for CaptureBufferError {} - -impl Display for CaptureBufferError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self { - CaptureBufferError::InvalidLength => write!(f, "Invalid buffer length"), - } - } -} - impl<'a> CaptureBuffer<'a> { /// Creates a new `CaptureBuffer` that holds a reference to the backing memory specified in /// `buffer`. diff --git a/audio_streams/src/shm_streams.rs b/audio_streams/src/shm_streams.rs index b11626fdea..02ae29d7d8 100644 --- a/audio_streams/src/shm_streams.rs +++ b/audio_streams/src/shm_streams.rs @@ -1,14 +1,15 @@ // Copyright 2019 The Chromium OS Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -use std::error; -use std::fmt; + use std::os::unix::io::RawFd; use std::sync::Arc; use std::time::{Duration, Instant}; +use remain::sorted; use sync::{Condvar, Mutex}; use sys_util::SharedMemory; +use thiserror::Error; use crate::{BoxError, SampleFormat, StreamDirection, StreamEffect}; @@ -29,25 +30,13 @@ pub trait BufferSet { fn ignore(&mut self) -> GenericResult<()>; } -#[derive(Debug)] +#[sorted] +#[derive(Error, Debug)] pub enum Error { + #[error("Provided number of frames {0} exceeds requested number of frames {1}")] TooManyFrames(usize, usize), } -impl error::Error for Error {} - -impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self { - Error::TooManyFrames(provided, requested) => write!( - f, - "Provided number of frames {} exceeds requested number of frames {}", - provided, requested - ), - } - } -} - /// `ServerRequest` represents an active request from the server for the client /// to provide a buffer in shared memory to playback from or capture to. pub struct ServerRequest<'a> {