liquid_core/error/
clone.rs

1use std::error;
2use std::fmt;
3
4/// An error that can be cloned.
5pub trait ErrorClone: error::Error + Send + Sync + 'static {
6    /// Clone the error.
7    fn clone_box(&self) -> Box<dyn ErrorClone>;
8}
9
10impl<E> ErrorClone for E
11where
12    E: error::Error + Clone + Send + Sync + 'static,
13{
14    fn clone_box(&self) -> Box<dyn ErrorClone> {
15        Box::new(self.clone())
16    }
17}
18
19impl Clone for Box<dyn ErrorClone> {
20    fn clone(&self) -> Box<dyn ErrorClone> {
21        self.clone_box()
22    }
23}
24
25/// A lossy way to have any error be cloneable.
26#[derive(Debug, Clone)]
27pub struct CloneableError {
28    error: String,
29}
30
31impl CloneableError {
32    /// Lossily make any error cloneable.
33    pub fn new<E>(error: E) -> Self
34    where
35        E: error::Error + Send + Sync + 'static,
36    {
37        Self {
38            error: error.to_string(),
39        }
40    }
41}
42
43impl fmt::Display for CloneableError {
44    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45        writeln!(f, "{}", self.error)
46    }
47}
48
49impl error::Error for CloneableError {
50    fn description(&self) -> &str {
51        self.error.as_str()
52    }
53
54    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
55        None
56    }
57}