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
use crate::prelude::*;
pub fn write_object_field<T: TypeId, S: WriterStream>(name: Ident<'_>, f: impl FnOnce(&mut S) -> T, stream: &mut S, num_fields_written: &mut usize) {
let id = stream.restore_if_void(|stream| {
write_ident(name, stream.bytes());
f(stream)
});
if id != T::void() {
*num_fields_written += 1;
}
}
#[inline]
pub fn write_fields<S: WriterStream, T: TypeId>(max_count: usize, stream: &mut S, f: impl FnOnce(&mut S, &mut usize)) -> usize {
let mut count = 0;
if max_count > 8 {
stream.reserve_and_write_with_varint(max_count as u64, move |stream| {
f(stream, &mut count);
count as u64
});
} else {
f(stream, &mut count);
}
count
}