arb_storage_errors/
any.rs

1use alloc::sync::Arc;
2use core::{error::Error, fmt};
3
4/// Cloneable wrapper around any [`Error`] type.
5///
6/// Used at the storage-errors boundary to erase the underlying
7/// `Database::Error` while still allowing the wrapped error to be inspected
8/// via [`AnyError::as_error`].
9#[derive(Clone)]
10pub struct AnyError {
11    inner: Arc<dyn Error + Send + Sync + 'static>,
12}
13
14impl AnyError {
15    /// Wraps `error` in an [`AnyError`].
16    pub fn new<E>(error: E) -> Self
17    where
18        E: Error + Send + Sync + 'static,
19    {
20        Self {
21            inner: Arc::new(error),
22        }
23    }
24
25    /// Returns the inner [`Error`] trait object.
26    pub fn as_error(&self) -> &(dyn Error + Send + Sync + 'static) {
27        self.inner.as_ref()
28    }
29}
30
31impl fmt::Debug for AnyError {
32    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33        fmt::Debug::fmt(&self.inner, f)
34    }
35}
36
37impl fmt::Display for AnyError {
38    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39        fmt::Display::fmt(&self.inner, f)
40    }
41}
42
43impl Error for AnyError {
44    fn source(&self) -> Option<&(dyn Error + 'static)> {
45        self.inner.source()
46    }
47}