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> {