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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use std::{
error::Error as ErrorTrait,
fmt::{self, Debug, Display},
marker::PhantomData,
mem,
};
#[allow(unused_imports)]
use core_extensions::prelude::*;
use crate::{
erased_types::{
c_functions::{adapt_std_fmt, debug_impl, display_impl},
FormattingMode,
},
marker_type::{SyncSend, UnsyncUnsend},
ErasedObject, OpaqueType,
std_types::{RBox, RResult, RString},
};
#[repr(C)]
#[derive(StableAbi)]
#[sabi(inside_abi_stable_crate)]
#[sabi(phantom(M))]
pub struct RBoxError_<M = SyncSend> {
value: RBox<ErasedObject>,
vtable: RErrorVTable<ErasedObject>,
_marker: PhantomData<OpaqueType<M>>,
}
pub type UnsyncRBoxError = RBoxError_<UnsyncUnsend>;
pub type RBoxError = RBoxError_<SyncSend>;
unsafe impl Send for RBoxError_<SyncSend> {}
unsafe impl Sync for RBoxError_<SyncSend> {}
impl<M> RBoxError_<M> {
pub fn from_fmt<T>(value: T) -> Self
where
T: Display + Debug,
{
DebugDisplay {
debug: format!("{:#?}", value),
display: format!("{:#}", value),
}
.piped(Self::new_inner)
}
fn new_inner<T>(value: T) -> Self
where
T: ErrorTrait + 'static,
{
unsafe {
let vtable = RErrorVTable::new::<T>();
let value = value
.piped(RBox::new)
.piped(|x| mem::transmute::<RBox<T>, RBox<ErasedObject>>(x));
Self {
value,
vtable,
_marker: PhantomData,
}
}
}
}
impl RBoxError_<SyncSend> {
pub fn new<T>(value: T) -> Self
where
T: ErrorTrait + Send + Sync + 'static,
{
Self::new_inner(value)
}
}
impl RBoxError_<UnsyncUnsend> {
pub fn new<T>(value: T) -> Self
where
T: ErrorTrait + 'static,
{
Self::new_inner(value)
}
}
impl<M> ErrorTrait for RBoxError_<M> {}
impl<M> Display for RBoxError_<M> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
adapt_std_fmt(&*self.value, self.vtable.display, f)
}
}
impl<M> Debug for RBoxError_<M> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
adapt_std_fmt(&*self.value, self.vtable.debug, f)
}
}
#[repr(C)]
#[derive(StableAbi)]
#[sabi(inside_abi_stable_crate)]
struct RErrorVTable<T> {
debug: extern "C" fn(&T, FormattingMode, &mut RString) -> RResult<(), ()>,
display: extern "C" fn(&T, FormattingMode, &mut RString) -> RResult<(), ()>,
}
impl RErrorVTable<ErasedObject> {
unsafe fn new<T>() -> Self
where
T: ErrorTrait + 'static,
{
let this = RErrorVTable {
debug: debug_impl::<T>,
display: display_impl::<T>,
};
mem::transmute::<RErrorVTable<T>, RErrorVTable<ErasedObject>>(this)
}
}
#[repr(C)]
#[derive(Clone)]
struct DebugDisplay {
debug: String,
display: String,
}
impl Display for DebugDisplay {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Display::fmt(&self.display, f)
}
}
impl Debug for DebugDisplay {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Display::fmt(&self.debug, f)
}
}
impl ErrorTrait for DebugDisplay {}