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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use std::{
	fmt::{Debug, Display, Formatter},
	sync::Mutex,
};

use super::{DescriptableMark, Error, Scope};

pub struct SyncError<T>(Mutex<T>)
where
	T: ?Sized + Send + 'static;

impl<S, T> DescriptableMark<SyncError<T>> for S
where
	T: ?Sized + Send + 'static,
	S: Scope + DescriptableMark<T>,
{
}

impl<T> Display for SyncError<T>
where
	T: ?Sized + Display + Send + 'static,
{
	fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
		let item = self.0.lock().unwrap();
		Display::fmt(&*item, f)
	}
}

impl<T> Debug for SyncError<T>
where
	T: ?Sized + Debug + Send + 'static,
{
	fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
		let item = self.0.lock().unwrap();
		Debug::fmt(&*item, f)
	}
}

impl<T> std::error::Error for SyncError<T> where T: ?Sized + std::error::Error + Send + 'static {}

#[cfg(feature = "checked")]
pub trait SyncErrorExt {
	fn sync_into<S>(self) -> Error<S>
	where
		S: Scope + DescriptableMark<Self>;
}

#[cfg(not(feature = "checked"))]
pub trait SyncErrorExt {
	fn sync_into<S>(self) -> Error<S>
	where
		S: Scope;
}

#[cfg(feature = "checked")]
impl<T> SyncErrorExt for T
where
	T: Send + 'static,
{
	fn sync_into<S>(self) -> Error<S>
	where
		S: Scope + DescriptableMark<T>,
	{
		SyncError(Mutex::new(self)).into()
	}
}

#[cfg(not(feature = "checked"))]
impl<T> SyncErrorExt for T
where
	T: Send + 'static,
{
	fn sync_into<S>(self) -> Error<S>
	where
		S: Scope,
	{
		SyncError(Mutex::new(self)).into()
	}
}

#[cfg(feature = "checked")]
pub trait SyncResultExt {
	type Value;
	type Error;
	fn sync_err_into<S>(self) -> Result<Self::Value, Error<S>>
	where
		S: Scope + DescriptableMark<Self::Error>;
}

#[cfg(not(feature = "checked"))]
pub trait SyncResultExt {
	type Value;
	fn sync_err_into<S>(self) -> Result<Self::Value, Error<S>>
	where
		S: Scope;
}

#[cfg(feature = "checked")]
impl<T, E> SyncResultExt for Result<T, E>
where
	E: Send + 'static,
{
	type Value = T;
	type Error = E;
	fn sync_err_into<S>(self) -> Result<T, Error<S>>
	where
		S: Scope + DescriptableMark<E>,
	{
		self.map_err(|e| SyncError(Mutex::new(e)).into())
	}
}

#[cfg(not(feature = "checked"))]
impl<T, E> SyncResultExt for Result<T, E>
where
	E: Send + 'static,
{
	type Value = T;
	fn sync_err_into<S>(self) -> Result<T, Error<S>>
	where
		S: Scope,
	{
		self.map_err(|e| SyncError(Mutex::new(e)).into())
	}
}