reth_storage_errors/any.rs
1use alloc::sync::Arc;
2use core::{error::Error, fmt};
3
4/// A thread-safe cloneable wrapper for any error type.
5#[derive(Clone)]
6pub struct AnyError {
7 inner: Arc<dyn Error + Send + Sync + 'static>,
8}
9
10impl AnyError {
11 /// Creates a new `AnyError` 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: Arc::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 AnyError {
26 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27 fmt::Debug::fmt(&self.inner, f)
28 }
29}
30
31impl fmt::Display for AnyError {
32 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33 fmt::Display::fmt(&self.inner, f)
34 }
35}
36
37impl Error for AnyError {
38 fn source(&self) -> Option<&(dyn Error + 'static)> {
39 self.inner.source()
40 }
41}