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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
use super::ErrorCode;
pub const ERROR_CHAIN_LEN: usize = 4;
#[derive(Clone, Copy, PartialEq, Eq)]
#[repr(transparent)]
pub struct ErrorData {
data: u32,
}
mod consts {
pub const CODE_MASK: [u32; 5] = [
0x0000_000f,
0x0000_00f0,
0x0000_0f00,
0x0000_f000,
0x000f_0000,
];
pub const ALL_CODE_MASK: u32 = 0x000f_ffff;
pub const CODE_WIDTH: u32 = 4;
#[inline(always)]
pub const fn make_code(value: super::ErrorCode) -> u32 {
(value & 0b1111) as u32
}
pub const FORMATTER_MASK: [u32; 4] = [0x0070_0000, 0x0380_0000, 0x1c00_0000, 0xe000_0000];
pub const ALL_FORMATTER_MASK: u32 = 0xfff0_0000;
pub const FORMATTER_BITOFFSET: u32 = 20;
pub const FORMATTER_IDX_WIDTH: u32 = 3;
#[inline(always)]
pub const fn make_formatter_idx(value: u8) -> u32 {
(value & 0b0111) as u32
}
}
impl ErrorData {
pub const fn new(error_code: ErrorCode) -> ErrorData {
ErrorData {
data: error_code as u32 & consts::CODE_MASK[0],
}
}
pub fn set_code(&mut self, code: ErrorCode) -> ErrorCode {
let old_ec = consts::make_code(self.data as u8) as ErrorCode;
self.data = (self.data & !(consts::CODE_MASK[0])) | consts::make_code(code);
old_ec
}
#[inline(always)]
pub fn code(&self) -> ErrorCode {
(self.data & consts::CODE_MASK[0]) as ErrorCode
}
#[inline(always)]
pub fn first_formatter_index(&self) -> Option<u8> {
let fmt_index =
((self.data & consts::FORMATTER_MASK[0]) >> consts::FORMATTER_BITOFFSET) as u8;
if fmt_index > 0 {
Some(fmt_index - 1)
} else {
None
}
}
#[inline]
pub fn chain_len(&self) -> usize {
let mut mask = consts::FORMATTER_MASK[0];
for fmt_index in 0..ERROR_CHAIN_LEN {
if (self.data & mask) == 0 {
return fmt_index;
}
mask <<= consts::FORMATTER_IDX_WIDTH;
}
ERROR_CHAIN_LEN
}
#[inline]
pub fn chain_full(&self) -> bool {
self.chain_len() == ERROR_CHAIN_LEN
}
#[inline(always)]
pub fn push_front(
&mut self,
error_code: ErrorCode,
category_index: u8,
) -> Option<(ErrorCode, u8)> {
let fmt_index_back = self.data & consts::FORMATTER_MASK[ERROR_CHAIN_LEN - 1];
let result = if fmt_index_back > 0 {
let ec_back = (self.data & consts::CODE_MASK[ERROR_CHAIN_LEN])
>> (ERROR_CHAIN_LEN as u32 * consts::CODE_WIDTH);
let fmt_index_back = fmt_index_back
>> ((ERROR_CHAIN_LEN as u32 - 1) * consts::FORMATTER_IDX_WIDTH
+ consts::FORMATTER_BITOFFSET);
Some((ec_back as ErrorCode, (fmt_index_back - 1) as u8))
} else {
None
};
let fmt_indices = ((self.data & consts::ALL_FORMATTER_MASK) << consts::FORMATTER_IDX_WIDTH)
| (consts::make_formatter_idx(category_index + 1) << consts::FORMATTER_BITOFFSET);
let err_codes = ((self.data << consts::CODE_WIDTH) & consts::ALL_CODE_MASK)
| consts::make_code(error_code);
self.data = fmt_indices | err_codes;
result
}
pub fn chain(&mut self, error_code: ErrorCode, category_index: u8) {
let overflow = self.push_front(error_code, category_index);
#[cfg(feature = "panic-on-overflow")]
debug_assert!(
overflow.is_none(),
"chaining two errors overflowed; error chain is full"
);
}
pub(crate) fn iter_chain(&self) -> ErrorDataChainIter {
ErrorDataChainIter {
error_codes: (self.data & consts::ALL_CODE_MASK) >> consts::CODE_WIDTH,
formatters: (self.data & consts::ALL_FORMATTER_MASK) >> consts::FORMATTER_BITOFFSET,
}
}
}
pub(crate) struct ErrorDataChainIter {
error_codes: u32,
formatters: u32,
}
impl Iterator for ErrorDataChainIter {
type Item = (ErrorCode, Option<u8>);
fn next(&mut self) -> Option<Self::Item> {
if self.formatters > 0 {
let ec = self.error_codes & consts::CODE_MASK[0];
self.error_codes >>= consts::CODE_WIDTH;
self.formatters >>= consts::FORMATTER_IDX_WIDTH;
let next_fmt_index = {
let next_fmt_index = consts::make_formatter_idx(self.formatters as u8);
if next_fmt_index > 0 {
Some(next_fmt_index as u8 - 1)
} else {
None
}
};
Some((ec as ErrorCode, next_fmt_index))
} else {
None
}
}
}