alloy_network/any/error.rs
1//! Error types for converting between `Any` types.
2
3use core::{error::Error, fmt};
4
5/// A ConversionError that can capture any error type that implements the `Error` trait.
6pub struct AnyConversionError {
7 inner: Box<dyn Error + Send + Sync + 'static>,
8}
9
10impl AnyConversionError {
11 /// Creates a new `AnyConversionError` wrapping the given error value.
12 pub fn new<E>(error: E) -> Self
13 where
14 E: Error + Send + Sync + 'static,
15 {
16 Self { inner: Box::new(error) }
17 }
18
19 /// Returns a reference to the underlying error value.
20 pub fn as_error(&self) -> &(dyn Error + Send + Sync + 'static) {
21 self.inner.as_ref()
22 }
23}
24
25impl fmt::Debug for AnyConversionError {
26 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27 fmt::Debug::fmt(&self.inner, f)
28 }
29}
30
31impl fmt::Display for AnyConversionError {
32 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33 fmt::Display::fmt(&self.inner, f)
34 }
35}
36
37impl Error for AnyConversionError {
38 fn source(&self) -> Option<&(dyn Error + 'static)> {
39 self.inner.source()
40 }
41}