Skip to content

Commit c11571b

Browse files
authored
derive Eq, PartialEq & Hash for error types (#935)
* derive Eq, PartialEq & Hash for error types Needed in rodio to rate-limit the error callback, (we log to stdout and POLLERR was wiling up logs & disk space rapidly). Recommended to be implemented by the [rust api guidlines](https://rust-lang.github.io/api-guidelines/interoperability.html). * fmt run
1 parent 823814b commit c11571b

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

src/error.rs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::error::Error;
22
use std::fmt::{Display, Formatter};
33

44
/// The requested host, although supported on this platform, is unavailable.
5-
#[derive(Copy, Clone, Debug)]
5+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
66
pub struct HostUnavailable;
77

88
impl Display for HostUnavailable {
@@ -25,7 +25,7 @@ impl Error for HostUnavailable {}
2525
/// **Note:** If you notice a `BackendSpecificError` that you believe could be better handled in a
2626
/// cross-platform manner, please create an issue or submit a pull request with a patch that adds
2727
/// the necessary error variant to the appropriate error enum.
28-
#[derive(Clone, Debug)]
28+
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
2929
pub struct BackendSpecificError {
3030
pub description: String,
3131
}
@@ -43,7 +43,7 @@ impl Display for BackendSpecificError {
4343
impl Error for BackendSpecificError {}
4444

4545
/// An error that might occur while attempting to enumerate the available devices on a system.
46-
#[derive(Clone, Debug)]
46+
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
4747
pub enum DevicesError {
4848
/// See the [`BackendSpecificError`] docs for more information about this error variant.
4949
BackendSpecific { err: BackendSpecificError },
@@ -66,7 +66,7 @@ impl From<BackendSpecificError> for DevicesError {
6666
}
6767

6868
/// An error that may occur while attempting to retrieve a device name.
69-
#[derive(Clone, Debug)]
69+
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
7070
pub enum DeviceNameError {
7171
/// See the [`BackendSpecificError`] docs for more information about this error variant.
7272
BackendSpecific { err: BackendSpecificError },
@@ -89,7 +89,7 @@ impl From<BackendSpecificError> for DeviceNameError {
8989
}
9090

9191
/// Error that can happen when enumerating the list of supported formats.
92-
#[derive(Clone, Debug)]
92+
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
9393
pub enum SupportedStreamConfigsError {
9494
/// The device no longer exists. This can happen if the device is disconnected while the
9595
/// program is running.
@@ -119,7 +119,7 @@ impl From<BackendSpecificError> for SupportedStreamConfigsError {
119119
}
120120

121121
/// May occur when attempting to request the default input or output stream format from a [`Device`](crate::Device).
122-
#[derive(Clone, Debug)]
122+
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
123123
pub enum DefaultStreamConfigError {
124124
/// The device no longer exists. This can happen if the device is disconnected while the
125125
/// program is running.
@@ -134,10 +134,10 @@ impl Display for DefaultStreamConfigError {
134134
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
135135
match self {
136136
Self::BackendSpecific { err } => err.fmt(f),
137-
DefaultStreamConfigError::DeviceNotAvailable => f.write_str(
137+
Self::DeviceNotAvailable => f.write_str(
138138
"The requested device is no longer available. For example, it has been unplugged.",
139139
),
140-
DefaultStreamConfigError::StreamTypeNotSupported => {
140+
Self::StreamTypeNotSupported => {
141141
f.write_str("The requested stream type is not supported by the device.")
142142
}
143143
}
@@ -152,7 +152,7 @@ impl From<BackendSpecificError> for DefaultStreamConfigError {
152152
}
153153
}
154154
/// Error that can happen when creating a [`Stream`](crate::Stream).
155-
#[derive(Clone, Debug)]
155+
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
156156
pub enum BuildStreamError {
157157
/// The device no longer exists. This can happen if the device is disconnected while the
158158
/// program is running.
@@ -174,18 +174,16 @@ impl Display for BuildStreamError {
174174
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
175175
match self {
176176
Self::BackendSpecific { err } => err.fmt(f),
177-
BuildStreamError::DeviceNotAvailable => f.write_str(
177+
Self::DeviceNotAvailable => f.write_str(
178178
"The requested device is no longer available. For example, it has been unplugged.",
179179
),
180-
BuildStreamError::StreamConfigNotSupported => {
180+
Self::StreamConfigNotSupported => {
181181
f.write_str("The requested stream configuration is not supported by the device.")
182182
}
183-
BuildStreamError::InvalidArgument => f.write_str(
183+
Self::InvalidArgument => f.write_str(
184184
"The requested device does not support this capability (invalid argument)",
185185
),
186-
BuildStreamError::StreamIdOverflow => {
187-
f.write_str("Adding a new stream ID would cause an overflow")
188-
}
186+
Self::StreamIdOverflow => f.write_str("Adding a new stream ID would cause an overflow"),
189187
}
190188
}
191189
}
@@ -203,7 +201,7 @@ impl From<BackendSpecificError> for BuildStreamError {
203201
/// As of writing this, only macOS may immediately return an error while calling this method. This
204202
/// is because both the alsa and wasapi backends only enqueue these commands and do not process
205203
/// them immediately.
206-
#[derive(Clone, Debug)]
204+
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
207205
pub enum PlayStreamError {
208206
/// The device associated with the stream is no longer available.
209207
DeviceNotAvailable,
@@ -215,7 +213,7 @@ impl Display for PlayStreamError {
215213
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
216214
match self {
217215
Self::BackendSpecific { err } => err.fmt(f),
218-
PlayStreamError::DeviceNotAvailable => {
216+
Self::DeviceNotAvailable => {
219217
f.write_str("the device associated with the stream is no longer available")
220218
}
221219
}
@@ -235,7 +233,7 @@ impl From<BackendSpecificError> for PlayStreamError {
235233
/// As of writing this, only macOS may immediately return an error while calling this method. This
236234
/// is because both the alsa and wasapi backends only enqueue these commands and do not process
237235
/// them immediately.
238-
#[derive(Clone, Debug)]
236+
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
239237
pub enum PauseStreamError {
240238
/// The device associated with the stream is no longer available.
241239
DeviceNotAvailable,
@@ -247,7 +245,7 @@ impl Display for PauseStreamError {
247245
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
248246
match self {
249247
Self::BackendSpecific { err } => err.fmt(f),
250-
PauseStreamError::DeviceNotAvailable => {
248+
Self::DeviceNotAvailable => {
251249
f.write_str("the device associated with the stream is no longer available")
252250
}
253251
}
@@ -263,7 +261,7 @@ impl From<BackendSpecificError> for PauseStreamError {
263261
}
264262

265263
/// Errors that might occur while a stream is running.
266-
#[derive(Clone, Debug)]
264+
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
267265
pub enum StreamError {
268266
/// The device no longer exists. This can happen if the device is disconnected while the
269267
/// program is running.
@@ -276,7 +274,7 @@ impl Display for StreamError {
276274
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
277275
match self {
278276
Self::BackendSpecific { err } => err.fmt(f),
279-
StreamError::DeviceNotAvailable => f.write_str(
277+
Self::DeviceNotAvailable => f.write_str(
280278
"The requested device is no longer available. For example, it has been unplugged.",
281279
),
282280
}

0 commit comments

Comments
 (0)