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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
use core::{
fmt::{self, Write as _},
ptr::NonNull,
};
use crate::{export, leb, Format, Write};
const MAX_NUM_BOOL_FLAGS: u8 = 8;
pub struct Formatter<'a> {
#[doc(hidden)]
pub inner: &'a mut InternalFormatter,
}
#[doc(hidden)]
pub struct InternalFormatter {
#[cfg(not(feature = "unstable-test"))]
writer: NonNull<dyn Write>,
#[cfg(feature = "unstable-test")]
bytes: Vec<u8>,
bool_flags: u8,
bools_left: u8,
omit_tag: bool,
}
#[doc(hidden)]
impl InternalFormatter {
#[cfg(not(feature = "unstable-test"))]
pub fn write(&mut self, bytes: &[u8]) {
unsafe { self.writer.as_mut().write(bytes) }
}
#[cfg(not(feature = "unstable-test"))]
pub unsafe fn from_raw(writer: NonNull<dyn Write>) -> Self {
Self {
writer,
bool_flags: 0,
bools_left: MAX_NUM_BOOL_FLAGS,
omit_tag: false,
}
}
#[cfg(not(feature = "unstable-test"))]
pub fn into_raw(self) -> NonNull<dyn Write> {
self.writer
}
pub fn fmt(&mut self, f: &impl Format, omit_tag: bool) {
let old_omit_tag = self.omit_tag;
if omit_tag {
self.omit_tag = true;
}
let formatter = Formatter { inner: self };
f.format(formatter);
if omit_tag {
self.omit_tag = old_omit_tag;
}
}
pub fn needs_tag(&self) -> bool {
!self.omit_tag
}
pub fn with_tag(&mut self, f: impl FnOnce(Formatter)) {
let omit_tag = self.omit_tag;
self.omit_tag = false;
let formatter = Formatter { inner: self };
f(formatter);
self.omit_tag = omit_tag;
}
pub fn leb64(&mut self, x: usize) {
let mut buf: [u8; 10] = [0; 10];
let i = leb::leb64(x, &mut buf);
self.write(&buf[..i])
}
pub fn i8(&mut self, b: &i8) {
self.write(&b.to_le_bytes())
}
pub fn i16(&mut self, b: &i16) {
self.write(&b.to_le_bytes())
}
pub fn i32(&mut self, b: &i32) {
self.write(&b.to_le_bytes())
}
pub fn i64(&mut self, b: &i64) {
self.write(&b.to_le_bytes())
}
pub fn i128(&mut self, b: &i128) {
self.write(&b.to_le_bytes())
}
pub fn isize(&mut self, b: &isize) {
self.leb64(leb::zigzag_encode(*b));
}
pub fn fmt_slice(&mut self, values: &[impl Format]) {
self.leb64(values.len());
let mut is_first = true;
for value in values {
let omit_tag = !is_first;
self.fmt(value, omit_tag);
is_first = false;
}
}
pub fn prim(&mut self, s: &Str) {
self.write(&[s.address as u8])
}
pub fn u8(&mut self, b: &u8) {
self.write(&[*b])
}
pub fn u16(&mut self, b: &u16) {
self.write(&b.to_le_bytes())
}
pub fn u24(&mut self, b: &u32) {
self.write(&b.to_le_bytes()[..3])
}
pub fn u32(&mut self, b: &u32) {
self.write(&b.to_le_bytes())
}
pub fn u64(&mut self, b: &u64) {
self.write(&b.to_le_bytes())
}
pub fn u128(&mut self, b: &u128) {
self.write(&b.to_le_bytes())
}
pub fn usize(&mut self, b: &usize) {
self.leb64(*b);
}
pub fn f32(&mut self, b: &f32) {
self.write(&f32::to_bits(*b).to_le_bytes())
}
pub fn f64(&mut self, b: &f64) {
self.write(&f64::to_bits(*b).to_le_bytes())
}
pub fn str(&mut self, s: &str) {
self.leb64(s.len());
self.write(s.as_bytes());
}
pub fn slice(&mut self, s: &[u8]) {
self.leb64(s.len());
self.write(s);
}
pub fn u8_array(&mut self, a: &[u8]) {
self.write(a);
}
pub fn fmt_array(&mut self, a: &[impl Format]) {
let mut is_first = true;
for value in a {
let omit_tag = !is_first;
self.fmt(value, omit_tag);
is_first = false;
}
}
pub fn istr(&mut self, s: &Str) {
if s.address < 128 {
self.write(&[s.address as u8])
} else {
self.write(&[s.address as u8 | (1 << 7), (s.address >> 7) as u8])
}
}
pub fn bool(&mut self, b: &bool) {
let b_u8 = *b as u8;
self.bool_flags = (self.bool_flags << 1) | b_u8;
self.bools_left -= 1;
if self.bools_left == 0 {
self.flush_and_reset_bools();
}
}
pub fn debug(&mut self, val: &dyn core::fmt::Debug) {
core::write!(FmtWrite { fmt: self }, "{:?}", val).ok();
self.write(&[0xff]);
}
pub fn display(&mut self, val: &dyn core::fmt::Display) {
core::write!(FmtWrite { fmt: self }, "{}", val).ok();
self.write(&[0xff]);
}
pub fn finalize(&mut self) {
if self.bools_left < MAX_NUM_BOOL_FLAGS {
self.flush_and_reset_bools();
}
}
fn flush_and_reset_bools(&mut self) {
let flags = self.bool_flags;
self.u8(&flags);
self.bools_left = MAX_NUM_BOOL_FLAGS;
self.bool_flags = 0;
}
#[inline(never)]
pub fn header(&mut self, s: &Str) {
self.istr(s);
export::timestamp(Formatter { inner: self });
}
}
#[derive(Clone, Copy)]
pub struct Str {
pub(crate) address: u16,
}
struct FmtWrite<'a> {
fmt: &'a mut InternalFormatter,
}
impl fmt::Write for FmtWrite<'_> {
fn write_str(&mut self, s: &str) -> fmt::Result {
self.fmt.write(s.as_bytes());
Ok(())
}
}
#[doc(hidden)]
#[cfg(feature = "unstable-test")]
impl super::InternalFormatter {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
Self {
bytes: vec![],
bool_flags: 0,
bools_left: MAX_NUM_BOOL_FLAGS,
omit_tag: false,
}
}
pub fn bytes(&mut self) -> &[u8] {
self.finalize();
&self.bytes
}
pub fn write(&mut self, bytes: &[u8]) {
self.bytes.extend_from_slice(bytes)
}
pub unsafe fn from_raw(_: NonNull<dyn Write>) -> Self {
core::unreachable!()
}
pub unsafe fn into_raw(self) -> NonNull<dyn Write> {
core::unreachable!()
}
}