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
use failure::Fail;
pub trait ErrorDesc: Copy + Eq {
fn describe(self) -> &'static str;
}
impl ErrorDesc for ! {
fn describe(self) -> &'static str {
self
}
}
pub trait ErrorBase<Val: ?Sized = ()>: Fail + ErrorDesc {
type With: ?Sized + ErrorWith<Val, Without = Self>;
fn val(self, val: Val) -> Self::With where Val: Sized;
}
impl<Val: ?Sized> ErrorBase<Val> for ! {
type With = !;
fn val(self, _: Val) -> Self::With where Val: Sized {
self
}
}
pub trait ErrorWith<Val: ?Sized> {
type Without: Fail;
fn as_error(&self) -> Self::Without;
fn as_value(&self) -> &Val;
fn into_value(self) -> Val where Self: Sized, Val: Sized;
fn into_error(self) -> Self::Without where Self: Sized, Val: Sized {
self.as_error()
}
fn map<F, Ret>(self, f: F) -> <Self::Without as ErrorBase<Ret>>::With
where
Self: Sized,
Val: Sized,
F: Fn(Val) -> Ret,
Self::Without: ErrorBase<Ret>,
<Self::Without as ErrorBase<Ret>>::With: Sized,
{
let err = self.as_error();
err.val(f(self.into_value()))
}
}
impl<Val: ?Sized> ErrorWith<Val> for ! {
type Without = !;
fn as_error(&self) -> ! {
*self
}
fn as_value(&self) -> &Val {
*self
}
fn into_value(self) -> Val where Val: Sized {
self
}
}