1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use std::error::Error;
use std::fmt;
use std::ops::Deref;
use std::rc::Rc;

/// A thin wrapper around [`Rc`] to provide [`Clone`] to error types that don't already impl Clone
pub struct RcError<T: Error>(Rc<T>);

impl<T: Error> AsRef<T> for RcError<T> {
	#[inline]
	fn as_ref(&self) -> &T {
		self.0.as_ref()
	}
}

impl<T: Error> fmt::Display for RcError<T> {
	#[inline]
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		<T as fmt::Display>::fmt(self.0.as_ref(), f)
	}
}

impl<T: Error> fmt::Debug for RcError<T> {
	#[inline]
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		<T as fmt::Debug>::fmt(self.0.as_ref(), f)
	}
}

impl<T: Error> Error for RcError<T> {}

impl<T: Error> From<T> for RcError<T> {
	#[inline]
	fn from(inner: T) -> Self {
		Self(Rc::new(inner))
	}
}

impl<T: Error> Clone for RcError<T> {
	#[inline]
	fn clone(&self) -> Self {
		Self(self.0.clone())
	}
}

impl<T: Error> Deref for RcError<T> {
	type Target = T;
	#[inline]
	fn deref(&self) -> &Self::Target {
		self.0.deref()
	}
}