use std::error;
use std::fmt;
pub trait ErrorClone: error::Error + Send + Sync + 'static {
fn clone_box(&self) -> Box<dyn ErrorClone>;
}
impl<E> ErrorClone for E
where
E: error::Error + Clone + Send + Sync + 'static,
{
fn clone_box(&self) -> Box<dyn ErrorClone> {
Box::new(self.clone())
}
}
impl Clone for Box<dyn ErrorClone> {
fn clone(&self) -> Box<dyn ErrorClone> {
self.clone_box()
}
}
#[derive(Debug, Clone)]
pub struct CloneableError {
error: String,
}
impl CloneableError {
pub fn new<E>(error: E) -> Self
where
E: error::Error + Send + Sync + 'static,
{
Self {
error: error.to_string(),
}
}
}
impl fmt::Display for CloneableError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "{}", self.error)
}
}
impl error::Error for CloneableError {
fn description(&self) -> &str {
self.error.as_str()
}
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
None
}
}